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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use crossterm::cursor;
use crossterm::style;
use crossterm::terminal;
use crossterm::QueueableCommand;
use parking_lot::Mutex;
use std::io::stderr;
use std::io::stdout;
use std::io::Stderr;
use std::io::Stdout;
use std::io::Write;
use std::sync::Arc;

pub enum LoggerTextItem {
  Text(String),
  HangingText { text: String, indent: u16 },
}

#[derive(PartialOrd, Ord, PartialEq, Eq)]
pub(crate) enum LoggerRefreshItemKind {
  // numbered by display order
  ProgressBars = 0,
  Selection = 1,
}

struct LoggerRefreshItem {
  kind: LoggerRefreshItemKind,
  text_items: Vec<LoggerTextItem>,
}

#[derive(Clone)]
pub struct Logger {
  output_lock: Arc<Mutex<LoggerState>>,
}

struct LoggerState {
  is_silent: bool,
  last_context_name: String,
  std_out: Stdout,
  std_err: Stderr,
  refresh_items: Vec<LoggerRefreshItem>,
  last_terminal_size: Option<(u16, u16)>,
}

impl Logger {
  pub fn new(initial_context_name: &str, is_silent: bool) -> Self {
    Logger {
      output_lock: Arc::new(Mutex::new(LoggerState {
        is_silent,
        last_context_name: initial_context_name.to_string(),
        std_out: stdout(),
        std_err: stderr(),
        refresh_items: Vec::new(),
        last_terminal_size: None,
      })),
    }
  }

  pub fn log(&self, text: &str, context_name: &str) {
    let mut state = self.output_lock.lock();
    if state.is_silent {
      return;
    }
    self.inner_log(&mut state, true, text, context_name);
  }

  pub fn log_bypass_silent(&self, text: &str, context_name: &str) {
    let mut state = self.output_lock.lock();
    self.inner_log(&mut state, true, text, context_name);
  }

  pub fn log_err(&self, text: &str, context_name: &str) {
    let mut state = self.output_lock.lock();
    self.inner_log(&mut state, false, text, context_name);
  }

  pub fn log_text_items(&self, text_items: &[LoggerTextItem], context_name: &str, terminal_width: Option<u16>) {
    let text = render_text_items_with_width(text_items, terminal_width);
    self.log(&text, context_name);
  }

  fn inner_log(&self, state: &mut LoggerState, is_std_out: bool, text: &str, context_name: &str) {
    if !state.refresh_items.is_empty() {
      self.inner_queue_clear_previous_draws(state);
    }

    let mut output_text = String::new();
    if state.last_context_name != context_name {
      output_text.push_str(&format!("[{}]\n", context_name));
      state.last_context_name = context_name.to_string();
    }

    output_text.push_str(text);

    // only add a newline if the logged text does not end with one
    if !output_text.ends_with('\n') {
      output_text.push('\n');
    }

    if is_std_out {
      state.std_out.queue(style::Print(output_text)).unwrap();
    } else {
      state.std_err.queue(style::Print(output_text)).unwrap();
    }

    if !state.refresh_items.is_empty() {
      self.inner_queue_draw_items(state);
    }

    if is_std_out {
      state.std_out.flush().unwrap();
      if !state.refresh_items.is_empty() {
        state.std_err.flush().unwrap();
      }
    } else {
      state.std_err.flush().unwrap();
    }
  }

  pub(crate) fn set_refresh_item(&self, kind: LoggerRefreshItemKind, text_items: Vec<LoggerTextItem>) {
    self.with_update_refresh_items(move |refresh_items| match refresh_items.binary_search_by(|i| i.kind.cmp(&kind)) {
      Ok(pos) => {
        let mut refresh_item = refresh_items.get_mut(pos).unwrap();
        refresh_item.text_items = text_items;
      }
      Err(pos) => {
        let refresh_item = LoggerRefreshItem { kind, text_items };
        refresh_items.insert(pos, refresh_item);
      }
    });
  }

  pub(crate) fn remove_refresh_item(&self, kind: LoggerRefreshItemKind) {
    self.with_update_refresh_items(move |refresh_items| {
      if let Ok(pos) = refresh_items.binary_search_by(|i| i.kind.cmp(&kind)) {
        refresh_items.remove(pos);
      } else {
        // already removed
      }
    });
  }

  fn with_update_refresh_items(&self, update_refresh_items: impl FnOnce(&mut Vec<LoggerRefreshItem>)) {
    let mut state = self.output_lock.lock();

    // hide the cursor if showing a refresh item for the first time
    if state.refresh_items.is_empty() {
      state.std_err.queue(cursor::Hide).unwrap();
    }

    self.inner_queue_clear_previous_draws(&mut state);

    update_refresh_items(&mut state.refresh_items);

    self.inner_queue_draw_items(&mut state);

    // show the cursor if no longer showing a refresh item
    if state.refresh_items.is_empty() {
      state.std_err.queue(cursor::Show).unwrap();
    }
    state.std_err.flush().unwrap();
  }

  fn inner_queue_clear_previous_draws(&self, state: &mut LoggerState) {
    let terminal_width = crate::terminal::get_terminal_width().unwrap();
    let text_items = state.refresh_items.iter().map(|item| item.text_items.iter()).flatten();
    let rendered_text = render_text_items_truncated_to_height(text_items, state.last_terminal_size);
    let last_line_count = get_text_line_count(&rendered_text, terminal_width);

    if last_line_count > 0 {
      if last_line_count > 1 {
        state.std_err.queue(cursor::MoveUp(last_line_count - 1)).unwrap();
      }
      state.std_err.queue(cursor::MoveToColumn(0)).unwrap();
      state.std_err.queue(terminal::Clear(terminal::ClearType::FromCursorDown)).unwrap();
    }

    state.std_err.queue(cursor::MoveToColumn(0)).unwrap();
  }

  fn inner_queue_draw_items(&self, state: &mut LoggerState) {
    let terminal_size = crate::terminal::get_terminal_size();
    let text_items = state.refresh_items.iter().map(|item| item.text_items.iter()).flatten();
    let rendered_text = render_text_items_truncated_to_height(text_items, terminal_size);
    state.std_err.queue(style::Print(&rendered_text)).unwrap();
    state.std_err.queue(cursor::MoveToColumn(0)).unwrap();
    state.last_terminal_size = terminal_size;
  }
}

/// Renders the text items with the specified width.
pub fn render_text_items_with_width(text_items: &[LoggerTextItem], terminal_width: Option<u16>) -> String {
  render_text_items_to_lines(text_items.iter(), terminal_width).join("\n")
}

fn render_text_items_truncated_to_height<'a>(text_items: impl Iterator<Item = &'a LoggerTextItem>, terminal_size: Option<(u16, u16)>) -> String {
  let lines = render_text_items_to_lines(text_items, terminal_size.map(|(width, _)| width));
  if let Some(height) = terminal_size.map(|(_, height)| height) {
    let max_height = height as usize;
    if lines.len() > max_height {
      return lines[lines.len() - max_height..].join("\n");
    }
  }
  lines.join("\n")
}

fn render_text_items_to_lines<'a>(text_items: impl Iterator<Item = &'a LoggerTextItem>, terminal_width: Option<u16>) -> Vec<String> {
  let mut result = Vec::new();
  for (_, item) in text_items.enumerate() {
    match item {
      LoggerTextItem::Text(text) => result.extend(render_text_to_lines(text, 0, terminal_width)),
      LoggerTextItem::HangingText { text, indent } => {
        result.extend(render_text_to_lines(text, *indent, terminal_width));
      }
    }
  }
  result
}

fn render_text_to_lines(text: &str, hanging_indent: u16, terminal_width: Option<u16>) -> Vec<String> {
  let mut lines = Vec::new();
  if let Some(terminal_width) = terminal_width {
    let mut current_line = String::new();
    let mut line_width: u16 = 0;
    let mut current_whitespace = String::new();
    for token in tokenize_words(text) {
      match token {
        WordToken::Word((word, word_width)) => {
          let is_word_longer_than_line = hanging_indent + word_width > terminal_width;
          if is_word_longer_than_line {
            // break it up onto multiple lines with indentation
            if !current_whitespace.is_empty() {
              if line_width < terminal_width {
                current_line.push_str(&current_whitespace);
              }
              current_whitespace = String::new();
            }
            for c in word.chars() {
              if line_width == terminal_width {
                lines.push(current_line);
                current_line = String::new();
                current_line.push_str(&" ".repeat(hanging_indent as usize));
                line_width = hanging_indent;
              }
              current_line.push(c);
              line_width += 1;
            }
          } else {
            if line_width + word_width > terminal_width {
              lines.push(current_line);
              current_line = String::new();
              current_line.push_str(&" ".repeat(hanging_indent as usize));
              line_width = hanging_indent;
              current_whitespace = String::new();
            }
            if !current_whitespace.is_empty() {
              current_line.push_str(&current_whitespace);
              current_whitespace = String::new();
            }
            current_line.push_str(word);
            line_width += word_width;
          }
        }
        WordToken::WhiteSpace(space_char) => {
          current_whitespace.push(space_char);
          line_width += 1;
        }
        WordToken::NewLine => {
          lines.push(current_line);
          current_line = String::new();
          line_width = 0;
        }
      }
    }
    if !current_line.is_empty() {
      lines.push(current_line);
    }
  } else {
    for line in text.lines() {
      lines.push(line.to_string());
    }
  }
  lines
}

enum WordToken<'a> {
  Word((&'a str, u16)),
  WhiteSpace(char),
  NewLine,
}

fn tokenize_words(text: &str) -> Vec<WordToken> {
  // todo: how to write an iterator version?
  let mut start_index = 0;
  let mut tokens = Vec::new();
  let mut word_width = 0;
  for (index, c) in text.char_indices() {
    if c.is_whitespace() || c == '\n' {
      if word_width > 0 {
        tokens.push(WordToken::Word((&text[start_index..index], word_width)));
        word_width = 0;
      }

      if c == '\n' {
        tokens.push(WordToken::NewLine);
      } else {
        tokens.push(WordToken::WhiteSpace(c));
      }

      start_index = index + c.len_utf8(); // start at next char
    } else if !c.is_ascii_control() {
      word_width += 1;
    }
  }
  if word_width > 0 {
    tokens.push(WordToken::Word((&text[start_index..text.len()], word_width)));
  }
  tokens
}

fn get_text_line_count(text: &str, terminal_width: u16) -> u16 {
  let mut line_count: u16 = 0;
  let mut line_width: u16 = 0;
  for c in text.chars() {
    if c.is_ascii_control() && !c.is_ascii_whitespace() {
      // ignore
    } else if c == '\n' {
      line_count += 1;
      line_width = 0;
    } else if line_width == terminal_width {
      line_width = 0;
      line_count += 1;
    } else {
      line_width += 1;
    }
  }
  line_count + 1
}