PromptMode

Trait PromptMode 

Source
pub trait PromptMode: Send + 'static {
    type ExitWidget: Widget
       where Self: Sized;

    // Required methods
    fn update(&mut self, pa: &mut Pass, text: Text, area: &RwArea) -> Text;
    fn prompt(&self) -> Text;

    // Provided methods
    fn on_switch(&mut self, pa: &mut Pass, text: Text, area: &RwArea) -> Text { ... }
    fn before_exit(&mut self, pa: &mut Pass, text: Text, area: &RwArea) { ... }
    fn post_update(&mut self, pa: &mut Pass, handle: &Handle<PromptLine>) { ... }
    fn return_handle(&self) -> Option<Handle<dyn Widget>> { ... }
}
Expand description

A mode to control the Prompt

Through the Pass, one can act on the entirety of Duat’s shared state:

use duat::prelude::*;

#[derive(Default, Clone)]
struct RealTimeSwitch {
    initial: Option<String>,
    current: Option<String>,
    name_was_correct: bool,
};

impl PromptMode for RealTimeSwitch {
    type ExitWidget = Buffer;

    fn update(&mut self, pa: &mut Pass, text: Text, area: &ui::RwArea) -> Text {
        let name = text.to_string();

        self.name_was_correct = if name != *self.current.as_ref().unwrap() {
            if cmd::buffer(pa, &name).is_ok() {
                self.current = Some(name);
                true
            } else {
                false
            }
        } else {
            true
        };

        text
    }

    fn on_switch(&mut self, pa: &mut Pass, text: Text, area: &ui::RwArea) -> Text {
        self.initial = Some(context::current_buffer(pa).read(pa).name());
        self.current = self.initial.clone();

        text
    }

    fn before_exit(&mut self, pa: &mut Pass, text: Text, area: &ui::RwArea) {
        if !self.name_was_correct {
            cmd::buffer(pa, self.initial.take().unwrap());
        }
    }

    fn prompt(&self) -> Text {
        txt!("[prompt]switch to")
    }
}

The PromptMode above will switch to the buffer with the same name as the one in the PromptLine, returning to the initial buffer if the match failed.

Required Associated Types§

Source

type ExitWidget: Widget where Self: Sized

What Widget to exit to, upon pressing enter, esc, or backspace in an empty PromptLine

Usually, this would be Buffer

Required Methods§

Source

fn update(&mut self, pa: &mut Pass, text: Text, area: &RwArea) -> Text

Updates the PromptLine and Text of the Prompt

This function is triggered every time the user presses a key in the Prompt mode.

Source

fn prompt(&self) -> Text

What text should be at the beginning of the PromptLine, as a Ghost

Provided Methods§

Source

fn on_switch(&mut self, pa: &mut Pass, text: Text, area: &RwArea) -> Text

What to do when switchin onto this PromptMode

The initial Text is always empty, except for the prompt Ghost at the beginning of the line.

Source

fn before_exit(&mut self, pa: &mut Pass, text: Text, area: &RwArea)

What to do before exiting the PromptMode

This usually involves some sor of “commitment” to the result, e.g., RunCommands executes the call, IncSearch finishes the search, etc.

Source

fn post_update(&mut self, pa: &mut Pass, handle: &Handle<PromptLine>)

A post update hook to be called on the Handle itself

One useful thing that you can do on this function is a call to CompletionsBuilder::open, which doesn’t work on PromptMode::update because the Text of the PromptLine is taken.

Source

fn return_handle(&self) -> Option<Handle<dyn Widget>>

An optional returning Handle for the ExitWidget

Implementors§