aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2024-11-17 16:38:34 +1300
committerTom Ryder <tom@sanctum.geek.nz>2024-11-17 16:38:34 +1300
commite1ba43af73c4a1dc46544e2f6b7273cba9a5192b (patch)
treed510d64b8d5af1ef54ff93fd5956f3b543851d0d
parentIgnore development venv (diff)
downloadvixf-e1ba43af73c4a1dc46544e2f6b7273cba9a5192b.tar.gz
vixf-e1ba43af73c4a1dc46544e2f6b7273cba9a5192b.zip
First Python version
-rw-r--r--requirements.txt2
-rw-r--r--vixf.bash40
-rw-r--r--vixf.py362
3 files changed, 64 insertions, 40 deletions
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..07e7fab
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,2 @@
+pynput
+pyperclip
diff --git a/vixf.bash b/vixf.bash
deleted file mode 100644
index fa49a51..0000000
--- a/vixf.bash
+++ /dev/null
@@ -1,40 +0,0 @@
-#!bash
-# We use Bash for a more reliable EXIT trap.
-
-# Name this script (and its xterm instance).
-self=vixf
-
-# We leave a little bit of time for window focus etc to switch around.
-refocus=0.5
-
-# Create a temporary directory with name in $tempdir, and handle POSIX-ish traps to
-# remove it when the script exits.
-tempdir=
-cleanup() {
- rm --force --recursive -- "$tempdir"
-}
-trap cleanup EXIT
-tempdir=$(mktemp --directory)
-
-# After a short delay for the window manager to finish re-focussing windows,
-# then select everything in the field and copy it.
-xdotool \
- sleep "$refocus" \
- key --clearmodifiers -- ctrl+a ctrl+c || exit
-
-# Write the clipboard contents to the temporary file.
-xsel --clipboard --output > "$tempdir"/vixf || exit
-
-# Fire up a terminal emulator with $VISUAL, or vi(1) if not set
-x-terminal-emulator \
- -name "$self" \
- -e "${VISUAL:-vi}" "$tempdir"/vixf || exit
-
-# Read the modified file into the clipboard
-xsel --clipboard --input < "$tempdir"/vixf || exit
-
-# After another short delay, paste the modified text over the original, which
-# should still be selected
-xdotool \
- sleep "$refocus" \
- key --clearmodifiers -- ctrl+v || exit
diff --git a/vixf.py3 b/vixf.py3
new file mode 100644
index 0000000..d7fa3cf
--- /dev/null
+++ b/vixf.py3
@@ -0,0 +1,62 @@
+#!python3
+
+import os
+import subprocess
+import tempfile
+import time
+
+import pynput
+import pyperclip
+
+SELF = 'vixf'
+
+SLEEP = 0.5
+
+EDITOR = 'vi'
+
+keyboard = pynput.keyboard.Controller()
+
+time.sleep(SLEEP)
+
+ctrl = pynput.keyboard.Key.ctrl
+
+keyboard.press(ctrl)
+keyboard.type('a')
+keyboard.release(ctrl)
+
+keyboard.press(ctrl)
+keyboard.type('c')
+keyboard.release(ctrl)
+
+with tempfile.NamedTemporaryFile(mode='w', delete_on_close=False) as tf:
+ tf.write(pyperclip.paste())
+ tf.close()
+
+ editor = EDITOR
+
+ env = os.environ
+ for key in ['VIXF_EDITOR', 'VISUAL', 'EDITOR']:
+ if key in env:
+ editor = env[key]
+ break
+
+ command = [
+ 'x-terminal-emulator',
+ '-name',
+ SELF,
+ '-e',
+ editor,
+ tf.name,
+ ]
+ subprocess.run(command)
+
+ with open(tf.name, mode='r') as tfr:
+ content = tfr.read()
+
+pyperclip.copy(content)
+
+time.sleep(SLEEP)
+
+keyboard.press(pynput.keyboard.Key.ctrl)
+keyboard.type('v')
+keyboard.release(pynput.keyboard.Key.ctrl)