Trait Editor

Source
pub trait Editor {
    // Provided methods
    fn highlight(&self, buffer: &str) -> String { ... }
    fn highlight_prompt(&self, prompt: &str, multiline: bool) -> String { ... }
    fn hint(&self, buffer: &str) -> Option<String> { ... }
    fn highlight_hint(&self, hint: &str) -> String { ... }
    fn complete(&self, buffer: &str, cursor: usize) -> Option<Completion> { ... }
    fn indent(&self, buffer: &mut String, cursor: &mut usize) { ... }
    fn is_multiline(&self, buffer: &str, cursor: usize) -> bool { ... }
    fn is_keyword(c: char) -> bool { ... }
    fn insert(&self, buffer: &mut String, cursor: &mut usize, c: char) { ... }
    fn next_event(&mut self, input: &mut Reader<impl Read>) -> Result<Event> { ... }
}
Expand description

Custom editor behaviour for a Prompt

All functions provided may be overrided with custom ones. For instance, colors may be added by implementing Editor::highlight. Detailed examples can be found in the examples directory.

Provided Methods§

Source

fn highlight(&self, buffer: &str) -> String

Highlights the current input by adding ANSI color sequences.

See also Editor::highlight_prompt and Editor::highlight_hint

§Implementation notes

The returned string should have the same length when displayed (including whitespace), so only “invisible” sequences like SGR should be added.

Source

fn highlight_prompt(&self, prompt: &str, multiline: bool) -> String

Highlights the current prompt.

See Editor::highlight for more information.

Source

fn hint(&self, buffer: &str) -> Option<String>

Returns a hint for the current input, if available.

This hint will be shown on the next line.

Source

fn highlight_hint(&self, hint: &str) -> String

Highlights the current hint.

See Editor::highlight for more information.

Source

fn complete(&self, buffer: &str, cursor: usize) -> Option<Completion>

Provides completion if available.

Returning Some will cause Event::Tab to cycle through all results in the Vec, replacing buffer[start..end] until another key is pressed. Otherwise, Editor::indent is called.

Source

fn indent(&self, buffer: &mut String, cursor: &mut usize)

Inserts indentation at the current cursor position when Editor::complete returns none.

Source

fn is_multiline(&self, buffer: &str, cursor: usize) -> bool

Returns true if the current input should be continued on another line.

Source

fn is_keyword(c: char) -> bool

Returns true if the given character is a word character.

This affects word movement keybinds (e.g. Ctrl-Right).

Source

fn insert(&self, buffer: &mut String, cursor: &mut usize, c: char)

Inserts a character at the current cursor position, moving it forward.

§Example

Auto-closing parenthesis. A better example can be found in the repo.

fn insert(&self, buffer: &mut String, cursor: &mut usize, c: char) {
    buffer.insert(*cursor, c);
    *cursor += c.len_utf8(); // Move forward

    if c == '(' {
        buffer.insert(*cursor, ')');
    }
}
Source

fn next_event(&mut self, input: &mut Reader<impl Read>) -> Result<Event>

Reads ANSI sequences from input and returns an editor event.

§Example
fn next_event(&mut self, input: &mut ansi::Reader<impl io::Read>) -> io::Result<Event> {
    loop {
        let event = match input.read_sequence()? {
            Ansi::Char(c) => Event::Insert(c),
            Ansi::Control(b'C') => Event::Interrupt,
            _ => continue,
        };

        return Ok(event);
    }
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§