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
//! Defines UI trait and some testing implementations
//!
//! It is used to allow exchanging the UI of hired and to insert a dummy UI for
//! script input.

use crate::{
  Ed,
  Result,
};

mod lock;
pub use lock::UILock;

mod scripted_ui;
pub use scripted_ui::ScriptedUI;

pub mod mock_ui;
pub mod dummy_ui;

/// The UI trait used to abstract all common UI operations
pub trait UI {
  /// A basic print for errors and other information messages
  fn print_message(&mut self,
    data: &str,
  ) -> Result<()>;

  /// Print a listing of the commands with short descriptions
  ///
  /// Default implementation uses `self.print_message()` to print the const
  /// string exported at `add_ed::messages::COMMANDS_LISTING`.
  fn print_commands(&mut self) -> Result<()> {
    self.print_message(crate::messages::COMMAND_LIST)
  }

  /// Print commands documentation
  ///
  /// Print usage documentation for the commands. You can use the std::concat
  /// macro to add your own documentation to the commands documentation string
  /// at `add_ed::messages::COMMANDS_DOCUMENTATION`.
  fn print_command_documentation(&mut self) -> Result<()>;

  /// Get a command for parsing and execution
  ///
  /// * Ed passed in for interactive viewing and status printouts. Ignore if unused.
  /// * Prefix is printed at start of the line if given. Ignore if unsuited for your UI.
  /// * Must return a single line to be parsed, trimming optional
  fn get_command(&mut self,
    ed: &Ed,
    prefix: Option<char>,
  ) -> Result<String>;

  /// Get input lines until given character is entered alone on a line
  ///
  /// * Ed passed in for interactive viewing and status printouts. Ignore if unused.
  /// * Must return a vector newline terminated strings and not return the terminating line
  fn get_input(&mut self,
    ed: &Ed,
    terminator: char,
    #[cfg(feature = "initial_input_data")]
    initial_buffer: Option<Vec<String>>,
  ) -> Result<Vec<String>>;

  /// Print the given selection with the given options
  ///
  /// Depending on UI this may mean changing viewport settings and moving to given selection.
  /// * Ed passed in for path based highlighting and status printouts. Ignore if unused.
  /// * Separate selection passed in since the selection to print isn't saved to state
  ///   until after printing.
  fn print_selection(&mut self,
    ed: &Ed,
    selection: (usize, usize),
    numbered: bool,
    literal: bool,
  ) -> Result<()>;

  /// Prepare UI before handing down stdin/out/err to child process
  ///
  /// The returned UIHandle should hold a mutable reference to its parent UI.
  /// Using that reference the UIHandle calls unlock_ui() when being dropped.
  fn lock_ui(&mut self) -> UILock<'_>;

  /// Resume UI after lock_ui has been called
  ///
  /// This method shouldn't be called except by UIHandle's Drop implementation.
  fn unlock_ui(&mut self);
}