Skip to content

Commit

Permalink
helper xctrl and better snippetr
Browse files Browse the repository at this point in the history
  • Loading branch information
Erotemic committed Apr 24, 2024
1 parent d56f994 commit 041769b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions autoload/vimtk_snippets.vim
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,18 @@ if vimtk.Python.is_module_pythonfile():
>>> cls.main(cmdline=cmdline, **kwargs)
"""
import rich
from rich.markup import escape
config = cls.cli(cmdline=cmdline, data=kwargs, strict=True)
rich.print('config = ' + ub.urepr(config, nl=1))
rich.print('config = ' + escape(ub.urepr(config, nl=1)))

__cli__ = {clsname}
main = __cli__.main

if __name__ == '__main__':
{rr}"""

{cmdline_block}
"""
main()
__cli__.main()
'''
).format(clsname=clsname, modname=modname, cmdline_block=cmdline_block, rr='{r}')
text = text.format(r='r' if '\\' in text else '')
Expand Down
22 changes: 17 additions & 5 deletions vimtk/xctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ def find(XWindow, pattern, method='mru'):
self = XWindow(wm_id)
return self

@classmethod
def findall(XWindow, pattern):
wm_ids = XCtrl.findall_window_ids(pattern)
result = [XWindow(wm_id) for wm_id in wm_ids]
return result

@classmethod
def current(XWindow):
r"""
Expand Down Expand Up @@ -420,6 +426,7 @@ def _wmctrl_terminal_patterns():
'xterm',
'tilda',
'Yakuake',
'kitty.kitty',
])
return terminal_pattern

Expand Down Expand Up @@ -506,15 +513,19 @@ def cmd(XCtrl, command):
return info

@classmethod
def findall_window_ids(XCtrl, pattern):
def findall_window_ids(XCtrl, pattern=None):
"""
Returns:
List[int]: wmctl ids for the matching windows
CommandLine:
python -m vimtk.xctrl XCtrl.findall_window_ids --pat=gvim
python -m vimtk.xctrl XCtrl.findall_window_ids --pat=gvim --noskip
python -m vimtk.xctrl XCtrl.findall_window_ids --pat=gvim
python -m vimtk.xctrl XCtrl.findall_window_ids --pat=joncrall
Example:
>>> # xdoctest: +SKIP
>>> # xdoctest: +REQUIRES(--noskip)
>>> from vimtk.xctrl import * # NOQA
>>> pattern = ub.argval('--pat')
>>> winid_list = XCtrl.findall_window_ids(pattern)
>>> print('winid_list = {!r}'.format(winid_list))
Expand All @@ -528,8 +539,9 @@ def findall_window_ids(XCtrl, pattern):
# List all windows and their identifiers
info = XCtrl.cmd('wmctrl -lx')
lines = info['out'].split('\n')
# Find windows with identifiers matching the pattern
lines = [line for line in lines if re.search(pattern, line)]
if pattern is not None:
# Find windows with identifiers matching the pattern
lines = [line for line in lines if re.search(pattern, line)]
# Get the hex-id portion of the output
winid_list = [line.split()[0] for line in lines]
winid_list = [int(h, 16) for h in winid_list if h]
Expand Down

0 comments on commit 041769b

Please sign in to comment.