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
use self::core::*;
use super::*;
use crate::report::*;

const DEFAULT_INTERVAL_INFO_TITLE: &str = "Interval info:";
const DEFAULT_TASK_INFO_TITLE: &str = "Task info:";

#[derive(Debug, Clone)]
pub struct IntervalData<'a> {
  pub interval: &'a Interval,
  pub task: &'a [Node],
  pub title: &'a str,
}

impl IntervalData<'_> {
  pub fn default_title() -> &'static str {
    DEFAULT_INTERVAL_INFO_TITLE
  }
}

impl ToString for IntervalData<'_> {
  fn to_string(&self) -> String {
    unimplemented!()
  }
}

#[derive(Debug, Clone)]
pub struct NodeData<'a> {
  pub node: &'a [Node],
  pub title: &'a str,
}

impl NodeData<'_> {
  pub fn default_title() -> &'static str {
    DEFAULT_TASK_INFO_TITLE
  }
}

#[derive(Debug, Clone)]
pub struct IntervalCmdData<'a> {
  pub cmd_text: &'a str,
  pub interval: IntervalData<'a>,
}

#[derive(Debug, Clone)]
pub struct NodeCmdData<'a> {
  pub cmd_text: &'a str,
  pub node: NodeData<'a>,
}

#[derive(Debug, Clone)]
pub struct IntervalError<'a> {
  pub err_text: &'a str,
  pub interval: IntervalData<'a>,
}

pub trait Printer {
  fn interval_cmd(&self, d: &IntervalCmdData);
  fn node_cmd(&self, d: &NodeCmdData);
  fn error(&self, e: &str);
  fn interval_error(&self, d: &IntervalData, e: &str);
  fn cmd(&self, d: &str);
  fn report(&self, r: &Report);
  fn prompt(&self, p: &str);
  fn task_list(&self, tasks: impl Iterator<Item = Vec<Node>>);
}

pub trait Markdown {
  fn markdown(&self) -> String;
}

pub struct TermPrinter {
  style: AppStyle,
}

impl Default for TermPrinter {
  fn default() -> Self {
    TermPrinter {
      style: Default::default(),
    }
  }
}

impl Printer for TermPrinter {
  fn interval_cmd(&self, d: &IntervalCmdData) {
    self.cmd(d.cmd_text);
    println!();
    print_interval_info(&d.interval, &self.style.task);
  }
  fn node_cmd(&self, d: &NodeCmdData) {
    self.cmd(d.cmd_text);
    println!();
    print_node_info(&d.node, &self.style.task)
  }
  fn error(&self, e: &str) {
    println!("Error: {}", &self.style.error.apply_to(e));
  }
  fn interval_error(&self, d: &IntervalData, e: &str) {
    self.error(e);
    println!();
    print_interval_info(d, &self.style.task);
  }
  fn cmd(&self, d: &str) {
    println!("{}", &self.style.cmd.apply_to(d));
  }
  fn report(&self, r: &Report) {
    println!(
      "{}",
      self
        .style
        .report
        .text(&r.markdown(), self.style.screen_width)
    );
  }
  fn prompt(&self, p: &str) {
    println!("{}", p);
  }
  fn task_list(&self, tasks: impl Iterator<Item = Vec<Node>>) {
    print_task_list(tasks, &self.style.task_list);
  }
}

fn print_task_list<'a>(
  d: impl Iterator<Item = Vec<Node>>,
  s: &TaskListStyle,
) {
  for task in d {
    let last = task.last().unwrap();
    print!("[{}] ", s.id.apply_to(last.id));
    for (i, t) in task.iter().enumerate() {
      if i > 0 {
        print!(" > ");
      }
      print!("{}", s.name.apply_to(&t.label));
    }
    print!(" {} \n", format_datetime(&last.created));
  }
}

fn print_interval_info(d: &IntervalData, s: &TaskStyle) {
  println!("{}", d.title);
  print!("  Task: ");
  for (i, t) in d.task.iter().enumerate() {
    print!("{}", s.name.apply_to(&t.label));
    if i < d.task.len() - 1 {
      print!(" > ");
    }
  }
  println!();
  print!(
    "  Started: {}",
    s.start_time.apply_to(format_datetime(&d.interval.begin))
  );

  let dur = Utc::now() - d.interval.begin;

  if dur.num_seconds() > 2 {
    print!(" ({} ago)", s.time_span.apply_to(format_duration(&dur)));
  }

  if d.interval.end.is_some() {
    let e = d.interval.end.unwrap();
    print!(
      "\n  Stopped: {}",
      s.end_time.apply_to(format_datetime(&e))
    );
    let dur = Utc::now() - e;
    if dur.num_seconds() > 2 {
      print!(
        " ({} ago)",
        s.time_span.apply_to(format_duration(&dur))
      );
    }
  }

  println!();
}

fn print_node_info(d: &NodeData, s: &TaskStyle) {
  println!("{}", d.title);
  print!("  Task: ");
  for (i, t) in d.node.iter().enumerate() {
    print!("{}", s.name.apply_to(&t.label));
    if i < d.node.len() - 1 {
      print!(" > ");
    }
  }
  println!();
  print!(
    "  Created: {}",
    s.created_time
      .apply_to(format_datetime(&d.node.last().unwrap().created))
  );
  println!();
}