diff --git a/Cargo.toml b/Cargo.toml index 58dfd8c..9e487e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [package] name = "i3status-custom-pim" +description = "Generates JSON output for use with custom block of i3status-rust." version = "0.1.0" authors = ["Martin Bley "] edition = "2018" @@ -7,4 +8,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +structopt = "0.3" chrono = "0.4" diff --git a/src/main.rs b/src/main.rs index fedd7db..67c69e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,67 +1,21 @@ -use chrono::{Local, NaiveTime}; -use std::process::Command; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +#[structopt( author, about)] +struct Cli { + #[structopt(short = "k", help="output khal events")] + khal: bool, + #[structopt(short = "t", help="output todos")] + todo: bool, +} fn main() { - const THRESHOLD_WARNING: i64 = 60; - const THRESHOLD_CRITICAL: i64 = 15; - let icon = String::from("calendar"); + let args = Cli::from_args(); - let mut events: Vec = Vec::new(); - let now = Local::now().time(); - let from = now.format("%H:%M").to_string(); - - let khal_cmd = Command::new("khal") - .arg("list") - .arg("-df") - .arg("{name}") - .arg("-f") - .arg("{start-time}") - .arg("--notstarted") - .arg(&from) - .arg("23:59") - .output(); - - let khal_output = khal_cmd.expect("failed to run command"); - let khal_stdout = String::from_utf8(khal_output.stdout) - .expect("can't read output"); - - let mut lines = khal_stdout.lines(); - let dayline = lines.nth(0).expect("output seems empty"); - - if dayline.trim() == "Today" { - for e in lines { - events.push(e.to_string()); - } + if args.khal && args.todo { + println!("khal"); } - // get duration up to next event and set state - let mut state = String::from("Idle"); + println!("{:?}", args); - let mut event_remaining: i64 = 24 * 60; - let event_count = events.len(); - for e in events.iter() { - let e_start = match NaiveTime::parse_from_str(e, "%H:%M") { - Ok(s) => s, - Err(_f) => NaiveTime::from_hms(0, 0, 0), - }; - let diff = e_start - now; - if (diff.num_minutes() < event_remaining) && - (diff.num_minutes() >= 0) { - event_remaining = diff.num_minutes() - } - - if event_remaining >= 0 { - if event_remaining <= THRESHOLD_WARNING { - state = String::from("Warning"); - } - if event_remaining <= THRESHOLD_CRITICAL { - state = String::from("Critical"); - } - } - } - - println!( - "{{ \"icon\": \"{}\", \"state\": \"{}\", \"text\": \"{}\" }}", - icon, state, event_count - ); }