URL hints kitten: Open the URL from within the kitty process. This ensures that the behavior of the program used to open the URL is the same as when ctrl+shift clicking URLs. Fixes #424

This commit is contained in:
Kovid Goyal 2018-04-01 16:48:07 +05:30
parent ce0db16479
commit 90a4b4f859
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
2 changed files with 29 additions and 9 deletions

View file

@ -176,12 +176,16 @@ def run_loop(args, lines, index_map):
handler = URLHints(lines, index_map)
loop.loop(handler)
if handler.chosen and loop.return_code == 0:
cmd = command_for_open(args.program)
ret = subprocess.Popen(cmd + [handler.chosen]).wait()
if ret != 0:
print('URL handler "{}" failed with return code: {}'.format(' '.join(cmd), ret), file=sys.stderr)
input('Press Enter to quit')
loop.return_code = ret
if args.in_kitty:
import json
print('OK:', json.dumps({'url': handler.chosen, 'program': args.program, 'action': 'open_with'}))
else:
cmd = command_for_open(args.program)
ret = subprocess.Popen(cmd + [handler.chosen]).wait()
if ret != 0:
print('URL handler "{}" failed with return code: {}'.format(' '.join(cmd), ret), file=sys.stderr)
input('Press Enter to quit')
loop.return_code = ret
raise SystemExit(loop.return_code)
@ -233,6 +237,12 @@ def run(args, source_file=None):
default={0}
Comma separated list of recognized URL prefixes. Defaults to:
{0}
--in-kitty
type=bool-set
Output the URL instead of opening it. Intended for use from within
kitty.
'''.format, ','.join(sorted(URL_PREFIXES)))

View file

@ -471,6 +471,8 @@ def input_unicode_character(self):
def get_output(self, source_window, num_lines=1):
output = ''
s = source_window.screen
if num_lines is None:
num_lines = s.lines
for i in range(min(num_lines, s.lines)):
output += str(s.linebuf.line(i))
return output
@ -517,19 +519,27 @@ def run_simple_kitten(self, type_of_input, kitten, *args):
if w is not None and tab is not None and w.overlay_for is None:
cmdline = args[0] if args else ''
args = shlex.split(cmdline) if cmdline else []
if '--program' not in cmdline:
args.extend(('--program', self.opts.open_url_with))
if kitten == 'url_hints':
args[0:0] = ['--in-kitty', '--program', self.opts.open_url_with]
if type_of_input in ('text', 'history', 'ansi', 'ansi-history'):
data = w.as_text(as_ansi='ansi' in type_of_input, add_history='history' in type_of_input).encode('utf-8')
elif type_of_input == 'none':
data = None
else:
raise ValueError('Unknown type_of_input: {}'.format(type_of_input))
tab.new_special_window(
overlay_window = tab.new_special_window(
SpecialWindow(
['kitty', '+runpy', 'from kittens.{}.main import main; main()'.format(kitten)] + args,
stdin=data,
overlay_for=w.id))
if kitten == 'url_hints':
overlay_window.action_on_close = self.open_hinted_url
def open_hinted_url(self, source_window):
output = self.get_output(source_window, num_lines=None)
if output.startswith('OK: '):
cmd = json.loads(output.partition(' ')[2].strip())
open_url(cmd['url'], cmd['program'])
def switch_focus_to(self, window_idx):
tab = self.active_tab