add_ed/ui/
mod.rs

1//! Defines UI trait and some testing implementations
2//!
3//! It is used to allow exchanging the UI of hired and to insert a dummy UI for
4//! script input.
5
6use crate::{
7  Ed,
8  Result,
9};
10
11mod lock;
12pub use lock::UILock;
13
14mod scripted_ui;
15pub use scripted_ui::ScriptedUI;
16
17pub mod mock_ui;
18pub mod dummy_ui;
19
20/// The UI trait used to abstract all common UI operations
21pub trait UI {
22  /// A basic print for errors and other information messages
23  fn print_message(&mut self,
24    data: &str,
25  ) -> Result<()>;
26
27  /// Print a listing of the commands with short descriptions
28  ///
29  /// Default implementation uses `self.print_message()` to print the const
30  /// string exported at `add_ed::messages::COMMANDS_LISTING`.
31  fn print_commands(&mut self) -> Result<()> {
32    self.print_message(crate::messages::COMMAND_LIST)
33  }
34
35  /// Print commands documentation
36  ///
37  /// Print usage documentation for the commands. You can use the std::concat
38  /// macro to add your own documentation to the commands documentation string
39  /// at `add_ed::messages::COMMANDS_DOCUMENTATION`.
40  fn print_command_documentation(&mut self) -> Result<()>;
41
42  /// Get a command for parsing and execution
43  ///
44  /// * Ed passed in for interactive viewing and status printouts. Ignore if unused.
45  /// * Prefix is printed at start of the line if given. Ignore if unsuited for your UI.
46  /// * Must return a single line to be parsed, trimming optional
47  fn get_command(&mut self,
48    ed: &Ed,
49    prefix: Option<char>,
50  ) -> Result<String>;
51
52  /// Get input lines until given character is entered alone on a line
53  ///
54  /// * Ed passed in for interactive viewing and status printouts. Ignore if unused.
55  /// * Must return a vector newline terminated strings and not return the terminating line
56  fn get_input(&mut self,
57    ed: &Ed,
58    terminator: char,
59    #[cfg(feature = "initial_input_data")]
60    initial_buffer: Option<Vec<String>>,
61  ) -> Result<Vec<String>>;
62
63  /// Print the given selection with the given options
64  ///
65  /// Depending on UI this may mean changing viewport settings and moving to given selection.
66  /// * Ed passed in for path based highlighting and status printouts. Ignore if unused.
67  /// * Separate selection passed in since the selection to print isn't saved to state
68  ///   until after printing.
69  fn print_selection(&mut self,
70    ed: &Ed,
71    selection: (usize, usize),
72    numbered: bool,
73    literal: bool,
74  ) -> Result<()>;
75
76  /// Prepare UI before handing down stdin/out/err to child process
77  ///
78  /// The returned UIHandle should hold a mutable reference to its parent UI.
79  /// Using that reference the UIHandle calls unlock_ui() when being dropped.
80  fn lock_ui(&mut self) -> UILock<'_>;
81
82  /// Resume UI after lock_ui has been called
83  ///
84  /// This method shouldn't be called except by UIHandle's Drop implementation.
85  fn unlock_ui(&mut self);
86}