From 05fb09f56cac508e958df0570ae4155cbb04f4bf Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 13 Apr 2021 08:23:15 +0530 Subject: [PATCH] Fix #3470 --- kitty/rc/ls.py | 19 ++++++++++++++++--- kitty/window.py | 6 +++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/kitty/rc/ls.py b/kitty/rc/ls.py index 0c0cd7179..85d51e387 100644 --- a/kitty/rc/ls.py +++ b/kitty/rc/ls.py @@ -3,7 +3,7 @@ # License: GPLv3 Copyright: 2020, Kovid Goyal import json -from typing import Any, Optional +from typing import Any, Dict, Optional from kitty.constants import appname @@ -24,7 +24,8 @@ class LS(RemoteCommand): f' operating system {appname} windows. Each OS window has an :italic:`id` and a list' ' of :italic:`tabs`. Each tab has its own :italic:`id`, a :italic:`title` and a list of :italic:`windows`.' ' Each window has an :italic:`id`, :italic:`title`, :italic:`current working directory`, :italic:`process id (PID)`, ' - ' :italic:`command-line` and :italic:`environment` of the process running in the window.\n\n' + ' :italic:`command-line` and :italic:`environment` of the process running in the window. Additionally, when' + ' running the command inside a kitty window, that window can be identified by the :italic:`is_self` parameter.\n\n' 'You can use these criteria to select windows/tabs for the other commands.' ) argspec = '' @@ -33,7 +34,19 @@ class LS(RemoteCommand): pass def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get: PayloadGetType) -> ResponseType: - data = list(boss.list_os_windows()) + + def serialize_callback(w: Window, result: Dict[str, Any]) -> Dict[str, Any]: + result['is_self'] = True + return result + + if window is not None: + orig_callback = window.serialize_callback + window.serialize_callback = serialize_callback + try: + data = list(boss.list_os_windows()) + finally: + if window is not None: + window.serialize_callback = orig_callback return json.dumps(data, indent=2, sort_keys=True) diff --git a/kitty/window.py b/kitty/window.py index 1007a587c..c056f4ef8 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -308,6 +308,7 @@ class Window: self.id: int = add_window(tab.os_window_id, tab.id, self.title) self.margin = EdgeWidths() self.padding = EdgeWidths() + self.serialize_callback: Optional[Callable[[Window, Dict[str, Any]], Dict[str, Any]]] = None 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)) self.tab_id = tab.id @@ -393,7 +394,7 @@ class Window: ) def serialize_state(self) -> Dict[str, Any]: - return { + ans = { 'version': 1, 'id': self.id, 'child_title': self.child_title, @@ -407,6 +408,9 @@ class Window: 'margin': self.margin.serialize(), 'padding': self.padding.serialize(), } + if self.serialize_callback is not None: + ans = self.serialize_callback(self, ans) + return ans @property def current_colors(self) -> Dict: