diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2024-11-17 22:38:59 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2024-11-17 22:58:55 +1300 |
commit | 841c95e3c745ae873789a126682f7e75081477c9 (patch) | |
tree | 70195cd5cb1112765e99edab0ab8afb82bc4276b | |
parent | Move encoding fetch inline (diff) | |
download | vixf-841c95e3c745ae873789a126682f7e75081477c9.tar.gz vixf-841c95e3c745ae873789a126682f7e75081477c9.zip |
Make terminal emulator and editor configurable
-rw-r--r-- | config.sample | 13 | ||||
-rw-r--r-- | i3.config | 7 | ||||
-rw-r--r-- | vixf.py3 | 73 |
3 files changed, 79 insertions, 14 deletions
diff --git a/config.sample b/config.sample new file mode 100644 index 0000000..7e6a113 --- /dev/null +++ b/config.sample @@ -0,0 +1,13 @@ +[terminal] +#required = False +# OR +#required = True # default anyway +#command = x-terminal-emulator -name vixf +#command = gnome-terminal +#command = kitty # weirdo + +[editor] +#command = gvim -f +#command = nano +#command = emacs -nw # terminal.required = True +#command = emacs # terminal.required = False @@ -1,5 +1,10 @@ # Key binding (see also: sxhkdrc) bindsym Mod4+grave exec vixf -# Make vixf terminal windows floating +# Make vixf terminal windows floating; for this to work, you need to have this +# in your ~/.config/vixf/config; it doesn't work out of the box. +# +# [terminal] +# command = xterm -name vixf +# for_window [class="XTerm" instance="vixf"] floating enable @@ -7,8 +7,11 @@ edit that file with the terminal editor, and when the editor is done, read the text. """ +import configparser import locale import os +import shlex +import shutil import subprocess import sys import tempfile @@ -19,15 +22,18 @@ import pyperclip SELF = 'vixf' -EDITOR = 'vi' FOCUS_DELAY = 0.2 -TERM = 'x-terminal-emulator' def main(environ): """ Just bundle everything into a main function. """ + config = configparser.ConfigParser() + config.read( + os.path.expanduser(f'~/.config/{SELF}/config'), + encoding=locale.getpreferredencoding(), + ) time.sleep(FOCUS_DELAY) @@ -59,18 +65,13 @@ def main(environ): tf.write(content_before) tf.close() - if 'VISUAL' in environ: - editor = environ['VISUAL'] - elif 'EDITOR' in environ: - editor = environ['EDITOR'] - else: - editor = EDITOR + editor = select_editor(config, environ) + terminal = select_terminal(config) + + command = shlex.split(editor) + [tf.name] + if terminal: + command = shlex.split(terminal) + ['-e'] + command - command = [ - TERM, - '-name', SELF, - '-e', editor, tf.name, - ] subprocess.run(command, check=True) with open( @@ -94,5 +95,51 @@ def main(environ): keyboard.type('x') +def select_editor(config, environ): + """ + Try very hard to pick a prefered and existent editor. Try VISUAL first, + then EDITOR, then rattle through some common paths, and just return "vi" + and hope for the best otherwise. + """ + editor = config.get('editor', 'command', fallback=None) + if not editor: + if 'VISUAL' in environ: + editor = environ['VISUAL'] + elif 'EDITOR' in environ: + editor = environ['EDITOR'] + else: + for candidate in [ + 'editor', + 'sensible-editor', + 'vi', + ]: + editor = candidate + if shutil.which(editor): + break + return editor + + +def select_terminal(config): + """ + Try very hard to pick a prefered and existent terminal emulator, or return + "None" if we're configured not to need one. Last resort is "xterm". + """ + if config.getboolean('terminal', 'required', fallback=True): + terminal = None + terminal = config.get('terminal', 'command', fallback=None) + if not terminal: + for candidate in [ + 'x-terminal-emulator', + 'sensible-terminal', + 'xterm', + ]: + terminal = candidate + if shutil.which(terminal): + break + else: + terminal = None + return terminal + + if __name__ == '__main__': main(os.environ) |