Remote control: Allow matching windows by the environment variables of their child process as well

This commit is contained in:
Kovid Goyal 2018-07-24 12:40:59 +05:30
parent 14ea69c9c7
commit ce2b3265be
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 29 additions and 9 deletions

View File

@ -19,6 +19,9 @@ Changelog
- macOS: Allow scrolling window contents using mouse wheel/trackpad even when the - macOS: Allow scrolling window contents using mouse wheel/trackpad even when the
window is not the active window (:iss:`729`) window is not the active window (:iss:`729`)
- Remote control: Allow matching windows by the environment variables of their
child process as well
0.11.3 [2018-07-10] 0.11.3 [2018-07-10]
------------------------------ ------------------------------

View File

@ -152,6 +152,13 @@ class Boss:
return return
if w is not None: if w is not None:
yield w yield w
return
if field == 'env':
kp, vp = exp.partition('=')[::2]
if vp:
pat = tuple(map(re.compile, (kp, vp)))
else:
pat = re.compile(kp), None
else: else:
pat = re.compile(exp) pat = re.compile(exp)
for window in self.all_windows: for window in self.all_windows:

View File

@ -46,18 +46,20 @@ def cmd(short_desc, desc=None, options_spec=None, no_response=False, argspec='..
MATCH_WINDOW_OPTION = '''\ MATCH_WINDOW_OPTION = '''\
--match -m --match -m
The window to match. Match specifications are of the form: The window to match. Match specifications are of the form:
:italic:`field:regexp`. Where field can be one of: id, title, pid, cwd, cmdline, num. :italic:`field:regexp`. Where field can be one of: id, title, pid, cwd, cmdline, num, env.
You can use the :italic:`ls` command to get a list of windows. Note that for You can use the :italic:`ls` command to get a list of windows. Note that for
numeric fields such as id, pid and num the expression is interpreted as a number, numeric fields such as id, pid and num the expression is interpreted as a number,
not a regular expression. The field num refers to the window position in the current tab, not a regular expression. The field num refers to the window position in the current tab,
starting from zero and counting clockwise (this is the same as the order in which the starting from zero and counting clockwise (this is the same as the order in which the
windows are reported by the :italic:`ls` command). The window id of the current window windows are reported by the :italic:`ls` command). The window id of the current window
is available as the KITTY_WINDOW_ID environment variable. is available as the KITTY_WINDOW_ID environment variable. When using the :italic:`env` field
to match on environment variables you can specify only the environment variable name or a name
and value, for example, :italic:`env:MY_ENV_VAR=2`
''' '''
MATCH_TAB_OPTION = '''\ MATCH_TAB_OPTION = '''\
--match -m --match -m
The tab to match. Match specifications are of the form: The tab to match. Match specifications are of the form:
:italic:`field:regexp`. Where field can be one of: id, title, pid, cwd, cmdline. :italic:`field:regexp`. Where field can be one of: id, title, pid, cwd, env, cmdline.
You can use the :italic:`ls` command to get a list of tabs. Note that for You can use the :italic:`ls` command to get a list of tabs. Note that for
numeric fields such as id and pid the expression is interpreted as a number, numeric fields such as id and pid the expression is interpreted as a number,
not a regular expression. When using title or id, first a matching tab is not a regular expression. When using title or id, first a matching tab is
@ -72,8 +74,8 @@ for that window is used.
'List all windows. The list is returned as JSON tree. The top-level is a list of' 'List all windows. The list is returned as JSON tree. The top-level is a list of'
' operating system {appname} windows. Each OS window has an :italic:`id` and a list' ' 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`.' ' 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)` and' ' Each window has an :italic:`id`, :italic:`title`, :italic:`current working directory`, :italic:`process id (PID)`, '
' :italic:`command-line` of the process running in the window.\n\n' ' :italic:`command-line` and :italic:`environment` of the process running in the window.\n\n'
'You can use these criteria to select windows/tabs for the other commands.'.format(appname=appname), 'You can use these criteria to select windows/tabs for the other commands.'.format(appname=appname),
argspec='' argspec=''
) )

View File

@ -140,7 +140,9 @@ class Window:
id=self.id, id=self.id,
title=self.override_title or self.title, title=self.override_title or self.title,
pid=self.child.pid, pid=self.child.pid,
cwd=self.child.current_cwd or self.child.cwd, cmdline=self.child.cmdline cwd=self.child.current_cwd or self.child.cwd,
cmdline=self.child.cmdline,
env=self.child.environ,
) )
@property @property
@ -161,6 +163,12 @@ class Window:
if pat.search(x) is not None: if pat.search(x) is not None:
return True return True
return False return False
if field == 'env':
key_pat, val_pat = pat
for key, val in self.child.environ.items():
if key_pat.search(key) is not None and (
val_pat is None or val_pat.search(val) is not None):
return True
return False return False
def set_visible_in_layout(self, window_idx, val): def set_visible_in_layout(self, window_idx, val):