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
//! This module is for supporting scripts.
//! The entire script is put into a vector of strings and treated as input.
//! It optionally takes a mutable UI reference, to support printing when the script requests it.

use super::{UI, UILock};
use super::Ed;

use super::Result;

use std::collections::VecDeque;

#[cfg(feature = "initial_input_data")]
use crate::error::UIError;

/// Error type for Scripted UI which can only occur if you enable the feature
/// `initial_input_data` and given initial data to [`ScriptedUI::get_input`]
#[cfg(feature = "initial_input_data")]
#[derive(Debug)]
pub struct UnsupportedInitialData{}
#[cfg(feature = "initial_input_data")]
impl std::fmt::Display for UnsupportedInitialData {
  fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
    write!(f, "You cannot take input with initial data in a script.")
  }
}
#[cfg(feature = "initial_input_data")]
impl std::error::Error for UnsupportedInitialData {}
#[cfg(feature = "initial_input_data")]
impl crate::error::UIErrorTrait for UnsupportedInitialData {}

/// This is a scripted UI. It returns the scripted input without querying user.
///
/// How to use:
/// * Put the input to simulate line-by-line in the input variable.
///   (terminating '\n' required, errors may arize if missing)
/// * If you want output from print commands put a UI to print with in
///   `print_ui`.
///   (If none given prints will be quietly ignored)

// Things not derived here since they would require the same being implemented
// on the UI trait, which is too extreme for me at this stage. If you have need,
// complain and I'll add it.
pub struct ScriptedUI<'a> {
  pub input: VecDeque<String>,
  pub print_ui: Option<&'a mut dyn UI>,
}
impl <'a> UI for ScriptedUI<'a> {
  fn get_command(&mut self,
    _ed: &Ed,
    _prefix: Option<char>
  ) -> Result<String> {
    match self.input.pop_front() {
      Some(x) => Ok(x),
      // Returns from the macro execution no matter what.
      None => Ok("Q\n".to_string()),
    }
  }
  fn get_input(&mut self,
    _ed: &Ed,
    terminator: char,
    #[cfg(feature = "initial_input_data")]
    initial_buffer: Option<Vec<String>>, // causes error
  ) -> Result<Vec<String>> {
    #[cfg(feature = "initial_input_data")]
    {
      if initial_buffer.is_some() {
        return Err(Into::<UIError>::into(UnsupportedInitialData{}).into())
      }
    }
    let mut ret = Vec::new();
    let term = format!("{}\n", terminator);
    // Loop until we run out of data or find the terminator
    loop {
      match self.input.pop_front() {
        None => return Ok(ret), // Return what we have
        Some(x) => {
          if x == term { return Ok(ret); }
          ret.push(x)
        }
      }
    }
  }
  // Printing is handed to the print_ui if one was given, else ignored
  fn print_message(
    &mut self,
    text: &str
  ) -> Result<()> {
    match &mut self.print_ui {
      Some(ui) => ui.print_message(text),
      None => Ok(()),
    }
  }
  fn print_commands(&mut self) -> Result<()> {
    match &mut self.print_ui {
      Some(ui) => ui.print_commands(),
      None => Ok(()),
    }
  }
  fn print_command_documentation(&mut self) -> Result<()> {
    match &mut self.print_ui {
      Some(ui) => ui.print_command_documentation(),
      None => Ok(()),
    }
  }
  fn print_selection(&mut self,
    ed: &Ed,
    selection: (usize, usize),
    numbered: bool,
    literal: bool,
  ) -> Result<()> {
    match &mut self.print_ui {
      Some(ui) => {
        ui.print_selection(ed, selection, numbered, literal)
      },
      None => Ok(()),
    }
  }
  fn lock_ui(&mut self) -> UILock<'_> {
    match self.print_ui {
      Some(ref mut i) => i.lock_ui(),
      None => UILock::new(self),
    }
  }
  // Will only be called if no inner UI, beware
  fn unlock_ui(&mut self) {}
}