Skip to content

Instantly share code, notes, and snippets.

@kuremu
Last active May 5, 2024 04:43
Show Gist options
  • Save kuremu/4d1441828ebab9d2da67f1298102fc3f to your computer and use it in GitHub Desktop.
Save kuremu/4d1441828ebab9d2da67f1298102fc3f to your computer and use it in GitHub Desktop.
Demo of header-click in fzf
import sys
from os.path import realpath
from subprocess import Popen, PIPE
FZFBIN = 'fzf'
THIS = f"{sys.executable} {realpath(__file__)}"
keyboard = """
` 1 2 3 4 5 6 7 8 9 0 - = BCK
TB q w e r t y u i o p [ ] \\
CAP a s d f g h j k l ; ' ENT
SHFT z x c v b n m , . / SHFT
C M {______________} M C <,^>
""".strip()
fzf = ' '.join([
FZFBIN,
'--reverse',
'--header-lines=5',
f'--bind "click-header:transform({THIS} \\$FZF_CLICK_HEADER_LINE \\$FZF_CLICK_HEADER_COLUMN)"',
])
if len(sys.argv) == 1:
input = '\n'.join([ l.strip() for l in sys.stdin.readlines() ])
input = keyboard + '\n' + input
p = Popen(fzf, shell=True, stdout=PIPE, stdin=PIPE)
out = p.communicate(input.encode())[0]
print(out.strip().decode())
elif len(sys.argv) == 3:
y, x = sys.argv[1:3]
try:
key = keyboard.split('\n')[int(y)-1][int(x)-1]
if key in "BCK":
print("backward-delete-char")
elif key in "ENT":
print("accept-or-print-query")
elif key in "<,^>":
print({
"<": "backward-char", "^": "up",
">": "forward-char", ",": "down",
}.get(key))
elif not key.isupper() and key != " ":
if key in "{_}":
key = " "
print(f"put({key})")
except Exception as e:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment