Clock<T> (#6)
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user