Skip to content

Commit 32bea87

Browse files
committed
time format
1 parent 4d2386b commit 32bea87

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
- Buttons may not be pressed within their "active_effect_duration" to prevent inadvertent activations https://github.com/Textualize/textual/pull/4621
1616
- `Screen.dismiss` is now a noop if the screen isn't active. Previously it would raise a `ScreenStackError`, now it returns `False`. https://github.com/Textualize/textual/pull/4621
1717
- Increased window for escape processing to 100ms
18-
- Added `icon` reactive to Header widget
1918

2019
### Added
2120

2221
- Added `Screen.is_active`
22+
- Added `icon` reactive to Header widget https://github.com/Textualize/textual/pull/4627
23+
- Added `time_format` reactive to Header widget https://github.com/Textualize/textual/pull/4627
2324

2425
## [0.65.2] - 2023-06-06
2526

src/textual/widgets/_header.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class HeaderClock(HeaderClockSpace):
7777
}
7878
"""
7979

80+
time_format: Reactive[str] = Reactive("%X")
81+
8082
def _on_mount(self, _: Mount) -> None:
8183
self.set_interval(1, callback=self.refresh, name=f"update header clock")
8284

@@ -86,7 +88,7 @@ def render(self) -> RenderResult:
8688
Returns:
8789
The rendered clock.
8890
"""
89-
return Text(datetime.now().time().strftime("%X"))
91+
return Text(datetime.now().time().strftime(self.time_format))
9092

9193

9294
class HeaderTitle(Widget):
@@ -142,6 +144,9 @@ class Header(Widget):
142144
icon: Reactive[str] = Reactive("⭘")
143145
"""A character for the icon at the top left."""
144146

147+
time_format: Reactive[str] = Reactive("%X")
148+
"""Time format of the clock."""
149+
145150
def __init__(
146151
self,
147152
show_clock: bool = False,
@@ -150,6 +155,7 @@ def __init__(
150155
id: str | None = None,
151156
classes: str | None = None,
152157
icon: str | None = None,
158+
time_format: str | None = None,
153159
):
154160
"""Initialise the header widget.
155161
@@ -158,16 +164,24 @@ def __init__(
158164
name: The name of the header widget.
159165
id: The ID of the header widget in the DOM.
160166
classes: The CSS classes of the header widget.
167+
icon: Single character to use as an icon, or `None` for default.
168+
time_format: Time format (used by strftime) for clock, or `None` for default.
161169
"""
162170
super().__init__(name=name, id=id, classes=classes)
163171
self._show_clock = show_clock
164172
if icon is not None:
165173
self.icon = icon
174+
if time_format is not None:
175+
self.time_format = time_format
166176

167177
def compose(self):
168178
yield HeaderIcon().data_bind(Header.icon)
169179
yield HeaderTitle()
170-
yield HeaderClock() if self._show_clock else HeaderClockSpace()
180+
yield (
181+
HeaderClock().data_bind(Header.time_format)
182+
if self._show_clock
183+
else HeaderClockSpace()
184+
)
171185

172186
def watch_tall(self, tall: bool) -> None:
173187
self.set_class(tall, "-tall")

0 commit comments

Comments
 (0)