From 5dc057f6243e9abd15436ae14118953e191ad685 Mon Sep 17 00:00:00 2001 From: 4o1x5 <4o1x5@4o1x5.dev> Date: Wed, 22 May 2024 00:24:51 +0200 Subject: [PATCH] enchantments: now double digit counter, but its code is janky needs fix melody has now became an option, by default it doesn not play it use -m /path/to/melody.mp3 flake changes --- README.md | 2 +- flake.nix | 5 ----- src/app.rs | 14 +++++++++++--- src/main.rs | 9 +++++++-- src/tests/mod.rs | 8 ++++++++ src/ui.rs | 26 +++++++++++++++++++++++--- 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 12bab02..cdc5b3a 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Options: - [ ] Styling - [ ] Custom sounds (via .config) - [x] Nix flake -- [ ] double digit counter for number below 10 +- [x] double digit counter for number below 10 # Develop diff --git a/flake.nix b/flake.nix index 9937bdd..eb76131 100644 --- a/flake.nix +++ b/flake.nix @@ -50,11 +50,6 @@ pkgs.cmake ]; cargoLock.lockFile = ./Cargo.lock; - installPhase = '' - # copy the sound file - mkdir -p $out - cp $src/src/assets/melody.mp3 $out - ''; }; }); diff --git a/src/app.rs b/src/app.rs index af0f4ad..312dbb1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -28,10 +28,11 @@ pub struct App { pub current_session: Session, pub sessions: Vec, + pub melody_path: String } impl App { - pub fn new(sl: i8, pl: i8, lpl: i8) -> Self { + pub fn new(sl: i8, pl: i8, lpl: i8, mldpth: String) -> Self { Self { running: true, start: Local::now(), @@ -39,16 +40,22 @@ impl App { pause_length: Duration::minutes(pl as i64), long_pause_length: Duration::minutes(lpl as i64), current_session: Session::Work, + melody_path: mldpth, sessions: vec![], } } pub fn tick(&mut self) { + // Notification logic... + let melody_path = self.melody_path.clone(); if self.current_time_left() <= 0 { - thread::spawn( || { + + // play the melody + if !melody_path.is_empty(){ + thread::spawn( move || { let (_stream, stream_handle) = OutputStream::try_default().unwrap(); // TODO add this to the app object instead of opening it every interval (ram) - let file = match File::open("./melody.mp3") { + match File::open(melody_path) { Ok(file) => { let file = BufReader::new(file); let source = Decoder::new(file).unwrap(); @@ -61,6 +68,7 @@ impl App { // FIXME increase the sleep based on the length of the sound std::thread::sleep(std::time::Duration::from_secs(5)); }); + } match self.current_session { Session::Pause => { diff --git a/src/main.rs b/src/main.rs index 6cfce46..1f5b972 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,13 +22,18 @@ struct Args { /// The length of a long pause (after 4 cycles) #[arg(short, long, default_value_t = 20)] long_pause: i8, + + + #[arg(short, long, default_value="")] + melody_path: String } mod tests; #[tokio::main] async fn main() -> AppResult<()> { - let args = Args::parse(); - let mut app = App::new(args.work, args.pause, args.long_pause); + let mut args = Args::parse(); + + let mut app = App::new(args.work, args.pause, args.long_pause, args.melody_path); // Initialize the terminal user interface. let backend = CrosstermBackend::new(io::stderr()); diff --git a/src/tests/mod.rs b/src/tests/mod.rs index c87a480..9612e78 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -23,6 +23,7 @@ pub fn test_longpause() { pause_length: Duration::minutes(1), long_pause_length: Duration::minutes(25), current_session: Session::Work, + melody_path: "".into(), sessions: vec![ Session::Work, Session::Pause, @@ -50,6 +51,7 @@ pub fn calculate_total_minutes_paused() { session_length: Duration::minutes(1), pause_length: Duration::minutes(1), long_pause_length: Duration::minutes(25), + melody_path: "".into(), current_session: Session::Work, sessions: vec![Session::Work, Session::Pause, Session::Work, Session::Pause], }; @@ -63,6 +65,7 @@ pub fn calculate_total_minutes_worked() { start: Local::now(), session_length: Duration::minutes(1), pause_length: Duration::minutes(1), + melody_path: "".into(), long_pause_length: Duration::minutes(25), current_session: Session::Work, sessions: vec![Session::Work, Session::Pause, Session::Work, Session::Pause], @@ -78,6 +81,7 @@ pub fn calculate_total_minutes_paused_with_longpause() { session_length: Duration::minutes(1), pause_length: Duration::minutes(5), long_pause_length: Duration::minutes(25), + melody_path: "".into(), current_session: Session::Work, sessions: vec![ Session::Work, @@ -103,6 +107,7 @@ fn test_time_left() { running: true, start: Local::now(), session_length: Duration::minutes(1), + melody_path: "".into(), pause_length: Duration::minutes(1), long_pause_length: Duration::minutes(25), current_session: Session::Work, @@ -118,6 +123,7 @@ fn test_time_left_with_minus() { start: Local::now() - Duration::seconds(60), session_length: Duration::minutes(1), pause_length: Duration::minutes(1), + melody_path: "".into(), long_pause_length: Duration::minutes(25), current_session: Session::Work, sessions: vec![], @@ -133,6 +139,7 @@ fn test_time_spent() { session_length: Duration::minutes(1), pause_length: Duration::minutes(1), long_pause_length: Duration::minutes(25), + melody_path: "".into(), current_session: Session::Work, sessions: vec![], }; @@ -148,6 +155,7 @@ pub fn pause() { pause_length: Duration::minutes(1), long_pause_length: Duration::minutes(25), current_session: Session::Pause, + melody_path: "".into(), sessions: vec![ Session::Work, Session::Pause, diff --git a/src/ui.rs b/src/ui.rs index 1aee90d..4d47f9c 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -16,9 +16,26 @@ pub fn render(app: &mut App, frame: &mut Frame) { let current_left = app.current_time_left(); - // TODO add double digit counter to minutes and seconds + let minutes = current_left / 60; let seconds = current_left % 60; + // TODO a better implementation of this..... sorry i wanted it quick and dirty + let minutes_str: String; + if minutes <= 9 { + minutes_str = format!("0{}", minutes.to_string()) + }else { + minutes_str = minutes.to_string() + }; + + let seconds_str: String; + if seconds <= 9 { + seconds_str = format!("0{}", seconds.to_string()) + }else { + seconds_str = seconds.to_string() + }; + + + let block = Block::bordered() .title( @@ -36,9 +53,12 @@ pub fn render(app: &mut App, frame: &mut Frame) { ) .border_type(BorderType::Rounded); + // FIXME stupid way of calculating the width of the text, but its good enough - let counter_text = format!("{}:{}", minutes, seconds); - let counter_text_size = format!("{}{}", minutes, seconds).len() * 10; + let counter_text = format!("{}:{}", minutes_str, seconds_str); + + + let counter_text_size = format!("{}{}", minutes_str, seconds_str).len() * 10; let big_text = BigText::builder() .pixel_size(PixelSize::Full) .style(Style::new().blue())