Implement @set-window-title

This commit is contained in:
Kovid Goyal 2018-01-08 14:04:27 +05:30
parent d2fdb1c1eb
commit 3b551a1dee
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 42 additions and 7 deletions

View File

@ -85,6 +85,30 @@ def send_text(boss, window, payload):
window.write_to_child(parse_send_text_bytes(payload['text']))
@cmd(
'Set the window title',
'Set the title for the specified window(s). If you use the |_ --match| option'
' the title will be set for all matched windows. By default, only the window'
' in which the command is run is affected. If you do not specify a title, the'
' last title set by the child process running in the window will be used.',
options_spec=MATCH_WINDOW_OPTION
)
def cmd_set_window_title(global_opts, opts, args):
return {'title': ' '.join(args), 'match': opts.match}
def set_window_title(boss, window, payload):
windows = [window or boss.active_window]
match = payload['match']
if match:
windows = tuple(boss.match_windows(match))
if not windows:
raise ValueError('No matching windows for expression: {}'.format(match))
for window in windows:
if window:
window.set_title(payload['title'])
cmap = {v.name: v for v in globals().values() if hasattr(v, 'is_cmd')}

View File

@ -88,7 +88,7 @@ class Window:
def __init__(self, tab, child, opts, args, override_title=None):
self.override_title = override_title
self.title = self.override_title or appname
self.child_title = appname
self.id = add_window(tab.os_window_id, tab.id, self.title)
if not self.id:
raise Exception('No tab with id: {} in OS Window: {} was found, or the window counter wrapped'.format(tab.id, tab.os_window_id))
@ -104,6 +104,10 @@ class Window:
self.screen = Screen(self, 24, 80, opts.scrollback_lines, self.id)
setup_colors(self.screen, opts)
@property
def title(self):
return self.override_title or self.child_title
def __repr__(self):
return 'Window(title={}, id={})'.format(self.title, self.id)
@ -188,6 +192,17 @@ class Window:
if get_boss().child_monitor.needs_write(self.id, data) is not True:
print('Failed to write to child %d as it does not exist' % self.id, file=sys.stderr)
def title_updated(self):
update_window_title(self.os_window_id, self.tab_id, self.id, self.title)
t = self.tabref()
if t is not None:
t.title_changed(self)
glfw_post_empty_event()
def set_title(self, title):
self.override_title = title or None
self.title_updated()
# screen callbacks {{{
def use_utf8(self, on):
get_boss().child_monitor.set_iutf8(self.window_id, on)
@ -201,13 +216,9 @@ class Window:
self.screen.send_escape_code_to_child(CSI, 'O')
def title_changed(self, new_title):
self.child_title = sanitize_title(new_title or appname)
if self.override_title is None:
self.title = sanitize_title(new_title or appname)
update_window_title(self.os_window_id, self.tab_id, self.id, self.title)
t = self.tabref()
if t is not None:
t.title_changed(self)
glfw_post_empty_event()
self.title_updated()
def icon_changed(self, new_icon):
pass # TODO: Implement this