summaryrefslogtreecommitdiffstats
path: root/src/bin/khaleesi.rs
blob: dd564355f85aec2d2ab418dee82f40f6f8a75dcf (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
extern crate atty;
extern crate khaleesi;
extern crate stderrlog;

#[macro_use]
extern crate log;

use khaleesi::config::Config;
use khaleesi::defaults::*;
use khaleesi::actions::*;
use khaleesi::utils::fileutil as utils;

use std::env;
use std::path::{Path,PathBuf};

fn main() {
  stderrlog::new()
    .timestamp(stderrlog::Timestamp::Off)
    .verbosity(3)
    .init()
    .unwrap();
  //            0 => LevelFilter::Error,
  //            1 => LevelFilter::Warn,
  //            2 => LevelFilter::Info,
  //            3 => LevelFilter::Debug,
  //            _ => LevelFilter::Trace,

  let args: Vec<String> = env::args().collect();
  let config = Config::read_config();

  main_internal(&args[0], &args[1..], &config);
}

fn main_internal(binary_name: &str, args: &[String], config: &Config) {
  if args.is_empty() {
    print_usage(&binary_name)
  } else {
    match args[0].as_str() {
      "agenda" => action_agenda(config, &args[1..]),
      "cal" => cal::printcal(),
      "copy" => action_copy(&args[1..]),
      "new" => action_new(&args[1..]),
      "dbg" => cal::dbg(),
      "edit" => action_edit(&args[1..]),
      "index" => action_index(&args[1..]),
      "list" => action_list(&args[1..]),
      "modify" => action_modify(&args[1..]),
      "select" => action_select(&args[1..]),
      "seq" => action_sequence(&args[1..]),
      "pretty" => action_prettyprint(&args[1..]),
      "show" => action_show(&args[1..]),
      "unroll" => action_unroll(&args[1..]),
      _  => print_usage(&args[0])
    }
  }

}

fn print_usage(name: &str) {
  error!("Usage: {} index|select|list|agenda|copy|new|edit|show|cal|dbg", name)
}

fn action_sequence(args: &[String]) {
  seq::do_seq(args);
}

fn action_list(args: &[String]) {
  //lists from sequence file or stdin
  if let Some(mut input) = default_input() {
    list::list_by_args(&mut input, &args);
  }
}

fn action_modify(args: &[String]) {
  if let Some(mut input) = default_input() {
    modify::do_modify(&mut input, &args);
  }
}

fn action_show(args: &[String]) {
  if let Some(mut input) = default_input() {
    show::do_show(&mut input, &args);
  }
}

fn action_edit(args: &[String]) {
  if let Some(mut input) = default_input() {
    edit::do_edit(&mut input, &args);
  }
}

fn action_select(args: &[String]) {
  //selects from index
  select::select_by_args(args);
}

fn action_agenda(config: &Config, args: &[String]) {
  if args.is_empty() {
    if let Some(mut input) = default_input() {
      agenda::show_events(&config, &mut input);
    }
  } else {
    let file = &args[0];
    let filepath = Path::new(file);
    agenda::show_events(&config, &mut utils::read_lines_from_file(filepath).unwrap());
  }
}

fn action_unroll(args: &[String]) {
  let file = &args[0];
  let filepath = Path::new(file);
  unroll::do_unroll(filepath)
}

fn action_prettyprint(_args: &[String]) {
  if let Some(mut input) = default_input() {
    prettyprint::prettyprint(&mut input);
  }
}

fn action_index(mut args: &[String]) {
  let reindex = !args.is_empty() && args[0] == "--reindex";
  if reindex {
    args = &args[1..];
  }
  let indexpath = if args.is_empty() {
    get_caldir()
  } else {
    PathBuf::from(&args[0])
  };
  index::index_dir(&indexpath, reindex)
}

fn action_copy(args: &[String]) {
  if let Some(mut input) = default_input() {
    copy::do_copy(&mut input, &args);
  }
}

fn action_new(args: &[String]) {
  if let Some(mut input) = default_input() {
    new::do_new(&mut input, &args);
  }
}

fn default_input() -> Option<Box<dyn Iterator<Item = String>>> {
  if atty::isnt(atty::Stream::Stdin) {
    debug!("stdin");
    Some(Box::new(utils::read_lines_from_stdin().unwrap()))
  } else {
    match seq::read_seqfile() {
      Ok(sequence) => Some(Box::new(sequence)),
      Err(err) => {
        error!("{}", err);
        None
      }
    }

  }
}

#[cfg(test)]
mod tests {
  extern crate assert_fs;
  extern crate predicates;

  use self::assert_fs::prelude::*;
  use self::assert_fs::TempDir;

  use super::*;

  fn path_to(artifact: &str) -> PathBuf {
    [env!("CARGO_MANIFEST_DIR"), "testdata", artifact].iter().collect()
  }

  fn prepare_testdir(template: &str) -> TempDir {
    let testdir = TempDir::new().unwrap();
    testdir.child(".khaleesi/").copy_from(path_to(template), &["*"]).unwrap();
    testdir
  }

  fn run(testdir: &TempDir, args: &[&str], config: Option<Config>) {
    env::set_current_dir(testdir).unwrap();

    let config = config.unwrap_or_default();
    let args: Vec<String> = args.iter().map(|x| x.to_string()).collect();
    main_internal("khaleesi", &args, &config)
  }

  #[test]
  fn test_index() {
    let testdir = prepare_testdir("testdir");

    run(&testdir, &["index"], None);

    testdir.child(".khaleesi/index/2018-W50").assert("1544740200 .khaleesi/cal/twodaysacrossbuckets.ics\n");
    testdir.child(".khaleesi/index/2018-W51").assert("1544740200 .khaleesi/cal/twodaysacrossbuckets.ics\n");
  }
}