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
use crate::{
  Ed,
  ui::UI,
  ui::UILock,
};
use super::Result;

/// Struct describing a print invocation.
#[derive(Debug, PartialEq)]
pub struct Print {
  /// The lines printed. Each string is a separate line.
  pub text: Vec<String>,
  /// If true, would have printed with line numbers.
  pub n: bool,
  /// If true, would have printed with literal escapes.
  pub l: bool,
}

/// A mock UI that logs all prints and panics when asked for input.
pub struct MockUI {
  pub prints_history: Vec<Print>,
}

impl UI for MockUI {
  fn print_message(
    &mut self,
    data: &str,
  ) -> Result<()> {
    self.prints_history.push(
      Print{
        text: vec![data.to_owned()],
        n: false,
        l: false,
      }
    );
    Ok(())
  }

  fn print_command_documentation(&mut self) -> Result<()> {
    self.print_message(crate::messages::COMMAND_DOCUMENTATION)
  }

  fn print_selection(
    &mut self,
    ed: &Ed,
    selection: (usize, usize),
    numbered: bool,
    literal: bool,
  ) -> Result<()> {
    self.prints_history.push(
      Print{
        text: ed.history.current().get_lines(selection)?
          .map(|s| s.to_string())
          .collect()
        ,
        n: numbered,
        l: literal,
      }
    );
    Ok(())
  }

  fn get_command(
    &mut self,
    _ed: &Ed,
    _prefix: Option<char>,
  ) -> Result<String> {
    panic!("get_command not implemented on mock ui")
  }

  fn get_input(
    &mut self,
    _ed: &Ed,
    _terminator: char,
    #[cfg(feature = "initial_input_data")]
    _initial_buffer: Option<Vec<String>>
  ) -> Result<Vec<String>> {
    panic!("get_input not implemented on mock ui")
  }

  fn lock_ui(&mut self) -> UILock<'_> {
    UILock::new(self)
  }
  fn unlock_ui(&mut self){}
}