Clock<T> (#6)

This commit is contained in:
Jens K.
2024-12-02 17:54:47 +01:00
committed by GitHub
parent 2f587c97b5
commit 4f66ea86d4
9 changed files with 189 additions and 48 deletions

View File

@@ -1,23 +1,30 @@
use ratatui::{
buffer::Buffer,
layout::Rect,
layout::{Constraint, Layout, Rect},
widgets::{Paragraph, Widget},
};
#[derive(Debug, Default, Clone, PartialEq, Eq)]
use crate::clock::{self, Clock};
#[derive(Debug)]
pub struct Countdown {
headline: String,
clock: Clock<clock::Countdown>,
}
impl Countdown {
pub const fn new(headline: String) -> Self {
Self { headline }
pub const fn new(headline: String, clock: Clock<clock::Countdown>) -> Self {
Self { headline, clock }
}
}
impl Widget for Countdown {
fn render(self, area: Rect, buf: &mut Buffer) {
fn render(mut self, area: Rect, buf: &mut Buffer) {
let h = Paragraph::new(self.headline).centered();
h.render(area, buf);
let c = Paragraph::new(self.clock.format()).centered();
let [v1, v2] = Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area);
h.render(v1, buf);
c.render(v2, buf)
}
}

View File

@@ -5,28 +5,26 @@ use ratatui::{
widgets::Widget,
};
use crate::utils::format_ms;
#[derive(Debug, Clone)]
pub struct Header {
tick: u128,
show_fps: bool,
}
impl Header {
pub fn new(tick: u128) -> Self {
Self { tick }
pub fn new(show_fps: bool) -> Self {
Self { show_fps }
}
}
impl Widget for Header {
fn render(self, area: Rect, buf: &mut Buffer) {
let time_string = format_ms(self.tick * 100, true);
let tick_span = Span::raw(time_string);
let tick_width = tick_span.width().try_into().unwrap_or(0);
let fps_txt = if self.show_fps { "FPS (soon)" } else { "" };
let fps_span = Span::raw(fps_txt);
let fps_width = fps_span.width().try_into().unwrap_or(0);
let [h1, h2] =
Layout::horizontal([Constraint::Fill(1), Constraint::Length(tick_width)]).areas(area);
Layout::horizontal([Constraint::Fill(1), Constraint::Length(fps_width)]).areas(area);
Span::raw("tim:r").render(h1, buf);
tick_span.render(h2, buf);
fps_span.render(h2, buf);
}
}

View File

@@ -1,24 +1,30 @@
use ratatui::{
buffer::Buffer,
layout::Rect,
layout::{Constraint, Layout, Rect},
widgets::{Paragraph, Widget},
};
#[derive(Debug, Default, Clone, PartialEq, Eq)]
use crate::clock::{self, Clock};
#[derive(Debug)]
pub struct Timer {
value: u64,
headline: String,
clock: Clock<clock::Timer>,
}
impl Timer {
pub const fn new(value: u64, headline: String) -> Self {
Self { value, headline }
pub const fn new(headline: String, clock: Clock<clock::Timer>) -> Self {
Self { headline, clock }
}
}
impl Widget for Timer {
fn render(self, area: Rect, buf: &mut Buffer) {
fn render(mut self, area: Rect, buf: &mut Buffer) {
let h = Paragraph::new(self.headline).centered();
h.render(area, buf);
let c = Paragraph::new(self.clock.format()).centered();
let [v1, v2] = Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area);
h.render(v1, buf);
c.render(v2, buf)
}
}