diff --git a/Cargo.toml b/Cargo.toml index 44fe63f..a407e45 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,13 @@ -[package] -name = "antidrift" -version = "0.1.0" -edition = "2021" - -[dependencies] -anyhow = "1.0.86" -ratatui = "0.27.0" -regex = "1.10.5" - -[target.'cfg(windows)'.dependencies] -winapi = { version = "0.3", features = ["winuser", "processthreadsapi"] } \ No newline at end of file +[package] +name = "antidrift" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1.0.86" +ratatui = "0.27.0" +regex = "1.10.5" +shellexpand = "3.1.0" + +[target.'cfg(windows)'.dependencies] +winapi = { version = "0.3", features = ["winuser", "processthreadsapi"] } diff --git a/src/constants.rs b/src/constants.rs index 9ebf483..b5850a8 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,14 +1,15 @@ pub const APP_TITLE: &str = "AntiDrift"; +pub const DEFAULT_DURATION: &str = "25"; pub const DURATION_TITLE: &str = "Duration"; pub const INTENTION_TITLE: &str = "Intention"; +pub const PAUSED: &str = "paused"; pub const PREVIOUS_SESSIONS_TITLE: &str = "Previous Sessions"; pub const PROVIDE_INTENTION: &str = "Provide intention! "; pub const PROVIDE_VALID_DURATION: &str = "Provide valid duration in minutes! "; pub const RATE_TITLES: &str = "Press 1, 2, 3 to rate titles!"; pub const READY_TO_START: &str = "Ready to start next session."; -pub const SESSION_PAUSED: &str = "Session is paused. Unpause with 'p'."; pub const SESSION_IN_PROGRESS: &str = "Session In-Progress"; +pub const SESSION_PAUSED: &str = "Session is paused. Unpause with 'p'."; pub const SESSION_STATS_TITLE: &str = "Session Stats"; +pub const STATUS_FILE: &str = "~/.antidrift_status"; pub const STATUS_TITLE: &str = "Status"; -pub const DEFAULT_DURATION: &str = "25"; -pub const PAUSED: &str = "paused"; diff --git a/src/main.rs b/src/main.rs index 7047a03..e520f76 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use anyhow::Result; +use shellexpand; use std::collections::HashMap; -use std::io::stdout; +use std::io::{stdout, Write}; use std::rc::Rc; use std::time::{Duration, Instant}; mod constants; @@ -137,6 +138,36 @@ impl App { self.session_ratings = session_stats_as_vec(&self.session_stats); } + fn cleanup(&self) { + let path = shellexpand::tilde(constants::STATUS_FILE).to_string(); + let _ = std::fs::remove_file(path); + } + + fn write_status(&self) { + let status = match self.state { + State::InProgress => format!( + "{} - {}", + self.user_intention, + duration_as_str(&self.session_remaining) + ), + State::Paused => format!( + "antidrift paused - {}", + duration_as_str(&self.session_remaining) + ), + _ => format!("antidrift inactive"), + }; + + let path = shellexpand::tilde(constants::STATUS_FILE).to_string(); + if let Ok(mut file) = std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(&path) + { + let _ = file.write_all(status.as_bytes()); + } + } + fn tick_50ms(&mut self) { match self.state { State::InputIntention | State::InputDuration => { @@ -172,6 +203,7 @@ impl App { fn tick_1s(&mut self) { self.last_tick_1s = Instant::now(); + self.write_status(); if self.state == State::Paused { self.user_duration = self.user_duration.saturating_add(Duration::from_secs(1)); @@ -261,6 +293,7 @@ fn main() -> Result<()> { app.handle_ticks(); } + app.cleanup(); disable_raw_mode()?; stdout().execute(LeaveAlternateScreen)?; Ok(()) @@ -450,7 +483,7 @@ fn ui(frame: &mut Frame, app: &App) { spans.push(span); } - if app.user_duration == Duration::ZERO { + if app.user_duration.is_zero() { let span = Span::styled( constants::PROVIDE_VALID_DURATION, Style::new().fg(Color::LightRed),