1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
extern crate chrono;
extern crate clap;

use crate::configure::Configuration;
use crate::log::{Event, Item, LogController};
use crate::util::{check_for_ongoing_event, describe, display_events, warn};
use chrono::Local;
use clap::{App, SubCommand};

fn after_help() -> &'static str {
    "\
The done subcommand places a DONE timestamp in the job log. This is just a \
timestamp followed by a colon and the word 'DONE':

  2019  1  2 15 04 05:DONE

Generally one ends one task by beginning another, but you want to go off the clock \
you can use the done subcommand.

All prefixes of 'done' -- 'd', 'do', and 'don' -- are aliases."
}

pub fn cli(mast: App<'static, 'static>, display_order: usize) -> App<'static, 'static> {
    mast.subcommand(
        SubCommand::with_name("done")
            .aliases(&["d", "do", "don"])
            .about("Ends a currently open task")
            .after_help(after_help())
            .display_order(display_order),
    )
}

pub fn run(directory: Option<&str>) {
    let conf = Configuration::read(None, directory);
    let mut reader = LogController::new(None, &conf).expect("could not read log");
    if let Some(event) = reader.last_event() {
        check_for_ongoing_event(&mut reader, &conf);
        if event.ongoing() {
            let (done, offset) = reader.close_event();
            describe(
                "ending",
                Some(&event.description),
                Item::Done(done, offset),
                &conf,
            );
        } else {
            warn("the most recent event is not ongoing", &conf);
            let now = Local::now().naive_local();
            let start = &event.start.clone();
            let event = Event::gather_by_day(vec![event], &now);
            println!();
            display_events(event, start, &now, &conf);
            println!();
            warn("no change to log", &conf)
        }
    } else {
        warn("there is currently no event in the log", &conf)
    }
}