Write out status to file (resolves #6)
This commit is contained in:
@@ -7,6 +7,7 @@ edition = "2021"
|
|||||||
anyhow = "1.0.86"
|
anyhow = "1.0.86"
|
||||||
ratatui = "0.27.0"
|
ratatui = "0.27.0"
|
||||||
regex = "1.10.5"
|
regex = "1.10.5"
|
||||||
|
shellexpand = "3.1.0"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = { version = "0.3", features = ["winuser", "processthreadsapi"] }
|
winapi = { version = "0.3", features = ["winuser", "processthreadsapi"] }
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
pub const APP_TITLE: &str = "AntiDrift";
|
pub const APP_TITLE: &str = "AntiDrift";
|
||||||
|
pub const DEFAULT_DURATION: &str = "25";
|
||||||
pub const DURATION_TITLE: &str = "Duration";
|
pub const DURATION_TITLE: &str = "Duration";
|
||||||
pub const INTENTION_TITLE: &str = "Intention";
|
pub const INTENTION_TITLE: &str = "Intention";
|
||||||
|
pub const PAUSED: &str = "paused";
|
||||||
pub const PREVIOUS_SESSIONS_TITLE: &str = "Previous Sessions";
|
pub const PREVIOUS_SESSIONS_TITLE: &str = "Previous Sessions";
|
||||||
pub const PROVIDE_INTENTION: &str = "Provide intention! ";
|
pub const PROVIDE_INTENTION: &str = "Provide intention! ";
|
||||||
pub const PROVIDE_VALID_DURATION: &str = "Provide valid duration in minutes! ";
|
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 RATE_TITLES: &str = "Press 1, 2, 3 to rate titles!";
|
||||||
pub const READY_TO_START: &str = "Ready to start next session.";
|
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_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 SESSION_STATS_TITLE: &str = "Session Stats";
|
||||||
|
pub const STATUS_FILE: &str = "~/.antidrift_status";
|
||||||
pub const STATUS_TITLE: &str = "Status";
|
pub const STATUS_TITLE: &str = "Status";
|
||||||
pub const DEFAULT_DURATION: &str = "25";
|
|
||||||
pub const PAUSED: &str = "paused";
|
|
||||||
|
|||||||
37
src/main.rs
37
src/main.rs
@@ -1,6 +1,7 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use shellexpand;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::io::stdout;
|
use std::io::{stdout, Write};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
mod constants;
|
mod constants;
|
||||||
@@ -137,6 +138,36 @@ impl App {
|
|||||||
self.session_ratings = session_stats_as_vec(&self.session_stats);
|
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) {
|
fn tick_50ms(&mut self) {
|
||||||
match self.state {
|
match self.state {
|
||||||
State::InputIntention | State::InputDuration => {
|
State::InputIntention | State::InputDuration => {
|
||||||
@@ -172,6 +203,7 @@ impl App {
|
|||||||
|
|
||||||
fn tick_1s(&mut self) {
|
fn tick_1s(&mut self) {
|
||||||
self.last_tick_1s = Instant::now();
|
self.last_tick_1s = Instant::now();
|
||||||
|
self.write_status();
|
||||||
|
|
||||||
if self.state == State::Paused {
|
if self.state == State::Paused {
|
||||||
self.user_duration = self.user_duration.saturating_add(Duration::from_secs(1));
|
self.user_duration = self.user_duration.saturating_add(Duration::from_secs(1));
|
||||||
@@ -261,6 +293,7 @@ fn main() -> Result<()> {
|
|||||||
app.handle_ticks();
|
app.handle_ticks();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.cleanup();
|
||||||
disable_raw_mode()?;
|
disable_raw_mode()?;
|
||||||
stdout().execute(LeaveAlternateScreen)?;
|
stdout().execute(LeaveAlternateScreen)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -450,7 +483,7 @@ fn ui(frame: &mut Frame, app: &App) {
|
|||||||
spans.push(span);
|
spans.push(span);
|
||||||
}
|
}
|
||||||
|
|
||||||
if app.user_duration == Duration::ZERO {
|
if app.user_duration.is_zero() {
|
||||||
let span = Span::styled(
|
let span = Span::styled(
|
||||||
constants::PROVIDE_VALID_DURATION,
|
constants::PROVIDE_VALID_DURATION,
|
||||||
Style::new().fg(Color::LightRed),
|
Style::new().fg(Color::LightRed),
|
||||||
|
|||||||
Reference in New Issue
Block a user