Skip to main content

brush_interactive/
input_backend.rs

1use crate::ShellError;
2
3/// Represents an input backend for reading lines of input.
4pub trait InputBackend: Send {
5    /// Reads a line of input, using the given prompt.
6    ///
7    /// # Arguments
8    ///
9    /// * `shell` - The shell instance for which input is being read.
10    /// * `prompt` - The prompt to display to the user.
11    fn read_line(
12        &mut self,
13        shell: &crate::ShellRef<impl brush_core::ShellExtensions>,
14        prompt: InteractivePrompt,
15    ) -> Result<ReadResult, ShellError>;
16
17    /// Returns the current contents of the read buffer and the current cursor
18    /// position within the buffer; None is returned if the read buffer is
19    /// empty or cannot be read by this implementation.
20    fn get_read_buffer(&self) -> Option<(String, usize)> {
21        None
22    }
23
24    /// Updates the read buffer with the given string and cursor. Considered a
25    /// no-op if the implementation does not support updating read buffers.
26    fn set_read_buffer(&mut self, _buffer: String, _cursor: usize) {
27        // No-op by default.
28    }
29}
30
31/// Result of a read operation.
32pub enum ReadResult {
33    /// The user entered a line of input.
34    Input(String),
35    /// A bound key sequence yielded a registered command.
36    BoundCommand(String),
37    /// End of input was reached.
38    Eof,
39    /// The user interrupted the input operation.
40    Interrupted,
41}
42
43/// Represents an interactive prompt.
44pub struct InteractivePrompt {
45    /// Prompt to display.
46    pub prompt: String,
47    /// Alternate-side prompt (typically right) to display.
48    pub alt_side_prompt: String,
49    /// Prompt to display on a continuation line of input.
50    pub continuation_prompt: String,
51}