Implement window listing

This commit is contained in:
Kovid Goyal
2018-01-07 19:57:16 +05:30
parent eff881f4bb
commit 0cea6d5a1b
5 changed files with 46 additions and 1 deletions

View File

@@ -95,6 +95,13 @@ class Boss:
if dpi_changed:
self.on_dpi_change(os_window_id)
def list_os_windows(self):
for os_window_id, tm in self.os_window_map.items():
yield {
'id': os_window_id,
'tabs': list(tm.list_tabs()),
}
def _new_os_window(self, args, cwd_from=None):
sw = self.args_to_special_window(args, cwd_from) if args else None
startup_session = create_session(self.opts, special_window=sw, cwd_from=cwd_from)

View File

@@ -19,6 +19,13 @@ def cwd_of_process(pid):
return os.path.realpath(ans)
def cmdline_of_process(pid):
if is_macos:
# TODO: macOS implementation, see DarwinProcess.c in htop for inspiration
raise NotImplementedError()
return open('/proc/{}/cmdline'.format(pid), 'rb').read().decode('utf-8').split('\0')
def remove_cloexec(fd):
fcntl.fcntl(fd, fcntl.F_SETFD, fcntl.fcntl(fd, fcntl.F_GETFD) & ~fcntl.FD_CLOEXEC)

View File

@@ -36,7 +36,9 @@ def cmd_ls(global_opts, opts, args):
def ls(boss, window):
raise NotImplementedError()
data = list(boss.list_os_windows())
data = json.dumps(data, indent=2, sort_keys=True)
return data
def handle_cmd(boss, window, cmd):

View File

@@ -202,6 +202,10 @@ class Tab: # {{{
def move_window_backward(self):
self.move_window(-1)
def list_windows(self):
for w in self:
yield w.as_dict()
def __iter__(self):
yield from iter(self.windows)
@@ -379,6 +383,14 @@ class TabManager: # {{{
def __len__(self):
return len(self.tabs)
def list_tabs(self):
for tab in self:
yield {
'id': tab.id,
'title': tab.name or tab.title,
'windows': list(tab.list_windows()),
}
@property
def active_tab(self):
return self.tabs[self.active_tab_idx] if self.tabs else None

View File

@@ -8,6 +8,7 @@ import weakref
from collections import deque
from enum import Enum
from .child import cmdline_of_process, cwd_of_process
from .config import build_ansi_color_table, parse_send_text_bytes
from .constants import (
ScreenGeometry, WindowGeometry, appname, get_boss, wakeup
@@ -107,6 +108,22 @@ class Window:
def __repr__(self):
return 'Window(title={}, id={})'.format(self.title, self.id)
def as_dict(self):
try:
cwd = cwd_of_process(self.child.pid)
except Exception:
cwd = None
try:
cmdline = cmdline_of_process(self.child.pid)
except Exception:
cmdline = None
return dict(
id=self.id,
title=self.override_title or self.title,
pid=self.child.pid,
cwd=cwd, cmdline=cmdline
)
def set_visible_in_layout(self, window_idx, val):
val = bool(val)
if val is not self.is_visible_in_layout: