Add a --type argument to hints kitten

Allows addition of more sophisticated types of matches later.
This commit is contained in:
Kovid Goyal 2018-04-12 10:30:32 +05:30
parent 3c7236557c
commit 6a090ac740
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 31 additions and 23 deletions

View File

@ -78,17 +78,19 @@ def render(lines, current_input):
return '\r\n'.join(ans) return '\r\n'.join(ans)
class URLHints(Handler): class Hints(Handler):
def __init__(self, lines, index_map): def __init__(self, lines, index_map, args):
self.lines, self.index_map = tuple(lines), index_map self.lines, self.index_map = tuple(lines), index_map
self.current_input = '' self.current_input = ''
self.current_text = None self.current_text = None
self.args = args
self.window_title = _('Choose URL') if args.type == 'url' else _('Choose text')
self.chosen = None self.chosen = None
def init_terminal_state(self): def init_terminal_state(self):
self.write(set_cursor_visible(False)) self.write(set_cursor_visible(False))
self.write(set_window_title(_('Choose URL'))) self.write(set_window_title(self.window_title))
def initialize(self, *args): def initialize(self, *args):
Handler.initialize(self, *args) Handler.initialize(self, *args)
@ -174,15 +176,15 @@ def mark(finditer, line, index_map):
def run_loop(args, lines, index_map): def run_loop(args, lines, index_map):
loop = Loop() loop = Loop()
handler = URLHints(lines, index_map) handler = Hints(lines, index_map, args)
loop.loop(handler) loop.loop(handler)
if handler.chosen and loop.return_code == 0: if handler.chosen and loop.return_code == 0:
return {'url': handler.chosen, 'program': args.program} return {'match': handler.chosen, 'program': args.program}
raise SystemExit(loop.return_code) raise SystemExit(loop.return_code)
def run(args, text): def run(args, text):
if args.regex is None: if args.type == 'url':
from .url_regex import url_delimiters from .url_regex import url_delimiters
url_pat = '(?:{})://[^{}]{{3,}}'.format( url_pat = '(?:{})://[^{}]{{3,}}'.format(
'|'.join(args.url_prefixes.split(',')), url_delimiters '|'.join(args.url_prefixes.split(',')), url_delimiters
@ -197,33 +199,37 @@ def run(args, text):
lines.append(marked) lines.append(marked)
if not index_map: if not index_map:
input(_('No {} found, press Enter to abort.').format( input(_('No {} found, press Enter to abort.').format(
'URLs' if args.regex is None else 'matches' 'URLs' if args.type == 'url' else 'matches'
)) ))
return return
return run_loop(args, lines, index_map) return run_loop(args, lines, index_map)
OPTIONS = partial('''\ OPTIONS = partial(r'''
--program --program
default=default default=default
What program to use to open matched text. Defaults What program to use to open matched text. Defaults to the default open program
to the default open program for the operating system. for the operating system. Use a value of - to paste the match into the
Use a value of - to paste the match into the terminal window terminal window instead. A value of @ will copy the match to the clipboard.
instead. A value of @ will copy the match to the clipboard.
--type
default=url
choices=url,regex
The type of text to search for.
--regex --regex
Instead of searching for URLs search for the specified regular default=(?m)^\s*(.+)\s*$
expression instead. If you specify a group in the regular expression The regular expression to use when --type=regex. If you specify a group in the
only the group will be matched. This allow you to match text ignoring a regular expression only the group will be matched. This allow you to match text
prefix/suffix, as needed. ignoring a prefix/suffix, as needed. The default expression matches lines.
--url-prefixes --url-prefixes
default={0} default={0}
Comma separated list of recognized URL prefixes. Defaults to: Comma separated list of recognized URL prefixes.
{0}
'''.format, ','.join(sorted(URL_PREFIXES))) '''.format, ','.join(sorted(URL_PREFIXES)))
@ -257,11 +263,11 @@ def handle_result(args, data, target_window_id, boss):
if program == '-': if program == '-':
w = boss.window_id_map.get(target_window_id) w = boss.window_id_map.get(target_window_id)
if w is not None: if w is not None:
w.paste(data['url']) w.paste(data['match'])
elif program == '@': elif program == '@':
set_clipboard_string(data['url']) set_clipboard_string(data['match'])
else: else:
boss.open_url(data['url'], None if program == 'default' else program) boss.open_url(data['match'], None if program == 'default' else program)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -436,8 +436,10 @@ map ctrl+shift+f2 edit_config_file
# URL is specified in open_url_with. You can customize how the URLs are # URL is specified in open_url_with. You can customize how the URLs are
# detected and opened by specifying command line options to hints. The # detected and opened by specifying command line options to hints. The
# special value of - for --program will cause the selected URL to be inserted # special value of - for --program will cause the selected URL to be inserted
# into the terminal. For example: # into the terminal. For example, to select words and insert them into the terminal:
# map ctrl+shift+e run_kitten text hints --program firefox --regex "http://[^ ]+" # map ctrl+shift+e run_kitten text hints --program - --type regex --regex \w+
#
# Use kitty +kitten hints --help to see the full help for the hints kitten.
map ctrl+shift+e run_kitten text hints map ctrl+shift+e run_kitten text hints
# Open the kitty shell in a new window/tab/overlay/os_window to control kitty using commands. # Open the kitty shell in a new window/tab/overlay/os_window to control kitty using commands.
map ctrl+shift+escape kitty_shell window map ctrl+shift+escape kitty_shell window