diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2024-11-17 19:53:24 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2024-11-17 19:53:24 +1300 |
commit | 9269424b70ec6baed37bf6fd02f3c5b2b17b1b10 (patch) | |
tree | 3e725a9ca7d2a5fe3d45fe5ed0a95109c6db754f | |
parent | Sort constants alphabetically, remove spaces (diff) | |
download | vixf-9269424b70ec6baed37bf6fd02f3c5b2b17b1b10.tar.gz vixf-9269424b70ec6baed37bf6fd02f3c5b2b17b1b10.zip |
Refactor into module with class
This is over-engineered already and pylint is upset about it, so I'm
just recording this working state until I can replace it with just
a main().
-rw-r--r-- | vixf.py3 | 148 |
1 files changed, 90 insertions, 58 deletions
@@ -17,62 +17,94 @@ import time from pynput.keyboard import Key, Controller import pyperclip -SELF = 'vixf' -EDITOR = 'vi' -FOCUS_DELAY = 0.2 -TERM = 'x-terminal-emulator' - -keyboard = Controller() - -time.sleep(FOCUS_DELAY) - -# Select all -with keyboard.pressed(Key.ctrl): - keyboard.type('a') - -# Copy -with keyboard.pressed(Key.ctrl): - keyboard.type('c') - -# Read clipboard -content_before = pyperclip.paste() - -# Stop here (error condition) if there's nothing in the clipboard -if len(content_before) == 0: - print( - f'{SELF}: Nothing to edit. Did the selection work?', - file=sys.stderr, - ) - sys.exit(1) - -with tempfile.NamedTemporaryFile(mode='w', delete_on_close=False) as tf: - tf.write(content_before) - tf.close() - - env = os.environ - if 'VISUAL' in env: - editor = env['VISUAL'] - elif 'EDITOR' in env: - editor = env['EDITOR'] - else: - editor = EDITOR - - command = [ - TERM, - '-name', SELF, - '-e', editor, tf.name, - ] - subprocess.run(command, check=True) - - encoding = locale.getpreferredencoding() - with open(tf.name, mode='r', encoding=encoding) as tfr: - content_after = tfr.read() - -pyperclip.copy(content_after) - -time.sleep(FOCUS_DELAY) - -# Paste -with keyboard.pressed(Key.ctrl): - keyboard.type('v') +class ViXF(): + """ + Using a class, to encapsulate a fair bit of system state that gets injected + into this. + """ + + SELF = 'vixf' + + EDITOR = 'vi' + FOCUS_DELAY = 0.2 + TERM = 'x-terminal-emulator' + + def __init__(self, environ): + """ + Object setup: read environment as provided by whatever instantiated us, + and set up a keyboard controller object. + """ + self.environ = environ + self.keyboard = Controller() + + def exec(self): + """ + Perform the actual keyboard automation and editor instantiation. + """ + + time.sleep(self.FOCUS_DELAY) + + # Select all + with self.keyboard.pressed(Key.ctrl): + self.keyboard.type('a') + + # Copy + with self.keyboard.pressed(Key.ctrl): + self.keyboard.type('c') + + # Read clipboard + content_before = pyperclip.paste() + + # Stop here (error condition) if there's nothing in the clipboard + if len(content_before) == 0: + print( + f'{self.SELF}: Nothing to edit. Did the selection work?', + file=sys.stderr, + ) + sys.exit(1) + + with tempfile.NamedTemporaryFile( + mode='w', + delete_on_close=False + ) as tf: + tf.write(content_before) + tf.close() + + if 'VISUAL' in self.environ: + editor = self.environ['VISUAL'] + elif 'EDITOR' in self.environ: + editor = self.environ['EDITOR'] + else: + editor = self.EDITOR + + command = [ + self.TERM, + '-name', self.SELF, + '-e', editor, tf.name, + ] + subprocess.run(command, check=True) + + encoding = locale.getpreferredencoding() + with open(tf.name, mode='r', encoding=encoding) as tfr: + content_after = tfr.read() + + pyperclip.copy(content_after) + + time.sleep(self.FOCUS_DELAY) + + # Paste + with self.keyboard.pressed(Key.ctrl): + self.keyboard.type('v') + + +def main(): + """ + Entry point for the command line client. + """ + vixf = ViXF(os.environ) + vixf.exec() + + +if __name__ == '__main__': + main() |