Skip to content

Commit

Permalink
add --watch option to status
Browse files Browse the repository at this point in the history
  • Loading branch information
frapa committed Feb 29, 2020
1 parent 8b7acee commit 2a6bb9a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
39 changes: 33 additions & 6 deletions src/basic_op.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::{thread, time};
use std::io::prelude::*;

use colored::*;
use chrono;

Expand Down Expand Up @@ -80,13 +83,37 @@ fn confirm_stop_current(time: chrono::DateTime<chrono::Utc>) -> bool {
}
}

pub fn status_command() {
match timers::get_current_log_task() {
Ok(task) => match task {
Some(task) => print_status(&task),
None => println!("You are not logging on any task.")
pub fn status_command(matches: &clap::ArgMatches) {
let minutes = match matches.value_of("watch") {
Some(val) => match parse_int(val) {
Ok(val) => val,
Err(_) => {
println!("Invalid watch interval '{}'", val);
return;
},
},
Err(err) => println!("Error finding current task: {}", err),
None => 1,
} as u64;

loop {
if matches.occurrences_of("watch") != 0 {
print!("\x1B[H\x1B[2J\r");
}

match timers::get_current_log_task() {
Ok(task) => match task {
Some(task) => print_status(&task),
None => print!("You are not logging on any task."),
},
Err(err) => print!("Error finding current task: {}", err),
};
std::io::stdout().flush().unwrap();

if matches.occurrences_of("watch") == 0 {
break
}

thread::sleep(time::Duration::from_secs(minutes * 60));
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {

match matches.subcommand_name() {
Some("log") => log_command(matches.subcommand_matches("log").unwrap()),
Some("status") => status_command(),
Some("status") => status_command(matches.subcommand_matches("status").unwrap()),
Some("stop") => stop_command(matches.subcommand_matches("stop").unwrap()),
Some("report") => {
let submatches = matches.subcommand_matches("report").unwrap();
Expand Down Expand Up @@ -54,6 +54,15 @@ fn parse_args() -> clap::ArgMatches<'static> {
)
.subcommand(clap::SubCommand::with_name("status")
.about("Get logging status")
.arg(clap::Arg::with_name("watch")
.short("w")
.long("watch")
.takes_value(true)
.min_values(0)
.max_values(1)
.default_value("1")
.help("Keep watching the status, for a GUI like effect.")
)
)
.subcommand(clap::SubCommand::with_name("stop")
.about("Stop logging time on the current task")
Expand All @@ -68,7 +77,7 @@ fn parse_args() -> clap::ArgMatches<'static> {
.subcommand(clap::SubCommand::with_name("report")
.about("Report statistics on the tasks")
.subcommand(clap::SubCommand::with_name("days")
.about("Report statistics on days")
.about("Report statistics on days.")
)
.arg(clap::Arg::with_name("no-tot")
.long("--no-tot")
Expand All @@ -80,7 +89,7 @@ fn parse_args() -> clap::ArgMatches<'static> {
.arg(clap::Arg::with_name("long")
.short("-l")
.long("--long")
.help("Display more information for each task")
.help("Display more information for each task.")
)
)
.get_matches();
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ pub fn parse_duration(raw_duration: &str) -> Option<chrono::Duration> {
}
}

fn parse_int(text: &str) -> Result<i64, ParseIntError> {
pub fn parse_int(text: &str) -> Result<i64, ParseIntError> {
Ok(text.trim().parse::<i64>()?)
}

0 comments on commit 2a6bb9a

Please sign in to comment.