From b64bceac7f52051cca945892619945df2d944f87 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 6 Apr 2018 14:59:13 +0530 Subject: [PATCH] Add a keyboard shortcut to open the kitty shell --- README.asciidoc | 4 +++- kitty/boss.py | 30 +++++++++++++++++++++++++----- kitty/config.py | 2 +- kitty/kitty.conf | 2 ++ kitty/shell.py | 2 ++ kitty/tabs.py | 1 + 6 files changed, 34 insertions(+), 7 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index d3014bcc3..caac91e93 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -13,6 +13,7 @@ :sc_fourth_window: pass:quotes[`ctrl+shift+4`] :sc_increase_font_size: pass:quotes[`ctrl+shift+equal`] :sc_input_unicode_character: pass:quotes[`ctrl+shift+u`] +:sc_kitty_shell_window: pass:quotes[`ctrl+shift+escape`] :sc_move_tab_backward: pass:quotes[`ctrl+shift+,`] :sc_move_tab_forward: pass:quotes[`ctrl+shift+.`] :sc_move_window_backward: pass:quotes[`ctrl+shift+b`] @@ -239,7 +240,8 @@ windows are: |Input unicode character | {sc_input_unicode_character} |Click URL using the keyboard | {sc_run_simple_kitten_text_url_hints} |Pass current selection to program | {sc_pass_selection_to_program} -|Edit kitty config file| {sc_edit_config_file} +|Edit kitty config file | {sc_edit_config_file} +|Open a kitty shell | {sc_kitty_shell} |=== diff --git a/kitty/boss.py b/kitty/boss.py index 0bbf4807d..70252af8a 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -120,6 +120,7 @@ def add_os_window(self, startup_session, os_window_id=None, wclass=None, wname=N self.os_window_map[os_window_id] = tm if dpi_changed: self.on_dpi_change(os_window_id) + return os_window_id def list_os_windows(self): for os_window_id, tm in self.os_window_map.items(): @@ -189,7 +190,7 @@ def set_active_window(self, window): def _new_os_window(self, args, cwd_from=None): sw = self.args_to_special_window(args, cwd_from) if args else None startup_session = create_session(self.opts, special_window=sw, cwd_from=cwd_from) - self.add_os_window(startup_session) + return self.add_os_window(startup_session) def new_os_window(self, *args): self._new_os_window(args) @@ -205,7 +206,7 @@ def add_child(self, window): def _handle_remote_command(self, cmd, window=None): response = None - if self.opts.allow_remote_control: + if self.opts.allow_remote_control or getattr(window, 'allow_remote_control', False): try: response = handle_cmd(self, window, cmd) except Exception as err: @@ -552,6 +553,25 @@ def open_hinted_url(self, source_window): cmd = json.loads(output.partition(' ')[2].strip()) open_url(cmd['url'], cmd['program']) + def kitty_shell(self, window_type): + cmd = ['kitty', '@'] + if window_type == 'tab': + window = self._new_tab(cmd).active_window + elif window_type == 'os_window': + os_window_id = self._new_os_window(cmd) + window = self.os_window_map[os_window_id].active_window + elif window_type == 'overlay': + w = self.active_window + tab = self.active_tab + if w is not None and tab is not None and w.overlay_for is None: + window = tab.new_special_window(SpecialWindow(cmd, overlay_for=w.id)) + else: + window = None + else: + window = self._new_window(cmd) + if window is not None: + window.allow_remote_control = True + def switch_focus_to(self, window_idx): tab = self.active_tab tab.set_active_window_idx(window_idx) @@ -661,7 +681,7 @@ def _new_tab(self, args, cwd_from=None): special_window = self.args_to_special_window(args, cwd_from=cwd_from) tm = self.active_tab_manager if tm is not None: - tm.new_tab(special_window=special_window, cwd_from=cwd_from) + return tm.new_tab(special_window=special_window, cwd_from=cwd_from) def new_tab(self, *args): self._new_tab(args) @@ -675,9 +695,9 @@ def _new_window(self, args, cwd_from=None): tab = self.active_tab if tab is not None: if args: - tab.new_special_window(self.args_to_special_window(args, cwd_from=cwd_from)) + return tab.new_special_window(self.args_to_special_window(args, cwd_from=cwd_from)) else: - tab.new_window(cwd_from=cwd_from) + return tab.new_window(cwd_from=cwd_from) def new_window(self, *args): self._new_window(args) diff --git a/kitty/config.py b/kitty/config.py index f35fb76f5..e327035a1 100644 --- a/kitty/config.py +++ b/kitty/config.py @@ -112,7 +112,7 @@ def parse_key_action(action): args = rest.split(' ', 2) elif func == 'goto_tab': args = (max(0, int(rest)), ) - elif func == 'goto_layout': + elif func == 'goto_layout' or func == 'kitty_shell': args = [rest] elif func == 'set_font_size': args = (float(rest),) diff --git a/kitty/kitty.conf b/kitty/kitty.conf index c9349b22f..733833979 100644 --- a/kitty/kitty.conf +++ b/kitty/kitty.conf @@ -437,6 +437,8 @@ map ctrl+shift+f2 edit_config_file # url_hints. For example: # map ctrl+shift+e run_simple_kitten text url_hints --program firefox --regex "http://[^ ]+" map ctrl+shift+e run_simple_kitten text url_hints +# Open the kitty shell in a new window/tab/overlay/os_window to control kitty using commands. +map ctrl+shift+escape kitty_shell window # Sending arbitrary text on shortcut key presses # You can tell kitty to send arbitrary (UTF-8) encoded text to diff --git a/kitty/shell.py b/kitty/shell.py index 64081825a..4d88bcdbd 100644 --- a/kitty/shell.py +++ b/kitty/shell.py @@ -143,6 +143,8 @@ def run_cmd(global_opts, cmd, func, opts, items): def real_main(global_opts): readline.read_init_file() print_help_for_seq.allow_pager = False + print('Welcome to the kitty shell!') + print('Use {} for assistance or {} to quit'.format(green('help'), green('exit'))) while True: try: diff --git a/kitty/tabs.py b/kitty/tabs.py index 5ac76ff77..6a3e3a378 100644 --- a/kitty/tabs.py +++ b/kitty/tabs.py @@ -467,6 +467,7 @@ def new_tab(self, special_window=None, cwd_from=None): self._add_tab(Tab(self, special_window=special_window, cwd_from=cwd_from)) self._set_active_tab(idx) self.update_tab_bar() + return self.tabs[idx] def remove(self, tab): self._remove_tab(tab)