clock style (#18)

This commit is contained in:
Jens K. 2024-12-18 13:51:22 +01:00 committed by GitHub
parent 3c9ae881c9
commit ab10a38901
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 105 additions and 56 deletions

View File

@ -4,7 +4,7 @@ use crate::{
events::{Event, EventHandler, Events}, events::{Event, EventHandler, Events},
terminal::Terminal, terminal::Terminal,
widgets::{ widgets::{
clock::{self, Clock}, clock::{self, Clock, Style as ClockStyle},
countdown::{Countdown, CountdownWidget}, countdown::{Countdown, CountdownWidget},
footer::Footer, footer::Footer,
header::Header, header::Header,
@ -43,6 +43,7 @@ pub struct App {
countdown: Countdown, countdown: Countdown,
timer: Timer, timer: Timer,
pomodoro: Pomodoro, pomodoro: Pomodoro,
clock_style: ClockStyle,
} }
impl App { impl App {
@ -51,6 +52,7 @@ impl App {
mode: Mode::Running, mode: Mode::Running,
content: Content::Countdown, content: Content::Countdown,
show_menu: false, show_menu: false,
clock_style: ClockStyle::Default,
countdown: Countdown::new(Clock::<clock::Countdown>::new( countdown: Countdown::new(Clock::<clock::Countdown>::new(
args.countdown, args.countdown,
Duration::from_millis(TICK_VALUE_MS), Duration::from_millis(TICK_VALUE_MS),
@ -100,6 +102,7 @@ impl App {
KeyCode::Char('t') => self.content = Content::Timer, KeyCode::Char('t') => self.content = Content::Timer,
KeyCode::Char('p') => self.content = Content::Pomodoro, KeyCode::Char('p') => self.content = Content::Pomodoro,
KeyCode::Char('m') => self.show_menu = !self.show_menu, KeyCode::Char('m') => self.show_menu = !self.show_menu,
KeyCode::Char('b') => self.clock_style = self.clock_style.next(),
KeyCode::Up => self.show_menu = true, KeyCode::Up => self.show_menu = true,
KeyCode::Down => self.show_menu = false, KeyCode::Down => self.show_menu = false,
_ => {} _ => {}
@ -119,9 +122,15 @@ struct AppWidget;
impl AppWidget { impl AppWidget {
fn render_content(&self, area: Rect, buf: &mut Buffer, state: &mut App) { fn render_content(&self, area: Rect, buf: &mut Buffer, state: &mut App) {
match state.content { match state.content {
Content::Timer => TimerWidget.render(area, buf, &mut state.timer), Content::Timer => {
Content::Countdown => CountdownWidget.render(area, buf, &mut state.countdown), TimerWidget.render(area, buf, &mut (state.clock_style, state.timer.clone()))
Content::Pomodoro => PomodoroWidget.render(area, buf, &mut state.pomodoro), }
Content::Countdown => {
CountdownWidget.render(area, buf, &mut (state.clock_style, state.countdown.clone()))
}
Content::Pomodoro => {
PomodoroWidget.render(area, buf, &mut (state.clock_style, state.pomodoro.clone()))
}
}; };
} }
} }

View File

@ -11,6 +11,25 @@ use ratatui::{
use crate::utils::center_horizontal; use crate::utils::center_horizontal;
#[derive(Debug, Copy, Clone)]
pub enum Style {
Default,
Empty,
Thick,
Cross,
}
impl Style {
pub fn next(&self) -> Self {
match self {
Style::Default => Style::Empty,
Style::Empty => Style::Thick,
Style::Thick => Style::Cross,
Style::Cross => Style::Default,
}
}
}
#[derive(Debug, Copy, Clone, Display, PartialEq, Eq)] #[derive(Debug, Copy, Clone, Display, PartialEq, Eq)]
pub enum Time { pub enum Time {
Seconds, Seconds,
@ -404,8 +423,6 @@ impl Clock<Timer> {
} }
} }
const DIGIT_SYMBOL: &str = "";
const DIGIT_SIZE: usize = 5; const DIGIT_SIZE: usize = 5;
const DIGIT_WIDTH: u16 = DIGIT_SIZE as u16; const DIGIT_WIDTH: u16 = DIGIT_SIZE as u16;
const DIGIT_HEIGHT: u16 = DIGIT_SIZE as u16 + 1 /* border height */; const DIGIT_HEIGHT: u16 = DIGIT_SIZE as u16 + 1 /* border height */;
@ -515,6 +532,7 @@ pub struct ClockWidget<T>
where where
T: std::fmt::Debug, T: std::fmt::Debug,
{ {
style: Style,
phantom: PhantomData<T>, phantom: PhantomData<T>,
} }
@ -522,12 +540,22 @@ impl<T> ClockWidget<T>
where where
T: std::fmt::Debug, T: std::fmt::Debug,
{ {
pub fn new() -> Self { pub fn new(style: Style) -> Self {
Self { Self {
style,
phantom: PhantomData, phantom: PhantomData,
} }
} }
fn get_digit_symbol(&self) -> &str {
match &self.style {
Style::Default => "",
Style::Empty => "",
Style::Cross => "",
Style::Thick => "",
}
}
fn get_horizontal_lengths(&self, format: &Format) -> Vec<u16> { fn get_horizontal_lengths(&self, format: &Format) -> Vec<u16> {
match format { match format {
Format::HhMmSs => vec![ Format::HhMmSs => vec![
@ -589,7 +617,7 @@ where
DIGIT_HEIGHT DIGIT_HEIGHT
} }
fn render_digit(number: u64, with_border: bool, area: Rect, buf: &mut Buffer) { fn render_digit(&self, number: u64, with_border: bool, area: Rect, buf: &mut Buffer) {
let left = area.left(); let left = area.left();
let top = area.top(); let top = area.top();
@ -616,7 +644,7 @@ where
y: top + y as u16, y: top + y as u16,
}; };
if let Some(cell) = buf.cell_mut(p) { if let Some(cell) = buf.cell_mut(p) {
cell.set_symbol(DIGIT_SYMBOL); cell.set_symbol(self.get_digit_symbol());
} }
} }
}); });
@ -635,7 +663,7 @@ where
} }
} }
fn render_colon(area: Rect, buf: &mut Buffer) { fn render_colon(&self, area: Rect, buf: &mut Buffer) {
let left = area.left(); let left = area.left();
let top = area.top(); let top = area.top();
@ -660,7 +688,7 @@ where
for pos in positions { for pos in positions {
if let Some(cell) = buf.cell_mut(pos) { if let Some(cell) = buf.cell_mut(pos) {
cell.set_symbol(DIGIT_SYMBOL); cell.set_symbol(self.get_digit_symbol());
} }
} }
} }
@ -673,9 +701,9 @@ where
type State = Clock<T>; type State = Clock<T>;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let format = &state.format; let format = state.format;
let widths = self.get_horizontal_lengths(format); let widths = self.get_horizontal_lengths(&format);
let area = center_horizontal(area, Constraint::Length(self.get_width(format))); let area = center_horizontal(area, Constraint::Length(self.get_width(&format)));
let edit_hours = matches!(state.mode, Mode::Editable(Time::Hours, _)); let edit_hours = matches!(state.mode, Mode::Editable(Time::Hours, _));
let edit_minutes = matches!(state.mode, Mode::Editable(Time::Minutes, _)); let edit_minutes = matches!(state.mode, Mode::Editable(Time::Minutes, _));
let edit_secs = matches!(state.mode, Mode::Editable(Time::Seconds, _)); let edit_secs = matches!(state.mode, Mode::Editable(Time::Seconds, _));
@ -683,51 +711,51 @@ where
Format::HhMmSs => { Format::HhMmSs => {
let [hh, _, h, c_hm, mm, _, m, c_ms, ss, _, s] = let [hh, _, h, c_hm, mm, _, m, c_ms, ss, _, s] =
Layout::horizontal(Constraint::from_lengths(widths)).areas(area); Layout::horizontal(Constraint::from_lengths(widths)).areas(area);
Self::render_digit(state.current_hours() / 10, edit_hours, hh, buf); self.render_digit(state.current_hours() / 10, edit_hours, hh, buf);
Self::render_digit(state.current_hours() % 10, edit_hours, h, buf); self.render_digit(state.current_hours() % 10, edit_hours, h, buf);
Self::render_colon(c_hm, buf); self.render_colon(c_hm, buf);
Self::render_digit(state.current_minutes_mod() / 10, edit_minutes, mm, buf); self.render_digit(state.current_minutes_mod() / 10, edit_minutes, mm, buf);
Self::render_digit(state.current_minutes_mod() % 10, edit_minutes, m, buf); self.render_digit(state.current_minutes_mod() % 10, edit_minutes, m, buf);
Self::render_colon(c_ms, buf); self.render_colon(c_ms, buf);
Self::render_digit(state.current_seconds_mod() / 10, edit_secs, ss, buf); self.render_digit(state.current_seconds_mod() / 10, edit_secs, ss, buf);
Self::render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf); self.render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf);
} }
Format::HMmSs => { Format::HMmSs => {
let [h, c_hm, mm, _, m, c_ms, ss, _, s] = let [h, c_hm, mm, _, m, c_ms, ss, _, s] =
Layout::horizontal(Constraint::from_lengths(widths)).areas(area); Layout::horizontal(Constraint::from_lengths(widths)).areas(area);
Self::render_digit(state.current_hours() % 10, edit_hours, h, buf); self.render_digit(state.current_hours() % 10, edit_hours, h, buf);
Self::render_colon(c_hm, buf); self.render_colon(c_hm, buf);
Self::render_digit(state.current_minutes_mod() / 10, edit_minutes, mm, buf); self.render_digit(state.current_minutes_mod() / 10, edit_minutes, mm, buf);
Self::render_digit(state.current_minutes_mod() % 10, edit_minutes, m, buf); self.render_digit(state.current_minutes_mod() % 10, edit_minutes, m, buf);
Self::render_colon(c_ms, buf); self.render_colon(c_ms, buf);
Self::render_digit(state.current_seconds_mod() / 10, edit_secs, ss, buf); self.render_digit(state.current_seconds_mod() / 10, edit_secs, ss, buf);
Self::render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf); self.render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf);
} }
Format::MmSs => { Format::MmSs => {
let [mm, _, m, c_ms, ss, _, s] = let [mm, _, m, c_ms, ss, _, s] =
Layout::horizontal(Constraint::from_lengths(widths)).areas(area); Layout::horizontal(Constraint::from_lengths(widths)).areas(area);
Self::render_digit(state.current_minutes_mod() / 10, edit_minutes, mm, buf); self.render_digit(state.current_minutes_mod() / 10, edit_minutes, mm, buf);
Self::render_digit(state.current_minutes_mod() % 10, edit_minutes, m, buf); self.render_digit(state.current_minutes_mod() % 10, edit_minutes, m, buf);
Self::render_colon(c_ms, buf); self.render_colon(c_ms, buf);
Self::render_digit(state.current_seconds_mod() / 10, edit_secs, ss, buf); self.render_digit(state.current_seconds_mod() / 10, edit_secs, ss, buf);
Self::render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf); self.render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf);
} }
Format::MSs => { Format::MSs => {
let [m, c_ms, ss, _, s] = let [m, c_ms, ss, _, s] =
Layout::horizontal(Constraint::from_lengths(widths)).areas(area); Layout::horizontal(Constraint::from_lengths(widths)).areas(area);
Self::render_digit(state.current_minutes_mod() % 10, edit_minutes, m, buf); self.render_digit(state.current_minutes_mod() % 10, edit_minutes, m, buf);
Self::render_colon(c_ms, buf); self.render_colon(c_ms, buf);
Self::render_digit(state.current_seconds_mod() / 10, edit_secs, ss, buf); self.render_digit(state.current_seconds_mod() / 10, edit_secs, ss, buf);
Self::render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf); self.render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf);
} }
Format::Ss => { Format::Ss => {
let [ss, _, s] = Layout::horizontal(Constraint::from_lengths(widths)).areas(area); let [ss, _, s] = Layout::horizontal(Constraint::from_lengths(widths)).areas(area);
Self::render_digit(state.current_seconds_mod() / 10, edit_secs, ss, buf); self.render_digit(state.current_seconds_mod() / 10, edit_secs, ss, buf);
Self::render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf); self.render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf);
} }
Format::S => { Format::S => {
let [s] = Layout::horizontal(Constraint::from_lengths(widths)).areas(area); let [s] = Layout::horizontal(Constraint::from_lengths(widths)).areas(area);
Self::render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf); self.render_digit(state.current_seconds_mod() % 10, edit_secs, s, buf);
} }
} }
} }

View File

@ -13,6 +13,8 @@ use crate::{
widgets::clock::{self, Clock, ClockWidget}, widgets::clock::{self, Clock, ClockWidget},
}; };
use super::clock::Style;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Countdown { pub struct Countdown {
clock: Clock<clock::Countdown>, clock: Clock<clock::Countdown>,
@ -67,9 +69,11 @@ impl EventHandler for Countdown {
pub struct CountdownWidget; pub struct CountdownWidget;
impl StatefulWidget for CountdownWidget { impl StatefulWidget for CountdownWidget {
type State = Countdown; type State = (Style, Countdown);
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let clock = ClockWidget::new(); let style = state.0;
let state = &mut state.1;
let clock = ClockWidget::new(style);
let label = Line::raw((format!("Countdown {}", state.clock.get_mode())).to_uppercase()); let label = Line::raw((format!("Countdown {}", state.clock.get_mode())).to_uppercase());
let area = center( let area = center(

View File

@ -15,6 +15,8 @@ use std::{cmp::max, time::Duration};
use strum::Display; use strum::Display;
use super::clock::Style;
#[derive(Debug, Clone, Display, Hash, Eq, PartialEq)] #[derive(Debug, Clone, Display, Hash, Eq, PartialEq)]
enum Mode { enum Mode {
Work, Work,
@ -117,9 +119,11 @@ impl EventHandler for Pomodoro {
pub struct PomodoroWidget; pub struct PomodoroWidget;
impl StatefulWidget for PomodoroWidget { impl StatefulWidget for PomodoroWidget {
type State = Pomodoro; type State = (Style, Pomodoro);
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let clock = ClockWidget::new(); let style = state.0;
let state = &mut state.1;
let clock_widget = ClockWidget::new(style);
let label = Line::raw( let label = Line::raw(
(format!( (format!(
"Pomodoro {} {}", "Pomodoro {} {}",
@ -132,16 +136,16 @@ impl StatefulWidget for PomodoroWidget {
let area = center( let area = center(
area, area,
Constraint::Length(max( Constraint::Length(max(
clock.get_width(&state.get_clock().get_format()), clock_widget.get_width(&state.get_clock().get_format()),
label.width() as u16, label.width() as u16,
)), )),
Constraint::Length(clock.get_height() + 1 /* height of mode_str */), Constraint::Length(clock_widget.get_height() + 1 /* height of mode_str */),
); );
let [v1, v2] = let [v1, v2] =
Layout::vertical(Constraint::from_lengths([clock.get_height(), 1])).areas(area); Layout::vertical(Constraint::from_lengths([clock_widget.get_height(), 1])).areas(area);
clock.render(v1, buf, state.get_clock()); clock_widget.render(v1, buf, state.get_clock());
label.centered().render(v2, buf); label.centered().render(v2, buf);
} }
} }

View File

@ -12,6 +12,8 @@ use ratatui::{
}; };
use std::cmp::max; use std::cmp::max;
use super::clock::Style;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Timer { pub struct Timer {
clock: Clock<clock::Timer>, clock: Clock<clock::Timer>,
@ -63,23 +65,25 @@ impl EventHandler for Timer {
pub struct TimerWidget; pub struct TimerWidget;
impl StatefulWidget for &TimerWidget { impl StatefulWidget for &TimerWidget {
type State = Timer; type State = (Style, Timer);
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let clock = ClockWidget::new(); let style = state.0;
let label = Line::raw((format!("Timer {}", state.clock.get_mode())).to_uppercase()); let clock = &mut state.1.clock;
let clock_widget = ClockWidget::new(style);
let label = Line::raw((format!("Timer {}", clock.get_mode())).to_uppercase());
let area = center( let area = center(
area, area,
Constraint::Length(max( Constraint::Length(max(
clock.get_width(&state.clock.get_format()), clock_widget.get_width(&clock.get_format()),
label.width() as u16, label.width() as u16,
)), )),
Constraint::Length(clock.get_height() + 1 /* height of label */), Constraint::Length(clock_widget.get_height() + 1 /* height of label */),
); );
let [v1, v2] = let [v1, v2] =
Layout::vertical(Constraint::from_lengths([clock.get_height(), 1])).areas(area); Layout::vertical(Constraint::from_lengths([clock_widget.get_height(), 1])).areas(area);
clock.render(v1, buf, &mut state.clock); clock_widget.render(v1, buf, clock);
label.centered().render(v2, buf); label.centered().render(v2, buf);
} }
} }