Skip to content

Instantly share code, notes, and snippets.

@madprops
Last active September 21, 2024 19:07
Show Gist options
  • Save madprops/db01a7e714e5305b8b46443cd665ddd6 to your computer and use it in GitHub Desktop.
Save madprops/db01a7e714e5305b8b46443cd665ddd6 to your computer and use it in GitHub Desktop.
command palette for terminals
import time
from subprocess import Popen, PIPE
from pathlib import Path
info = "Pick a command"
items = Path("~/.config/awesome/scripts/data/commands.data").expanduser().read_text().split("\n")
selected = 0
rofi_cmd = f"""
rofi -dmenu -i -format i
-inputchange-action 'kb-row-first'
keys -kb-accept-alt ''
-p '{info}'
-selected-row {selected}
"""
rofi_cmd = " ".join(rofi_cmd.strip().split())
proc = Popen(
rofi_cmd,
stdout=PIPE,
stdin=PIPE,
shell=True,
text=True,
)
s_index, stderr = proc.communicate("\n".join(items))
code = proc.returncode
if code == 1:
exit(0)
index = int(s_index)
ans = items[index].strip()
if ans == "":
exit(0)
time.sleep(0.2)
Popen(f"xdotool type '{ans}'", shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment