Let Clipext take stdin 1 line at a time

This commit is contained in:
Ethan Dalool 2017-04-07 17:14:25 -07:00
parent a63b397232
commit 12f8ce9dcf

View file

@ -4,7 +4,7 @@ CLIPBOARD_STRINGS = ['!c', '!clip', '!clipboard']
INPUT_STRINGS = ['!i', '!in', '!input', '!stdin'] INPUT_STRINGS = ['!i', '!in', '!input', '!stdin']
EOF = '\x1a' EOF = '\x1a'
def multi_line_input(): def _input_lines():
userinput = [] userinput = []
while True: while True:
try: try:
@ -15,17 +15,33 @@ def multi_line_input():
else: else:
userinput.append(additional) userinput.append(additional)
if EOF in additional: additional = additional.split(EOF)
has_eof = len(additional) > 1
additional = additional[0]
yield additional
if has_eof:
break break
userinput = '\n'.join(userinput) def multi_line_input(split_lines=False):
userinput = userinput.split(EOF)[0] generator = _input_lines()
return userinput.strip() if split_lines:
return generator
else:
return '\n'.join(generator)
def resolve(arg): def resolve(arg, split_lines=False):
lowered = arg.lower() lowered = arg.lower()
if lowered in CLIPBOARD_STRINGS:
return pyperclip.paste()
if lowered in INPUT_STRINGS: if lowered in INPUT_STRINGS:
return multi_line_input() return multi_line_input(split_lines=split_lines)
return arg elif lowered in CLIPBOARD_STRINGS:
text = pyperclip.paste()
else:
text = arg
if split_lines:
lines = text.splitlines()
return lines
else:
return text