From 3b551a1deec3f4f2d18b93909cfee81415d2cb06 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 8 Jan 2018 14:04:27 +0530 Subject: [PATCH] Implement @set-window-title --- kitty/remote_control.py | 24 ++++++++++++++++++++++++ kitty/window.py | 25 ++++++++++++++++++------- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/kitty/remote_control.py b/kitty/remote_control.py index b3fcedf7b..700d751aa 100644 --- a/kitty/remote_control.py +++ b/kitty/remote_control.py @@ -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')} diff --git a/kitty/window.py b/kitty/window.py index 21979f21b..4a30f56de 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -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