From 827a7d50946d3196b32d99f9ad283d6a3b813e2d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 2 Nov 2023 08:04:42 +0530 Subject: [PATCH] Add a new interactive action to set the active window title --- docs/changelog.rst | 2 ++ kitty/boss.py | 14 ++++++++------ kitty/window.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 41ddd96fa..4c3a528de 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -88,6 +88,8 @@ Detailed list of changes - Shell integration: Fix ``sudo --edit`` not working and also fix completions for sudo not working in zsh (:iss:`6754`, :iss:`6771`) +- A new action :ac:`set_window_title` to interactively change the title of the active window + 0.30.1 [2023-10-05] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/boss.py b/kitty/boss.py index a5d7a14d1..53524569c 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -1053,7 +1053,8 @@ class Boss: callback: Callable[..., None], # called with the answer or empty string when aborted window: Optional[Window] = None, # the window associated with the confirmation prompt: str = '> ', - is_password: bool = False + is_password: bool = False, + initial_value: str = '' ) -> None: result: str = '' @@ -1065,6 +1066,8 @@ class Boss: callback(result) cmd = ['--type', 'password' if is_password else 'line', '--message', msg, '--prompt', prompt] + if initial_value: + cmd.append('--default=' + initial_value) self.run_kitten_with_metadata( 'ask', cmd, window=window, custom_callback=callback_, default_data={'response': ''}, action_on_removal=on_popup_overlay_removal ) @@ -1942,12 +1945,11 @@ class Boss: prefilled = tab.name or tab.title if title in ('" "', "' '"): prefilled = '' - args = [ - '--name=tab-title', '--message', _('Enter the new title for this tab below.'), - '--default', prefilled, 'do_set_tab_title', str(tab.id)] - self.run_kitten_with_metadata('ask', args) + self.get_line( + _('Enter the new title for this tab below. An empty title will cause the default title to be used.'), + partial(self.do_set_tab_title, tab.id), window=tab.active_window, initial_value=prefilled) - def do_set_tab_title(self, title: str, tab_id: int) -> None: + def do_set_tab_title(self, tab_id: int, title: str) -> None: tm = self.active_tab_manager if tm is not None: tab_id = int(tab_id) diff --git a/kitty/window.py b/kitty/window.py index e826cb499..1029f8ead 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -874,9 +874,39 @@ class Window: if title: title = sanitize_title(title) self.override_title = title or None - self.call_watchers(self.watchers.on_title_change, {'title': self.child_title, 'from_child': False}) + self.call_watchers(self.watchers.on_title_change, {'title': self.title, 'from_child': False}) self.title_updated() + @ac( + 'win', ''' + Change the title of the active window interactively, by typing in the new title. + If you specify an argument to this action then that is used as the title instead of asking for it. + Use the empty string ("") to reset the title to default. Use a space (" ") to indicate that the + prompt should not be pre-filled. For example:: + + # interactive usage + map f1 set_window_title + # set a specific title + map f2 set_window_title some title + # reset to default + map f3 set_window_title "" + # interactive usage without prefilled prompt + map f3 set_window_title " " + ''' + ) + def set_window_title(self, title: Optional[str] = None) -> None: + if title is not None and title not in ('" "', "' '"): + if title in ('""', "''"): + title = '' + self.set_title(title) + return + prefilled = self.title + if title in ('" "', "' '"): + prefilled = '' + get_boss().get_line( + _('Enter the new title for this window below. An empty title will cause the default title to be used.'), + self.set_title, window=self, initial_value=prefilled) + def set_user_var(self, key: str, val: Optional[Union[str, bytes]]) -> None: key = sanitize_control_codes(key).replace('\n', ' ') self.user_vars.pop(key, None) # ensure key will be newest in user_vars even if already present