aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2024-11-17 17:12:27 +1300
committerTom Ryder <tom@sanctum.geek.nz>2024-11-17 17:22:56 +1300
commite868ab35739fec132097b4e811a504ccf2c0ba50 (patch)
treedc4144e6638bfc5be5c84c74b38e9b90ce58ccff
parentRefactor keyboard automation (diff)
downloadvixf-e868ab35739fec132097b4e811a504ccf2c0ba50.tar.gz
vixf-e868ab35739fec132097b4e811a504ccf2c0ba50.zip
Bail out on empty copy
-rw-r--r--vixf.py317
1 files changed, 14 insertions, 3 deletions
diff --git a/vixf.py3 b/vixf.py3
index d9e791c..60ff7ae 100644
--- a/vixf.py3
+++ b/vixf.py3
@@ -10,6 +10,7 @@ text.
import locale
import os
import subprocess
+import sys
import tempfile
import time
@@ -34,9 +35,19 @@ with keyboard.pressed(Key.ctrl):
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(pyperclip.paste())
+ tf.write(content_before)
tf.close()
env = os.environ
@@ -59,9 +70,9 @@ with tempfile.NamedTemporaryFile(mode='w', delete_on_close=False) as tf:
encoding = locale.getpreferredencoding()
with open(tf.name, mode='r', encoding=encoding) as tfr:
- content = tfr.read()
+ content_after = tfr.read()
-pyperclip.copy(content)
+pyperclip.copy(content_after)
time.sleep(SLEEP)