Files
termato/src/main.rs
T

401 lines
11 KiB
Rust
Raw Normal View History

use std::{
io::{self, stdout},
sync::mpsc,
thread,
time::Duration,
};
use crossterm::{
ExecutableCommand,
event::{self, Event, KeyCode, KeyModifiers},
terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};
use figlet_rs::Toilet;
use notify_rust::Notification;
use ratatui::{
Terminal,
backend::CrosstermBackend,
layout::{Alignment, Constraint, Direction, Layout},
style::{Color, Style},
widgets::{Block, Borders, Clear, Paragraph},
};
const SECONDS_PER_MIN: i64 = 60;
#[derive(Debug, PartialEq, Clone)]
enum TimerState {
Work,
Break,
Paused,
}
fn send_desktop_notification(title: &str, body: &str) {
let title = title.to_string();
let body = body.to_string();
std::thread::spawn(move || {
let _ = Notification::new()
.summary(&title)
.body(&body)
.appname("termato")
.show();
});
}
struct Termato {
work_mins: i64,
break_mins: i64,
state: TimerState,
prior_state: TimerState,
is_running: bool,
duration_in_secs: i64,
time_remaining_in_sec: i64,
2026-07-22 22:24:48 -05:00
show_help: bool,
}
impl Termato {
fn new(work_mins: i64, break_mins: i64) -> Self {
Termato {
work_mins,
break_mins,
state: TimerState::Work,
prior_state: TimerState::Work,
is_running: false,
duration_in_secs: work_mins * SECONDS_PER_MIN,
time_remaining_in_sec: work_mins * SECONDS_PER_MIN,
2026-07-22 22:24:48 -05:00
show_help: false,
}
}
fn toggle_running(&mut self) {
if self.is_running {
self.prior_state = self.state.clone();
self.state = TimerState::Paused;
self.is_running = false;
} else {
self.state = self.prior_state.clone();
self.is_running = true;
}
}
fn toggle_mode(&mut self) {
let is_on_break = self.state == TimerState::Break
|| (self.state == TimerState::Paused && self.prior_state == TimerState::Break);
if is_on_break {
self.state = TimerState::Work;
self.prior_state = TimerState::Work;
self.duration_in_secs = self.work_mins * SECONDS_PER_MIN;
} else {
self.state = TimerState::Break;
self.prior_state = TimerState::Break;
self.duration_in_secs = self.break_mins * SECONDS_PER_MIN;
}
self.time_remaining_in_sec = self.duration_in_secs;
}
fn tick(&mut self) {
if self.is_running && self.time_remaining_in_sec > 0 {
self.time_remaining_in_sec -= 1;
if self.time_remaining_in_sec == 0 {
if self.state == TimerState::Work {
send_desktop_notification("Work Done", "Time to take a break.");
} else {
send_desktop_notification("Break Over", "Time to focus.");
}
self.toggle_mode();
}
}
}
fn reset(&mut self) {
self.is_running = false;
self.state = TimerState::Work;
self.prior_state = TimerState::Work;
self.duration_in_secs = self.work_mins * SECONDS_PER_MIN;
self.time_remaining_in_sec = self.duration_in_secs;
}
2026-07-22 22:24:48 -05:00
fn toggle_help(&mut self) {
self.show_help = !self.show_help;
}
}
fn main() -> Result<(), io::Error> {
enable_raw_mode()?;
stdout().execute(EnterAlternateScreen)?;
let backend = CrosstermBackend::new(stdout());
let mut terminal = Terminal::new(backend)?;
let font = Toilet::smblock().unwrap();
let mut termato = Termato::new(1, 1);
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
loop {
thread::sleep(Duration::from_secs(1));
if tx.send(()).is_err() {
break;
}
}
});
loop {
terminal.draw(|f| {
let size = f.area();
2026-07-22 22:24:48 -05:00
let minutes = termato.time_remaining_in_sec / SECONDS_PER_MIN;
let seconds = termato.time_remaining_in_sec % SECONDS_PER_MIN;
let time_str = format!("{:02}:{:02}", minutes, seconds);
2026-07-22 22:24:48 -05:00
let time_color = match termato.state {
TimerState::Work => Color::Red,
TimerState::Break => Color::Green,
TimerState::Paused => Color::Yellow,
};
let big_time_text = if let Some(fig) = font.convert(&time_str) {
fig.to_string()
} else {
time_str
};
let text_height = big_time_text.lines().count() as u16;
let vertical_layout = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Min(0),
Constraint::Length(text_height),
Constraint::Min(0),
])
.split(size);
let timer_paragraph = Paragraph::new(big_time_text)
.style(Style::default().fg(time_color))
.alignment(Alignment::Center);
f.render_widget(timer_paragraph, vertical_layout[1]);
2026-07-22 22:24:48 -05:00
if termato.show_help {
let popup_width = 44;
let popup_height = 7;
if size.width >= popup_width && size.height >= popup_height {
let popup_vertical = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length((size.height.saturating_sub(popup_height)) / 2),
Constraint::Length(popup_height),
Constraint::Min(1),
])
.split(size);
let popup_horizontal = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Length((size.width.saturating_sub(popup_width)) / 2),
Constraint::Length(popup_width),
Constraint::Min(1),
])
.split(popup_vertical[1]);
let popup_area = popup_horizontal[1];
let help_block = Block::default()
.title(" Controls ")
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::DarkGray));
let help_content = Paragraph::new(
"[Space] Pause / Resume\n\
[S] Switch Mode\n\
[R] Reset Timer\n\
[?] Toggle Help\n\
[Q] Quit",
)
.block(help_block)
.style(Style::default().fg(Color::DarkGray))
.alignment(Alignment::Left);
f.render_widget(Clear, popup_area);
f.render_widget(help_content, popup_area);
}
}
})?;
if event::poll(Duration::from_millis(100))?
&& let Event::Key(key) = event::read()?
&& key.kind == event::KeyEventKind::Press
{
match key.code {
KeyCode::Char('q') | KeyCode::Char('Q') => break,
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => break,
KeyCode::Char(' ') => termato.toggle_running(),
KeyCode::Char('s') | KeyCode::Char('S') => termato.toggle_mode(),
KeyCode::Char('r') | KeyCode::Char('R') => termato.reset(),
2026-07-22 22:24:48 -05:00
KeyCode::Char('?') => termato.toggle_help(),
_ => {}
}
}
if rx.try_recv().is_ok() {
termato.tick();
}
}
disable_raw_mode()?;
stdout().execute(LeaveAlternateScreen)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn termato_when_called_it_should_return_expected_starting_state() {
let result = Termato::new(25, 5);
assert_eq!(result.work_mins, 25);
assert_eq!(result.break_mins, 5);
assert_eq!(result.state, TimerState::Work);
assert_eq!(result.prior_state, TimerState::Work);
assert!(!result.is_running);
assert_eq!(result.duration_in_secs, 1500);
assert_eq!(result.time_remaining_in_sec, 1500);
}
#[test]
fn termato_tick_when_called_and_not_running_it_should_do_nothing() {
let mut termato = Termato::new(1, 1);
termato.tick();
assert_eq!(termato.time_remaining_in_sec, 60);
}
#[test]
fn termato_tick_when_called_and_no_time_remaining_it_should_do_nothing() {
let mut termato = Termato::new(1, 1);
termato.is_running = true;
termato.time_remaining_in_sec = 0;
termato.tick();
assert_eq!(termato.time_remaining_in_sec, 0);
}
#[test]
fn termato_tick_when_running_and_time_reamining_it_should_reduce_remaining_by_one() {
let mut termato = Termato::new(1, 1);
termato.is_running = true;
termato.tick();
assert_eq!(termato.time_remaining_in_sec, 59);
}
#[test]
fn termato_tick_when_running_on_work_and_time_remaining_reaches_zero_it_should_toggle_to_break() {
let mut termato = Termato::new(1, 2);
termato.is_running = true;
termato.time_remaining_in_sec = 1;
termato.tick();
assert_eq!(termato.state, TimerState::Break);
assert_eq!(termato.prior_state, TimerState::Break);
assert_eq!(termato.duration_in_secs, 120);
assert_eq!(termato.time_remaining_in_sec, 120);
}
#[test]
fn termato_tick_when_running_on_break_and_time_remaining_reaches_zero_it_should_toggle_to_work() {
let mut termato = Termato::new(2, 1);
termato.state = TimerState::Break;
termato.is_running = true;
termato.time_remaining_in_sec = 1;
termato.tick();
assert_eq!(termato.state, TimerState::Work);
assert_eq!(termato.prior_state, TimerState::Work);
assert_eq!(termato.duration_in_secs, 120);
assert_eq!(termato.time_remaining_in_sec, 120);
}
#[test]
fn termato_toggle_mode_when_paused_and_previous_state_is_break_it_should_toggle_to_work() {
let mut termato = Termato::new(2, 1);
termato.state = TimerState::Paused;
termato.prior_state = TimerState::Break;
termato.toggle_mode();
assert_eq!(termato.state, TimerState::Work);
assert_eq!(termato.prior_state, TimerState::Work);
assert_eq!(termato.duration_in_secs, 120);
assert_eq!(termato.time_remaining_in_sec, 120);
}
#[test]
fn termato_toggle_running_when_called_and_already_running_it_should_pause() {
let mut termato = Termato::new(1, 1);
termato.is_running = true;
termato.toggle_running();
assert_eq!(termato.state, TimerState::Paused);
assert_eq!(termato.prior_state, TimerState::Work);
assert!(!termato.is_running)
}
#[test]
fn termato_toggle_running_when_called_and_already_paused_it_should_run() {
let mut termato = Termato::new(1, 1);
termato.toggle_running();
assert_eq!(termato.state, TimerState::Work);
assert_eq!(termato.prior_state, TimerState::Work);
assert!(termato.is_running)
}
#[test]
fn termato_rest_when_called_it_should_reset_the_apps_state() {
let mut termato = Termato::new(1, 1);
termato.is_running = true;
termato.state = TimerState::Break;
termato.prior_state = TimerState::Break;
termato.duration_in_secs = 1;
termato.time_remaining_in_sec = 1;
termato.reset();
assert!(!termato.is_running);
assert_eq!(termato.state, TimerState::Work);
assert_eq!(termato.prior_state, TimerState::Work);
assert_eq!(termato.duration_in_secs, 60);
assert_eq!(termato.time_remaining_in_sec, 60);
}
2026-07-22 22:24:48 -05:00
#[test]
fn termato_toggle_help_when_called_it_should_toggle_help() {
let mut termato = Termato::new(1, 1);
termato.toggle_help();
assert!(termato.show_help);
termato.toggle_help();
assert!(!termato.show_help);
}
2026-07-22 18:25:02 -05:00
}