From 5a5982b05a4d1149b3974dbcf9152e7eca359d41 Mon Sep 17 00:00:00 2001 From: kang Date: Sat, 8 Feb 2025 13:28:43 +0800 Subject: [PATCH] feat(kitty): Add JIRA hints to kitty terminal The use of load_env in jira-hints is because environment variables cannot be accessed in the kitty script. --- .env.example | 1 + kitty/.config/kitty/jira-hints.py | 30 ++++++++++++++++++++++++++++++ kitty/.config/kitty/kitty.conf | 2 ++ 3 files changed, 33 insertions(+) create mode 100644 kitty/.config/kitty/jira-hints.py diff --git a/.env.example b/.env.example index 234e2d9..4392516 100755 --- a/.env.example +++ b/.env.example @@ -1,5 +1,6 @@ export WORK=false export WORK_JIRA_URL=https://jira.example.com export JIRA_URL=$WORK_JIRA_URL +export JIRA_KEYS=PROJ|BUGS export HOMEBREW_GITHUB_API_TOKEN= export VULTR_API_KEY= diff --git a/kitty/.config/kitty/jira-hints.py b/kitty/.config/kitty/jira-hints.py new file mode 100644 index 0000000..ae756ec --- /dev/null +++ b/kitty/.config/kitty/jira-hints.py @@ -0,0 +1,30 @@ +import re +import os + + +def load_env(env_path): + with open(os.path.expanduser(env_path)) as f: + for line in f: + if line.strip() and not line.startswith('#'): + line = line.replace('export ', '').strip() + if '=' in line: + key, value = line.split('=', 1) + os.environ[key.strip()] = value.strip().strip('"\'') + +load_env('~/.dotfiles/.env') +JIRA_URL = os.environ.get('WORK_JIRA_URL') +JIRA_KEYS = os.environ.get('JIRA_KEYS') + +def mark(text, args, Mark, extra_cli_args, *a): + for idx, m in enumerate(re.finditer(f'(?:{JIRA_KEYS})-[0-9]+', text)): + start, end = m.span() + mark_text = text[start:end].replace('\n', '').replace('\0', '') + yield Mark(idx, start, end, mark_text, {}) + +def handle_result(args, data, target_window_id, boss, extra_cli_args, *a): + matches, groupdicts = [], [] + for m, g in zip(data['match'], data['groupdicts']): + if m: + matches.append(m), groupdicts.append(g) + for ticket in matches: + boss.open_url(f'{JIRA_URL}/browse/{ticket}') diff --git a/kitty/.config/kitty/kitty.conf b/kitty/.config/kitty/kitty.conf index 18081c6..7b69111 100644 --- a/kitty/.config/kitty/kitty.conf +++ b/kitty/.config/kitty/kitty.conf @@ -65,3 +65,5 @@ map ctrl+shift+r no_op map cmd+w no_op cursor_trail 3 + +map ctrl+shift+p>j kitten hints --customize-processing jira-hints.py