From 90bcbbf426197a3a14a3bf41cd2a4c017696023f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 28 Nov 2019 10:11:14 +0530 Subject: [PATCH] No need to add index to titles when using hits kitten to choose tab/os_window --- kittens/hints/main.py | 4 ++++ kitty/boss.py | 23 +++++++++++++++-------- kitty/choose_entry.py | 12 ++++++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 kitty/choose_entry.py diff --git a/kittens/hints/main.py b/kittens/hints/main.py index 9ffc819d7..edd967036 100644 --- a/kittens/hints/main.py +++ b/kittens/hints/main.py @@ -324,6 +324,10 @@ def parse_input(text): def load_custom_processor(customize_processing): + if customize_processing.startswith('::import::'): + import importlib + m = importlib.import_module(customize_processing[len('::import::'):]) + return {k: getattr(m, k) for k in dir(m)} from kitty.constants import config_dir customize_processing = os.path.expandvars(os.path.expanduser(customize_processing)) if os.path.isabs(customize_processing): diff --git a/kitty/boss.py b/kitty/boss.py index c8cc20a57..0ec655d9f 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -1191,7 +1191,7 @@ class Boss: 'Choose a tab to move the window to', '' ] - fmt = '{} {}' + fmt = ': {1}' tab_id_map = {} current_tab = self.active_tab for i, tab in enumerate(self.all_tabs): @@ -1206,7 +1206,7 @@ class Boss: lines.append(fmt.format(new_idx, 'New OS Window')) def done(data, target_window_id, self): - done.tab_id = tab_id_map[int(data['match'][0].strip().partition(' ')[0])] + done.tab_id = tab_id_map[int(data['groupdicts'][0]['index']) + 1] def done2(target_window_id, self): if not hasattr(done, 'tab_id'): @@ -1223,8 +1223,11 @@ class Boss: self._move_window_to(window=target_window, target_tab_id=tab_id) self._run_kitten( - 'hints', args=('--ascending', '--type=regex', r'--regex=(?m)^\s*\d+ .+$',), - input_data='\r\n'.join(lines).encode('utf-8'), custom_callback=done, action_on_removal=done2) + 'hints', args=( + '--ascending', '--customize-processing=::import::kitty.choose_entry', + r'--regex=(?m)^:\s+.+$', + ), input_data='\r\n'.join(lines).encode('utf-8'), custom_callback=done, action_on_removal=done2 + ) def detach_tab(self, *args): if not args or args[0] == 'new': @@ -1234,16 +1237,17 @@ class Boss: 'Choose an OS window to move the tab to', '' ] + fmt = ': {1}' os_window_id_map = {} current_os_window = getattr(self.active_tab, 'os_window_id', 0) for i, osw in enumerate(self.os_window_map): tm = self.os_window_map[osw] if current_os_window != osw and tm.active_tab and tm.active_tab: os_window_id_map[i + 1] = osw - lines.append('{} {}'.format(i + 1, tm.active_tab.title)) + lines.append(fmt.format(i + 1, tm.active_tab.title)) new_idx = len(os_window_id_map) + 1 os_window_id_map[new_idx] = None - lines.append('{} {}'.format(new_idx, 'New OS Window')) + lines.append(fmt.format(new_idx, 'New OS Window')) def done(data, target_window_id, self): done.os_window_id = os_window_id_map[int(data['match'][0].partition(' ')[0])] @@ -1262,5 +1266,8 @@ class Boss: self._move_tab_to(tab=target_tab, target_os_window_id=os_window_id) self._run_kitten( - 'hints', args=('--ascending', '--type=regex', r'--regex=(?m)^\d+ .+$',), - input_data='\r\n'.join(lines).encode('utf-8'), custom_callback=done, action_on_removal=done2) + 'hints', args=( + '--ascending', '--customize-processing=::import::kitty.choose_entry', + r'--regex=(?m)^:\s+.+$', + ), input_data='\r\n'.join(lines).encode('utf-8'), custom_callback=done, action_on_removal=done2 + ) diff --git a/kitty/choose_entry.py b/kitty/choose_entry.py new file mode 100644 index 000000000..8115f0e1f --- /dev/null +++ b/kitty/choose_entry.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +# License: GPLv3 Copyright: 2019, Kovid Goyal + +import re + + +def mark(text, args, Mark, extra_cli_args, *a): + for idx, m in enumerate(re.finditer(args.regex, text)): + start, end = m.span() + mark_text = text[start:end].replace('\n', '').replace('\0', '') + yield Mark(idx, start, end, mark_text, {'index': idx})