2
2
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
3
3
4
4
import json
5
+ import os
5
6
from collections .abc import Callable
6
- from typing import TYPE_CHECKING
7
+ from typing import TYPE_CHECKING , Sequence
7
8
8
9
from kitty .constants import appname
9
10
10
11
from .base import MATCH_TAB_OPTION , MATCH_WINDOW_OPTION , ArgsType , Boss , PayloadGetType , PayloadType , RCOptions , RemoteCommand , ResponseType , Tab , Window
12
+ from ..boss import OSWindowDict
13
+ from ..child import ProcessDesc
14
+ from ..launch import is_excluded_env_var
11
15
12
16
if TYPE_CHECKING :
13
17
from kitty .cli_stub import LSRCOptions as CLIOptions
@@ -19,6 +23,7 @@ class LS(RemoteCommand):
19
23
match/str: Window to change colors in
20
24
match_tab/str: Tab to change colors in
21
25
self/bool: Boolean indicating whether to list only the window the command is run in
26
+ output_format/str: Output in json or session format
22
27
'''
23
28
24
29
short_desc = 'List tabs/windows'
@@ -41,6 +46,13 @@ class LS(RemoteCommand):
41
46
--self
42
47
type=bool-set
43
48
Only list the window this command is run in.
49
+
50
+
51
+ --output-format
52
+ type=choices
53
+ choices=json,session
54
+ default=json
55
+ Output in json or session format
44
56
''' + '\n \n ' + MATCH_WINDOW_OPTION + '\n \n ' + MATCH_TAB_OPTION .replace ('--match -m' , '--match-tab -t' , 1 )
45
57
46
58
def message_to_kitty (self , global_opts : RCOptions , opts : 'CLIOptions' , args : ArgsType ) -> PayloadType :
@@ -49,6 +61,7 @@ def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: Arg
49
61
def response_from_kitty (self , boss : Boss , window : Window | None , payload_get : PayloadGetType ) -> ResponseType :
50
62
tab_filter : Callable [[Tab ], bool ] | None = None
51
63
window_filter : Callable [[Window ], bool ] | None = None
64
+ output_session : bool = False
52
65
53
66
if payload_get ('self' ):
54
67
def wf (w : Window ) -> bool :
@@ -59,26 +72,32 @@ def wf(w: Window) -> bool:
59
72
def wf (w : Window ) -> bool :
60
73
return w .id in window_ids
61
74
window_filter = wf
62
- data = list (boss .list_os_windows (window , tab_filter , window_filter ))
63
- if not payload_get ('all_env_vars' ):
64
- all_env_blocks : list [dict [str , str ]] = []
65
- common_env_vars : set [tuple [str , str ]] = set ()
66
- for osw in data :
67
- for tab in osw .get ('tabs' , ()):
68
- for w in tab .get ('windows' , ()):
69
- env : dict [str , str ] = w .get ('env' , {})
70
- frozen_env = set (env .items ())
71
- if all_env_blocks :
72
- common_env_vars &= frozen_env
73
- else :
74
- common_env_vars = frozen_env
75
- all_env_blocks .append (env )
76
- if common_env_vars and len (all_env_blocks ) > 1 :
77
- remove_env_vars = {k for k , v in common_env_vars }
78
- for env in all_env_blocks :
79
- for r in remove_env_vars :
80
- env .pop (r , None )
81
- return json .dumps (data , indent = 2 , sort_keys = True )
75
+ elif payload_get ('output_format' ) == 'session' :
76
+ output_session = True
77
+
78
+ if not output_session :
79
+ data = list (boss .list_os_windows (window , tab_filter , window_filter ))
80
+ if not payload_get ('all_env_vars' ):
81
+ all_env_blocks : list [dict [str , str ]] = []
82
+ common_env_vars : set [tuple [str , str ]] = set ()
83
+ for osw in data :
84
+ for tab in osw .get ('tabs' , ()):
85
+ for w in tab .get ('windows' , ()):
86
+ env : dict [str , str ] = w .get ('env' , {})
87
+ frozen_env = set (env .items ())
88
+ if all_env_blocks :
89
+ common_env_vars &= frozen_env
90
+ else :
91
+ common_env_vars = frozen_env
92
+ all_env_blocks .append (env )
93
+ if common_env_vars and len (all_env_blocks ) > 1 :
94
+ remove_env_vars = {k for k , v in common_env_vars }
95
+ for env in all_env_blocks :
96
+ for r in remove_env_vars :
97
+ env .pop (r , None )
98
+ return json .dumps (data , indent = 2 , sort_keys = True )
99
+ else :
100
+ return "\n " .join (boss .serialize_state_as_session ())
82
101
83
102
84
103
ls = LS ()
0 commit comments