mirror of
https://github.com/kovidgoyal/kitty.git
synced 2024-09-22 03:57:30 +03:00
DRYer
This commit is contained in:
parent
4af2526f54
commit
bc25e5a8a2
@ -674,6 +674,13 @@ def close_tab(self, tab: Optional[Tab] = None) -> None:
|
||||
if tab:
|
||||
self.confirm_tab_close(tab)
|
||||
|
||||
def confirm(self, msg: str, callback: Callable[..., None], *args: Any,
|
||||
window: Optional[Window] = None, confirm_on_cancel: bool = False) -> None:
|
||||
def callback_(res: Dict[str, Any], x: int, boss: Boss) -> None:
|
||||
callback(res.get('response') == 'y', *args)
|
||||
self._run_kitten('ask', ['--type=yesno', '--message', msg],
|
||||
window=window, custom_callback=callback_, default_data={'response': 'y' if confirm_on_cancel else 'n'})
|
||||
|
||||
def confirm_tab_close(self, tab: Tab) -> None:
|
||||
x = get_options().confirm_os_window_close
|
||||
num = tab.number_of_windows_with_running_programs if x < 0 else len(tab)
|
||||
@ -685,16 +692,15 @@ def confirm_tab_close(self, tab: Tab) -> None:
|
||||
tm = tab.tab_manager_ref()
|
||||
if tm is not None:
|
||||
tm.set_active_tab(tab)
|
||||
self._run_kitten('ask', ['--type=yesno', '--message', _(
|
||||
self.confirm(_(
|
||||
'Are you sure you want to close this tab, it has {}'
|
||||
' windows running?').format(num)],
|
||||
' windows running?').format(num),
|
||||
self.handle_close_tab_confirmation, tab.id,
|
||||
window=tab.active_window,
|
||||
custom_callback=partial(self.handle_close_tab_confirmation, tab.id),
|
||||
default_data={'response': 'n'}
|
||||
)
|
||||
|
||||
def handle_close_tab_confirmation(self, tab_id: int, data: Dict[str, Any], *a: Any) -> None:
|
||||
if data.get('response') != 'y':
|
||||
def handle_close_tab_confirmation(self, confirmed: bool, tab_id: int) -> None:
|
||||
if not confirmed:
|
||||
return
|
||||
for tab in self.all_tabs:
|
||||
if tab.id == tab_id:
|
||||
@ -1207,16 +1213,15 @@ def confirm_os_window_close(self, os_window_id: int) -> None:
|
||||
return
|
||||
if tm is not None:
|
||||
w = tm.active_window
|
||||
self._run_kitten('ask', ['--type=yesno', '--message', _(
|
||||
'Are you sure you want to close this OS window, it has {}'
|
||||
' windows running?').format(num)],
|
||||
self.confirm(
|
||||
_('Are you sure you want to close this OS window, it has {}'
|
||||
' windows running?').format(num),
|
||||
self.handle_close_os_window_confirmation, os_window_id,
|
||||
window=w,
|
||||
custom_callback=partial(self.handle_close_os_window_confirmation, os_window_id),
|
||||
default_data={'response': 'n'}
|
||||
)
|
||||
|
||||
def handle_close_os_window_confirmation(self, os_window_id: int, data: Dict[str, Any], *a: Any) -> None:
|
||||
if data.get('response') == 'y':
|
||||
def handle_close_os_window_confirmation(self, confirmed: bool, os_window_id: int) -> None:
|
||||
if confirmed:
|
||||
self.mark_os_window_for_close(os_window_id)
|
||||
else:
|
||||
self.mark_os_window_for_close(os_window_id, NO_CLOSE_REQUESTED)
|
||||
@ -1248,16 +1253,15 @@ def quit(self, *args: Any) -> None:
|
||||
if current_application_quit_request() == CLOSE_BEING_CONFIRMED:
|
||||
return
|
||||
assert tm is not None
|
||||
self._run_kitten('ask', ['--type=yesno', '--message', _(
|
||||
'Are you sure you want to quit kitty, it has {} windows running?').format(num)],
|
||||
self.confirm(
|
||||
_('Are you sure you want to quit kitty, it has {} windows running?').format(num),
|
||||
self.handle_quit_confirmation,
|
||||
window=tm.active_window,
|
||||
custom_callback=self.handle_quit_confirmation,
|
||||
default_data={'response': 'n'}
|
||||
)
|
||||
set_application_quit_request(CLOSE_BEING_CONFIRMED)
|
||||
|
||||
def handle_quit_confirmation(self, data: Dict[str, Any], *a: Any) -> None:
|
||||
set_application_quit_request(IMPERATIVE_CLOSE_REQUESTED if data.get('response') == 'y' else NO_CLOSE_REQUESTED)
|
||||
def handle_quit_confirmation(self, confirmed: bool) -> None:
|
||||
set_application_quit_request(IMPERATIVE_CLOSE_REQUESTED if confirmed else NO_CLOSE_REQUESTED)
|
||||
|
||||
def notify_on_os_window_death(self, address: str) -> None:
|
||||
import socket
|
||||
|
@ -1049,23 +1049,21 @@ def write_ftc_to_child(self, payload: FileTransmissionCommand, appendleft: bool
|
||||
def start_send(self, asd_id: str) -> None:
|
||||
asd = self.active_sends[asd_id]
|
||||
if asd.bypass_ok is not None:
|
||||
self.handle_receive_confirmation(asd_id, {'response': 'y' if asd.bypass_ok else 'n'})
|
||||
self.handle_receive_confirmation(asd.bypass_ok, asd_id)
|
||||
return
|
||||
boss = get_boss()
|
||||
window = boss.window_id_map.get(self.window_id)
|
||||
if window is not None:
|
||||
boss._run_kitten('ask', ['--type=yesno', '--message', _(
|
||||
'The remote machine wants to read some files from this computer. Do you want to allow the transfer?'
|
||||
)],
|
||||
window=window, custom_callback=partial(self.handle_receive_confirmation, asd_id),
|
||||
default_data={'response': 'n'}
|
||||
boss.confirm(_(
|
||||
'The remote machine wants to read some files from this computer. Do you want to allow the transfer?'),
|
||||
self.handle_receive_confirmation, asd_id, window=window,
|
||||
)
|
||||
|
||||
def handle_receive_confirmation(self, cmd_id: str, data: Dict[str, str], *a: Any) -> None:
|
||||
def handle_receive_confirmation(self, confirmed: bool, cmd_id: str) -> None:
|
||||
asd = self.active_sends.get(cmd_id)
|
||||
if asd is None:
|
||||
return
|
||||
if data.get('response') == 'y':
|
||||
if confirmed:
|
||||
asd.accepted = True
|
||||
else:
|
||||
self.drop_send(asd.id)
|
||||
@ -1081,23 +1079,21 @@ def handle_receive_confirmation(self, cmd_id: str, data: Dict[str, str], *a: Any
|
||||
def start_receive(self, ar_id: str) -> None:
|
||||
ar = self.active_receives[ar_id]
|
||||
if ar.bypass_ok is not None:
|
||||
self.handle_send_confirmation(ar_id, {'response': 'y' if ar.bypass_ok else 'n'})
|
||||
self.handle_send_confirmation(ar.bypass_ok, ar_id)
|
||||
return
|
||||
boss = get_boss()
|
||||
window = boss.window_id_map.get(self.window_id)
|
||||
if window is not None:
|
||||
boss._run_kitten('ask', ['--type=yesno', '--message', _(
|
||||
'The remote machine wants to send some files to this computer. Do you want to allow the transfer?'
|
||||
)],
|
||||
window=window, custom_callback=partial(self.handle_send_confirmation, ar_id),
|
||||
default_data={'response': 'n'}
|
||||
boss.confirm(_(
|
||||
'The remote machine wants to send some files to this computer. Do you want to allow the transfer?'),
|
||||
self.handle_send_confirmation, ar_id, window=window,
|
||||
)
|
||||
|
||||
def handle_send_confirmation(self, cmd_id: str, data: Dict[str, str], *a: Any) -> None:
|
||||
def handle_send_confirmation(self, confirmed: bool, cmd_id: str) -> None:
|
||||
ar = self.active_receives.get(cmd_id)
|
||||
if ar is None:
|
||||
return
|
||||
if data.get('response') == 'y':
|
||||
if confirmed:
|
||||
ar.accepted = True
|
||||
else:
|
||||
self.drop_receive(ar.id)
|
||||
@ -1130,10 +1126,10 @@ def write_ftc_to_child(self, payload: FileTransmissionCommand, appendleft: bool
|
||||
return True
|
||||
|
||||
def start_receive(self, aid: str) -> None:
|
||||
self.handle_send_confirmation(aid, {'response': 'y' if self.allow else 'n'})
|
||||
self.handle_send_confirmation(self.allow, aid)
|
||||
|
||||
def start_send(self, aid: str) -> None:
|
||||
self.handle_receive_confirmation(aid, {'response': 'y' if self.allow else 'n'})
|
||||
self.handle_receive_confirmation(self.allow, aid)
|
||||
|
||||
def callback_after(self, callback: Callable[[Optional[int]], None], timeout: float = 0) -> Optional[int]:
|
||||
callback(None)
|
||||
|
@ -909,19 +909,17 @@ def ask_to_read_clipboard(self, primary: bool = False) -> None:
|
||||
self.current_clipboard_read_ask = primary
|
||||
return
|
||||
self.current_clipboard_read_ask = primary
|
||||
get_boss()._run_kitten('ask', ['--type=yesno', '--message', _(
|
||||
get_boss().confirm(_(
|
||||
'A program running in this window wants to read from the system clipboard.'
|
||||
' Allow it do so, once?')],
|
||||
window=self,
|
||||
custom_callback=self.handle_clipboard_confirmation,
|
||||
default_data={'response': 'n'}
|
||||
' Allow it do so, once?'),
|
||||
self.handle_clipboard_confirmation, window=self,
|
||||
)
|
||||
|
||||
def handle_clipboard_confirmation(self, data: Dict[str, Any], *a: Any) -> None:
|
||||
def handle_clipboard_confirmation(self, confirmed: bool) -> None:
|
||||
try:
|
||||
loc = 'p' if self.current_clipboard_read_ask else 'c'
|
||||
response = ''
|
||||
if data.get('response') == 'y':
|
||||
if confirmed:
|
||||
response = get_primary_selection() if self.current_clipboard_read_ask else get_clipboard_string()
|
||||
self.send_osc52(loc, response)
|
||||
finally:
|
||||
|
Loading…
Reference in New Issue
Block a user