feat(localtime): date (#111)

* feat(localtime): date

* update demo
This commit is contained in:
Jens Krause 2025-09-30 13:24:45 +02:00 committed by GitHub
parent cb6c2d5142
commit 2277eeb033
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 4 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 30 KiB

View File

@ -135,6 +135,17 @@ impl AppTime {
.unwrap_or_else(|e| e.to_string())
}
pub fn format_date(&self) -> String {
format_description::parse("[year]-[month]-[day]")
.map_err(|_| "parse error")
.and_then(|fd| {
OffsetDateTime::from(*self)
.format(&fd)
.map_err(|_| "format error")
})
.unwrap_or_else(|e| e.to_string())
}
pub fn get_period(&self) -> String {
format_description::parse("[period]")
.map_err(|_| "parse error")

View File

@ -115,6 +115,8 @@ impl StatefulWidget for LocalTimeWidget {
let symbol = self.style.get_digit_symbol();
let label = Line::raw("Local Time".to_uppercase());
let label_date = Line::raw(state.time.format_date().to_uppercase());
let mut content_width = max(label.width(), label_date.width()) as u16;
let format = state.format;
let widths = self.get_horizontal_lengths(&format);
@ -127,14 +129,21 @@ impl StatefulWidget for LocalTimeWidget {
widths[2] = 0; // `space`
}
let width = widths.iter().sum();
content_width = max(widths.iter().sum(), content_width);
let v_heights = [
1, // empty (offset) to keep everything centered vertically comparing to "clock" widgets with one label only
DIGIT_HEIGHT, // local time
1, // label
1, // date
];
let area = center(
area,
Constraint::Length(max(width, label.width() as u16)),
Constraint::Length(DIGIT_HEIGHT + 1 /* height of label */),
Constraint::Length(content_width),
Constraint::Length(v_heights.iter().sum()),
);
let [v1, v2] = Layout::vertical(Constraint::from_lengths([DIGIT_HEIGHT, 1])).areas(area);
let [_, v1, v2, v3] = Layout::vertical(Constraint::from_lengths(v_heights)).areas(area);
match state.format {
AppTimeFormat::HhMmSs => {
@ -181,5 +190,6 @@ impl StatefulWidget for LocalTimeWidget {
}
}
label.centered().render(v2, buf);
label_date.centered().render(v3, buf);
}
}