aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2024-11-17 20:01:33 +1300
committerTom Ryder <tom@sanctum.geek.nz>2024-11-17 20:01:33 +1300
commit31dc4ce9a2a2e0d1217b94d7a89bc339e3e9df4a (patch)
tree84e5af97c73341e0ff7660a3f6016bfe929f75ff
parentRefactor into module with class (diff)
downloadvixf-31dc4ce9a2a2e0d1217b94d7a89bc339e3e9df4a.tar.gz
vixf-31dc4ce9a2a2e0d1217b94d7a89bc339e3e9df4a.zip
Refactor into just a main() function
-rw-r--r--vixf.py3148
1 files changed, 64 insertions, 84 deletions
diff --git a/vixf.py3 b/vixf.py3
index aca8294..18ae078 100644
--- a/vixf.py3
+++ b/vixf.py3
@@ -17,94 +17,74 @@ import time
from pynput.keyboard import Key, Controller
import pyperclip
+SELF = 'vixf'
-class ViXF():
- """
- Using a class, to encapsulate a fair bit of system state that gets injected
- into this.
- """
+EDITOR = 'vi'
+FOCUS_DELAY = 0.2
+TERM = 'x-terminal-emulator'
- 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():
+
+def main(environ):
"""
- Entry point for the command line client.
+ Just bundle everything into a main function.
"""
- vixf = ViXF(os.environ)
- vixf.exec()
+
+ time.sleep(FOCUS_DELAY)
+
+ keyboard = Controller()
+
+ # 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()
+
+ if 'VISUAL' in environ:
+ editor = environ['VISUAL']
+ elif 'EDITOR' in environ:
+ editor = environ['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')
if __name__ == '__main__':
- main()
+ main(os.environ)