-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader_timer.py
More file actions
50 lines (41 loc) · 1.45 KB
/
header_timer.py
File metadata and controls
50 lines (41 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Provides a Textual application header widget."""
from datetime import datetime
from rich.text import Text
from textual.widgets import Header
from textual.widgets._header import HeaderIcon, HeaderTitle, HeaderClockSpace
from textual.reactive import Reactive
class HeaderClock(HeaderClockSpace):
"""Display a clock on the right of the header."""
DEFAULT_CSS = """
HeaderClock {
background: $foreground-darken-1 5%;
color: $text;
text-opacity: 85%;
content-align: center middle;
}
"""
time_seconds: Reactive[str] = Reactive(0)
def render(self):
"""Render the header clock.
Returns:
The rendered clock.
"""
if self.time_seconds == 0: return Text('--:--')
hours = int(self.time_seconds / 3600)
minutes = int((self.time_seconds - 3600 * hours) / 60)
seconds = int((self.time_seconds - 3600 * hours) % 60)
if hours == 0:
return Text(f'{minutes:02}:{seconds:02}')
else:
return Text(f'{hours:02}:{minutes:02}:{seconds:02}')
class HeaderTimer(Header):
time_seconds: Reactive[str] = Reactive(0)
"""Time of the Clock in seconds."""
def compose(self):
yield HeaderIcon().data_bind(Header.icon)
yield HeaderTitle()
yield (
HeaderClock().data_bind(HeaderTimer.time_seconds)
if self._show_clock
else HeaderClockSpace()
)