diff --git a/.config/i3/config b/.config/i3/config index 5fd5aaf..6ab8e24 100644 --- a/.config/i3/config +++ b/.config/i3/config @@ -71,7 +71,7 @@ bindsym $mod+Shift+t exec --no-startup-id "rofi -show mworkspace" bindsym $mod+n exec $config_dir/rename_workspace.py bindsym $mod+c exec --no-startup-id rofi-pass --last-used -bindsym $mod+Shift+c exec --no-startup-id autopass +bindsym $mod+Shift+c exec --no-startup-id "$config_dir/pass_typer.py" # Brighness Control bindsym XF86MonBrightnessUp exec light -A 10 diff --git a/.config/i3/pass_typer.py b/.config/i3/pass_typer.py new file mode 100755 index 0000000..c27f63a --- /dev/null +++ b/.config/i3/pass_typer.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +import os +import re +import subprocess +from functools import partial +from pathlib import PosixPath + +PASSWORD_STORE = PosixPath( + os.environ.get("PASSWORD_STORE_DIR", PosixPath("~/.password-store").expanduser()) +) + +ALIASES = { + "salt": "cms-net-svcs", +} + +cmd = partial(subprocess.run, capture_output=True, encoding="ascii") + + +def notify(summary: str, body: str) -> None: + subprocess.run(["notify-send", summary, body]) + + +def rofi_select(options: list[str]) -> str: + options_str = "\n".join(options) + rofi = cmd(["rofi", "-dmenu"], input=options_str) + return rofi.stdout.strip() + + +def get_password(password_name: str) -> None: + pass_result = cmd(["pass", password_name]) + password, _, _ = pass_result.stdout.partition("\n") + return password + + +def select_and_type(glob: str) -> None: + files = PASSWORD_STORE.glob(glob) + file_list = [str(f.relative_to(PASSWORD_STORE).with_suffix("")) for f in files] + + selected = rofi_select(file_list) + password = get_password(selected) + + subprocess.run(["xdotool", "type", password + '\n']) + + +window_name = cmd(["xdotool", "getactivewindow", "getwindowname"]).stdout.strip() + +ssh_match = re.search(":(mosh|ssh) (?P.*)", window_name) +if ssh_match: + raw_server_name = ssh_match.group("server") + server_name = ALIASES.get(raw_server_name, raw_server_name) + + notify(f"Matched server '{server_name}'", f"Window name: {window_name}") + + select_and_type(f"servers/**/{server_name}.gpg") + +else: + notify("Window name did not match any rules", f"Window name: {window_name}")