Write out status to file (resolves #6)
This commit is contained in:
37
src/main.rs
37
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),
|
||||
|
||||
Reference in New Issue
Block a user