diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 8fdd7369c..a91a0e60e 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -2459,7 +2459,8 @@ use :code:`{sup.index}`. All data available is: If a program running in a window reports its status using the :ref:`program status protocol `, a short symbol for the most urgent status of all windows in the tab: blocked on the user, failed, finished or working - (with percentage, if reported). Empty string if no program reports a status. + (with percentage, if reported), followed by the number of windows in that status, + if more than one. Empty string if no program reports a status. :code:`tab.children_mem_usage` The current memory usage of all child process of the tab and their descendants :code:`custom` diff --git a/kitty/program_status.py b/kitty/program_status.py index 5ae863c04..d03b6aeae 100644 --- a/kitty/program_status.py +++ b/kitty/program_status.py @@ -9,6 +9,7 @@ import re from collections import OrderedDict from collections.abc import Iterable, Iterator from enum import Enum +from itertools import count from typing import Any, NamedTuple OSC_CODE = 7501 @@ -54,6 +55,10 @@ UNSEEN_STATES = frozenset((State.done, State.error)) URGENCY = {State.idle: 0, State.working: 1, State.done: 2, State.error: 3, State.blocked: 4} +# Orders updates across all windows, to find the most recently updated record +update_counter = count() + + class InvalidReport(ValueError): pass @@ -168,6 +173,7 @@ class ProgramStatus: def __init__(self) -> None: self.records: OrderedDict[str, Record] = OrderedDict() + self.update_seq: dict[str, int] = {} def __bool__(self) -> bool: return bool(self.records) @@ -179,24 +185,26 @@ class ProgramStatus: "Apply a report, returns True if the records changed" if isinstance(report, Clear): return self.remove(lambda r: is_descendant_or_self(r.id, report.id)) + self.update_seq[report.id] = next(update_counter) if self.records.get(report.id) == report: self.records.move_to_end(report.id) return False self.records[report.id] = report self.records.move_to_end(report.id) while len(self.records) > MAX_RECORDS: # drop the least recently updated record - self.records.popitem(last=False) + del self.update_seq[self.records.popitem(last=False)[0]] return True def remove(self, predicate: Any) -> bool: remove = tuple(rid for rid, r in self.records.items() if predicate(r)) for rid in remove: - del self.records[rid] + del self.records[rid], self.update_seq[rid] return bool(remove) def clear(self) -> bool: ans = bool(self.records) self.records.clear() + self.update_seq.clear() return ans def on_process_exit_or_prompt(self) -> bool: @@ -217,7 +225,11 @@ class ProgramStatus: return '' def most_urgent(self) -> Record | None: - return most_urgent(reversed(self.records.values())) # prefer the most recently updated + ans: Record | None = None + for r in reversed(self.records.values()): # prefer the most recently updated + if ans is None or URGENCY[r.state] > URGENCY[ans.state]: + ans = r + return ans def as_list(self) -> list[dict[str, Any]]: ans = [] @@ -228,24 +240,31 @@ class ProgramStatus: return ans -def most_urgent(records: Iterable[Record]) -> Record | None: - ans: Record | None = None - for r in records: - if ans is None or URGENCY[r.state] > URGENCY[ans.state]: - ans = r - return ans - - -def status_symbol(record: Record | None) -> str: - "A short indicator for use in tab and window titles" +def status_symbol(record: Record | None, num: int = 1) -> str: + "A short indicator for use in tab and window titles, num is the number of windows in this state" if record is None or record.state is State.idle: return '' if record.state is State.blocked: - return ' ' - if record.state is State.error: - return ' ' - if record.state is State.done: - return ' ' - if record.progress is not None: - return f' {record.progress}% ' - return ' ' + sym = '\uf071' + elif record.state is State.error: + sym = '\ue654' + elif record.state is State.done: + sym = '\uf00c' + else: + sym = '\uf110' + ans = f'{sym} {num} ' if num > 1 else f'{sym} ' + if record.state is State.working and record.progress is not None: + ans += f'{record.progress}% ' + return ans + + +def combined_status_symbol(statuses: Iterable[ProgramStatus]) -> str: + """ + The most urgent status of several windows, along with the number of + windows in that status. Ties are broken by the most recently updated record. + """ + items = [(r, ps.update_seq[r.id]) for ps in statuses if (r := ps.most_urgent()) is not None] + if not items: + return '' + best = max(items, key=lambda x: (URGENCY[x[0].state], x[1]))[0] + return status_symbol(best, sum(1 for r, _ in items if r.state is best.state)) diff --git a/kitty/tab_bar.py b/kitty/tab_bar.py index 10af5ff8f..9934aee2a 100644 --- a/kitty/tab_bar.py +++ b/kitty/tab_bar.py @@ -291,12 +291,10 @@ class TabAccessor: @property def program_status(self) -> str: - from .program_status import most_urgent, status_symbol + from .program_status import combined_status_symbol tab = get_boss().tab_for_id(self.tab_id) - if not tab: - return '' - return status_symbol(most_urgent(r for w in tab if (r := w.program_status.most_urgent()) is not None)) + return combined_status_symbol(w.program_status for w in tab) if tab else '' @property def children_mem_usage(self) -> str: diff --git a/kitty_tests/program_status.py b/kitty_tests/program_status.py index 16d42b752..1f445b925 100644 --- a/kitty_tests/program_status.py +++ b/kitty_tests/program_status.py @@ -12,6 +12,7 @@ from kitty.program_status import ( ProgramStatus, Record, State, + combined_status_symbol, parse_report, status_symbol, ) @@ -127,6 +128,30 @@ class TestProgramStatus(BaseTest): def test_program_status_symbol(self): self.ae(status_symbol(None), '') self.ae(status_symbol(Record(state=State.idle)), '') - self.assertIn('40%', status_symbol(Record(state=State.working, progress=40))) + self.ae(status_symbol(Record(state=State.working, progress=40)), '\uf110 40% ') + self.ae(status_symbol(Record(state=State.working, progress=40), 2), '\uf110 2 40% ') + self.ae(status_symbol(Record(state=State.blocked), 3), '\uf071 3 ') syms = {status_symbol(Record(state=s)) for s in State if s is not State.idle} self.ae(len(syms), 4) + + def window(*bodies: str) -> ProgramStatus: + ps = ProgramStatus() + for b in bodies: + ps.apply(parse_report(b)) + return ps + + self.ae(combined_status_symbol([]), '') + self.ae(combined_status_symbol([ProgramStatus(), window('state=idle')]), '') + # the most urgent state wins and the windows in it are counted + a, b, c = window('state=blocked'), window('state=working'), window('state=idle') + self.ae(combined_status_symbol([a, b, c]), '\uf071 ') + b.apply(parse_report('state=blocked:kind=auth')) + self.ae(combined_status_symbol([a, b, c]), '\uf071 2 ') + # several records in one window count as one window + self.ae(combined_status_symbol([window('state=blocked', 'state=blocked:id=x'), c]), '\uf071 ') + # ties are broken by the most recently updated record, across windows + a, b = window('state=working:progress=10'), window('state=working:progress=20') + self.ae(combined_status_symbol([a, b]), '\uf110 2 20% ') + a.apply(parse_report('state=working:progress=30')) + self.ae(combined_status_symbol([a, b]), '\uf110 2 30% ') + self.ae(combined_status_symbol([b, a]), '\uf110 2 30% ')