Browse Source

Initial commit

master
Schneider 5 years ago
commit
b36bb109a6
Signed by: schneider GPG Key ID: 3F50B02A50039F3B
  1. 2
      .gitignore
  2. 91
      Cargo.lock
  3. 12
      Cargo.toml
  4. 99
      src/main.rs

2
.gitignore

@ -0,0 +1,2 @@
/target
**/*.rs.bk

91
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"

12
Cargo.toml

@ -0,0 +1,12 @@
[package]
name = "pom"
version = "0.1.0"
authors = ["Marcel Schneider <marcel@webschneider.org>"]
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"

99
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<Output> {
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")
}
Loading…
Cancel
Save