Skip to content

Commit 1a265af

Browse files
committed
Added a counter feature to each substate
1 parent 7b5a5f6 commit 1a265af

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

tui/components.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
from enum import Enum
23
from typing import Optional
34

@@ -80,6 +81,47 @@ class TUIComponents(str, Enum):
8081
LOG_FILTER = "log-filter"
8182

8283

84+
class SubstateLine(Horizontal):
85+
"""A single substate row with an attached timer."""
86+
87+
def __init__(self, text: str, indent: str, **kwargs):
88+
super().__init__(**kwargs)
89+
self.text = text
90+
self.indent = indent
91+
self.start_time = time.monotonic()
92+
self._line_widget: Static | None = None
93+
94+
def compose(self):
95+
self._line_widget = Static(self._format_line(), classes="substate-line-text")
96+
yield self._line_widget
97+
98+
def on_mount(self) -> None:
99+
self._refresh_timer()
100+
self.set_interval(1, self._refresh_timer)
101+
102+
def _format_timer(self) -> str:
103+
elapsed = int(time.monotonic() - self.start_time)
104+
if elapsed < 60:
105+
return f"{elapsed}s"
106+
minutes = elapsed // 60
107+
seconds = elapsed % 60
108+
if minutes < 60:
109+
return f"{minutes}m {seconds}s"
110+
hours = minutes // 60
111+
return f"{hours}h {minutes % 60}m"
112+
113+
def _format_line(self) -> str:
114+
timer = self._format_timer()
115+
return f"{self.indent}{self.text} [#888888]({timer})[/#888888]"
116+
117+
def _refresh_timer(self) -> None:
118+
try:
119+
if self._line_widget:
120+
self._line_widget.update(self._format_line())
121+
except Exception:
122+
pass
123+
124+
83125
class ProgressItem(Vertical):
84126
"""A vertical container for a status, description, and substates."""
85127

@@ -176,7 +218,7 @@ async def _render_substates_recursive(self, container: Vertical, substates: list
176218

177219
for substate in substates:
178220
# Render the current substate
179-
substate_widget = Static(f"{indent}{substate.text}", classes="substate")
221+
substate_widget = SubstateLine(substate.text, indent, classes="substate-row")
180222
await container.mount(substate_widget)
181223

182224
# Recursively render children if they exist

tui/styles.css

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ ProgressItem {
8181
padding: 0;
8282
}
8383

84-
.substate {
85-
margin: 0;
86-
padding: 0;
84+
.substate-row {
85+
height: auto;
86+
width: 100%;
87+
padding: 0 1;
88+
}
89+
90+
.substate-line-text {
91+
width: 1fr;
92+
color: #FFF;
93+
content-align: left middle;
8794
}
8895

8996
#frid-progress Vertical {

0 commit comments

Comments
 (0)