-
-
Save pkillnine/f20d69bf852c71bc5bb1358c4667850f to your computer and use it in GitHub Desktop.
qutebrowser userscript: 'read' (emulates vim's 'read' command)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# This script is a userscript for qutebrowser. Written by Pkill9. Contact me on IRC at #qutebrowser on Freenode for any questions. | |
# | |
# Emulates vim's 'read' command | |
# | |
# WARNING: the command outputs are stored in qutebrowser's | |
# debug log reachable via the url qute://log | |
import os | |
from sys import argv | |
import subprocess | |
def sanitise_text(text): | |
text = text.replace('\\', '\\\\') | |
text = text.replace('\n', '\\n') | |
text = text.replace("'", r"\'") | |
text = text.replace('"', r'\"') | |
text = text.replace(')', '\)') | |
text = text.replace('(', '\(') | |
text = text.replace('{', '\{') | |
text = text.replace('}', '\}') | |
return text | |
def exec_qb(command): | |
with open(os.environ['QUTE_FIFO'], 'w') as f: | |
f.write(command) | |
def insert_to_active_element(text): | |
text = sanitise_text(text) | |
js="document.activeElement.value += '{}';".format(text) | |
command = "jseval -q {}".format(js) | |
exec_qb(command) | |
if __name__ == "__main__": | |
if len(argv) > 1 and argv[1][0] == '!': | |
command = ' '.join(argv[1:]) | |
command = command[1:] #Remove first exclamation mark | |
command = command.strip() | |
cmd_output = subprocess.check_output(command, shell=True).decode() | |
insert_to_active_element(cmd_output) | |
elif len(argv) > 1: | |
with open(argv[1]) as f: | |
file_output = f.read().strip() | |
insert_to_active_element(file_output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment