Allow asking for confirmation before opening hyperlinks

This commit is contained in:
Kovid Goyal
2020-09-15 10:26:05 +05:30
parent e21a8e3cc2
commit 0a027fad9a
2 changed files with 26 additions and 3 deletions

View File

@@ -1145,10 +1145,17 @@ program, even one running on a remote server via SSH can read your clipboard.
''')) '''))
o('allow_hyperlinks', True, long_text=_(''' def allow_hyperlinks(x: str) -> int:
if x == 'ask':
return 0b11
return 1 if to_bool(x) else 0
o('allow_hyperlinks', 'yes', option_type=allow_hyperlinks, long_text=_('''
Process hyperlink (OSC 8) escape sequences. If disabled OSC 8 escape Process hyperlink (OSC 8) escape sequences. If disabled OSC 8 escape
sequences are ignored. Otherwise they become clickable links, that you sequences are ignored. Otherwise they become clickable links, that you
can click by holding down ctrl+shift and clicking with the mouse''')) can click by holding down ctrl+shift and clicking with the mouse. The special
value of ``ask`` means that kitty will ask before opening the link.'''))
o('term', 'xterm-kitty', long_text=_(''' o('term', 'xterm-kitty', long_text=_('''

View File

@@ -8,6 +8,8 @@ import sys
import weakref import weakref
from collections import deque from collections import deque
from enum import IntEnum from enum import IntEnum
from functools import partial
from gettext import gettext as _
from itertools import chain from itertools import chain
from typing import ( from typing import (
Any, Callable, Deque, Dict, Iterable, List, Optional, Pattern, Sequence, Any, Callable, Deque, Dict, Iterable, List, Optional, Pattern, Sequence,
@@ -491,6 +493,8 @@ class Window:
def open_url(self, url: str, hyperlink_id: int) -> None: def open_url(self, url: str, hyperlink_id: int) -> None:
if hyperlink_id: if hyperlink_id:
if not self.opts.allow_hyperlinks:
return
from urllib.parse import unquote, urlparse from urllib.parse import unquote, urlparse
try: try:
purl = urlparse(url) purl = urlparse(url)
@@ -507,9 +511,21 @@ class Window:
if remote_hostname and remote_hostname != hostname: if remote_hostname and remote_hostname != hostname:
self.handle_remote_file(purl.netloc, unquote(purl.path)) self.handle_remote_file(purl.netloc, unquote(purl.path))
return return
if self.opts.allow_hyperlinks & 0b10:
from kittens.tui.operations import styled
get_boss()._run_kitten('ask', ['--type=yesno', '--message', _(
'Do you want to open the following URL:\n') +
styled(unquote(url), fg='yellow')],
window=self,
custom_callback=partial(self.hyperlink_open_confirmed, url)
)
return
get_boss().open_url(url) get_boss().open_url(url)
def hyperlink_open_confirmed(self, url: str, data: Dict[str, Any], *a: Any) -> None:
if data['response'] == 'y':
get_boss().open_url(url)
def handle_remote_file(self, netloc: str, remote_path: str) -> None: def handle_remote_file(self, netloc: str, remote_path: str) -> None:
from kittens.ssh.main import get_connection_data from kittens.ssh.main import get_connection_data
args = self.child.foreground_cmdline args = self.child.foreground_cmdline