commit b36bb109a68f8dce97fb5775e43a0a17e49e74db Author: Marcel Schneider Date: Wed Jun 5 16:14:35 2019 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..6e15d5a --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,91 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "autocfg" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "chrono" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.58" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "num-integer" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pom" +version = "0.1.0" +dependencies = [ + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "redox_syscall" +version = "0.1.54" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "time" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf" +"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" +"checksum libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "6281b86796ba5e4366000be6e9e18bf35580adf9e63fbe2294aadb587613a319" +"checksum num-integer 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "b85e541ef8255f6cf42bbfe4ef361305c6c135d10919ecc26126c4e5ae94bc09" +"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32" +"checksum redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)" = "12229c14a0f65c4f1cb046a3b52047cdd9da1f4b30f8a39c5063c8bae515e252" +"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +"checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c5efe5f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "pom" +version = "0.1.0" +authors = ["Marcel Schneider "] +edition = "2018" +repository = "https://git.schneider-hosting.de/schneider/pom" +license = "MIT/Apache-2.0" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +chrono = "0.4" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b4ed8d6 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,99 @@ +use chrono::prelude::*; +use chrono::Duration; +use std::io::{stdout, Result, StdoutLock, Write}; +use std::process::{Command, Output}; +use std::thread::sleep; + +const TIME_FORMAT: &'static str = "%H:%M:%S"; + +fn main() { + run().unwrap(); +} + +/// Runs the application +/// +/// Default time settings are 25 minutes for a working block and 5 minutes for a break. +fn run() -> Result<()> { + let work_duration = Duration::seconds(if cfg!(debug_assertions) { 5 } else { 25 * 60 }); + let break_duration = Duration::seconds(if cfg!(debug_assertions) { 5 } else { 5 * 60 }); + + loop { + timer( + "Work", + "The working time is over, have a break now!", + &work_duration, + )?; + + timer( + "Break", + "The break is over, get back to work!", + &break_duration, + )?; + } +} + +/// Handles a timer block +/// +/// # Arguments +/// +/// * name - The name for this block, e.g. 'Work' or 'Break' +/// * after_msg - The message to be displayed by the Dialog window at the end of the time +/// * duration - How long this block shall be +fn timer(name: &str, after_msg: &str, duration: &Duration) -> Result<()> { + let out = stdout(); + let mut out = out.lock(); + let start = Local::now(); + writeln!(out, "{} start {}", name, start.format(TIME_FORMAT))?; + + loop { + reset_line(&mut out)?; + let elapsed: Duration = Local::now().signed_duration_since(start); + + if elapsed >= *duration { + break; + } + + let elapsed_time: NaiveTime = NaiveTime::from_hms( + elapsed.num_hours() as u32, + elapsed.num_minutes() as u32, + elapsed.num_seconds() as u32, + ); + write!(out, "Time elapsed: {}", elapsed_time.format(TIME_FORMAT))?; + out.flush().unwrap(); + + sleep(Duration::seconds(1).to_std().unwrap()); + } + + let now = Local::now(); + writeln!(out, "{} end {}", name, now.format(TIME_FORMAT))?; + dialog(after_msg).unwrap(); + Ok(()) +} + +/// Shows a dialog +/// +/// Currently only supports KDE dialogs via command line program `kdialgo`. +/// +/// # Arguments +/// +/// * text - The text to be shown in the Dialog +fn dialog(text: &str) -> Result { + Command::new("sh") + .arg("-c") + .arg(format!( + "kdialog --title 'Pomodoro Timer' --msgbox '{}'", + text + )) + .output() +} + +/// Resets the current line on stdout +/// +/// Works for meĀ® but is probably unportable as hell. +/// +/// # Arguments +/// +/// * w - A reference to the locked stdout handle +pub fn reset_line(w: &mut StdoutLock) -> Result<()> { + write!(w, "\r[2K") +}