Add a new interactive action to set the active window title

This commit is contained in:
Kovid Goyal 2023-11-02 08:04:42 +05:30
parent a04d19df4a
commit 827a7d5094
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 41 additions and 7 deletions

View file

@ -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]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -1053,7 +1053,8 @@ def get_line(
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 @@ def on_popup_overlay_removal(wid: int, boss: Boss) -> None:
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 @@ def set_tab_title(self, title: Optional[str] = None) -> None:
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)

View file

@ -874,9 +874,39 @@ def set_title(self, title: Optional[str]) -> None:
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