Add an is_focused field to the output of kitty @ ls

This commit is contained in:
Kovid Goyal 2018-08-23 16:07:26 +05:30
parent 8ba3c4ba50
commit 8b02b89bd6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 32 additions and 6 deletions

View File

@ -118,10 +118,13 @@ class Boss:
return os_window_id return os_window_id
def list_os_windows(self): def list_os_windows(self):
active_tab, active_window = self.active_tab, self.active_window
active_tab_manager = self.active_tab_manager
for os_window_id, tm in self.os_window_map.items(): for os_window_id, tm in self.os_window_map.items():
yield { yield {
'id': os_window_id, 'id': os_window_id,
'tabs': list(tm.list_tabs()), 'is_focused': tm is active_tab_manager,
'tabs': list(tm.list_tabs(active_tab, active_window)),
} }
@property @property

View File

@ -2,6 +2,7 @@
# vim:fileencoding=utf-8 # vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net> # License: GPLv3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
import os
import shlex import shlex
import sys import sys
@ -64,6 +65,25 @@ def kitty_cli_opts(ans, prefix=None):
ans.match_groups['Options'] = matches ans.match_groups['Options'] = matches
def executables(ans, prefix=None):
matches = {}
prefix = prefix or ''
for src in os.environ.get('PATH', '').split(os.pathsep):
if src:
try:
it = os.scandir(src)
except EnvironmentError:
continue
for entry in it:
try:
if entry.name.startswith(prefix) and entry.is_file() and os.access(entry.path, os.X_OK):
matches[entry.name] = None
except EnvironmentError:
pass
if matches:
ans.match_groups['Executables'] = matches
def find_completions(words, new_word, entry_points, namespaced_entry_points): def find_completions(words, new_word, entry_points, namespaced_entry_points):
ans = Completions() ans = Completions()
if not words or words[0] != 'kitty': if not words or words[0] != 'kitty':
@ -73,6 +93,7 @@ def find_completions(words, new_word, entry_points, namespaced_entry_points):
prefix = words[0] if words else '' prefix = words[0] if words else ''
completions_for_first_word(ans, prefix, entry_points, namespaced_entry_points) completions_for_first_word(ans, prefix, entry_points, namespaced_entry_points)
kitty_cli_opts(ans, prefix) kitty_cli_opts(ans, prefix)
executables(ans, prefix)
return ans return ans
return ans return ans

View File

@ -272,9 +272,9 @@ class Tab: # {{{
def move_window_backward(self): def move_window_backward(self):
self.move_window(-1) self.move_window(-1)
def list_windows(self): def list_windows(self, active_window):
for w in self: for w in self:
yield w.as_dict() yield w.as_dict(is_focused=w is active_window)
def matches(self, field, pat): def matches(self, field, pat):
if field == 'id': if field == 'id':
@ -410,12 +410,13 @@ class TabManager: # {{{
def __len__(self): def __len__(self):
return len(self.tabs) return len(self.tabs)
def list_tabs(self): def list_tabs(self, active_tab, active_window):
for tab in self: for tab in self:
yield { yield {
'id': tab.id, 'id': tab.id,
'is_focused': tab is active_tab,
'title': tab.name or tab.title, 'title': tab.name or tab.title,
'windows': list(tab.list_windows()), 'windows': list(tab.list_windows(active_window)),
} }
@property @property

View File

@ -136,9 +136,10 @@ class Window:
return 'Window(title={}, id={}, overlay_for={}, overlay_window_id={})'.format( return 'Window(title={}, id={}, overlay_for={}, overlay_window_id={})'.format(
self.title, self.id, self.overlay_for, self.overlay_window_id) self.title, self.id, self.overlay_for, self.overlay_window_id)
def as_dict(self): def as_dict(self, is_focused=False):
return dict( return dict(
id=self.id, id=self.id,
is_focused=is_focused,
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, cwd=self.child.current_cwd or self.child.cwd,