pub trait PromptMode<U: Ui>:
Clone
+ Send
+ 'static {
type ExitWidget: Widget<U> = File<U>;
// Required methods
fn update(&mut self, pa: &mut Pass, text: Text, area: &U::Area) -> Text;
fn prompt(&self) -> Text;
// Provided methods
fn on_switch(&mut self, pa: &mut Pass, text: Text, area: &U::Area) -> Text { ... }
fn before_exit(&mut self, pa: &mut Pass, text: Text, area: &U::Area) { ... }
fn once() { ... }
fn return_handle(&self) -> Option<Handle<Self::ExitWidget, U>> { ... }
}Expand description
A mode to control the Prompt, by acting on its Text and
U::Area
Through the Pass, one can act on the entirety of Duat’s shared
state:
use duat_core::prelude::*;
use duat_utils::modes::PromptMode;
#[derive(Default, Clone)]
struct RealTimeSwitch {
initial: Option<String>,
current: Option<String>,
name_was_correct: bool,
};
impl<U: Ui> PromptMode<U> for RealTimeSwitch {
fn update(&mut self, pa: &mut Pass, text: Text, area: &U::Area) -> 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: &U::Area) -> Text {
self.initial = Some(context::fixed_file::<U>(pa).unwrap().read(pa).name());
self.current = self.initial.clone();
text
}
fn before_exit(&mut self, pa: &mut Pass, text: Text, area: &U::Area) {
if !self.name_was_correct {
cmd::buffer(pa, self.initial.take().unwrap());
}
}
fn prompt(&self) -> Text {
txt!("[prompt]switch to").build()
}
}The PromptMode above will switch to the file with the same
name as the one in the PromptLine, returning to the initial
file if the match failed.
Provided Associated Types§
Sourcetype ExitWidget: Widget<U> = File<U>
type ExitWidget: Widget<U> = File<U>
What Widget to exit to, upon pressing enter, esc, or
backspace in an empty PromptLine
Required Methods§
Sourcefn update(&mut self, pa: &mut Pass, text: Text, area: &U::Area) -> Text
fn update(&mut self, pa: &mut Pass, text: Text, area: &U::Area) -> Text
Updates the PromptLine and Text of the Prompt
This function is triggered every time the user presses a key
in the Prompt mode.
Sourcefn prompt(&self) -> Text
fn prompt(&self) -> Text
What text should be at the beginning of the PromptLine, as
a Ghost
Provided Methods§
Sourcefn on_switch(&mut self, pa: &mut Pass, text: Text, area: &U::Area) -> Text
fn on_switch(&mut self, pa: &mut Pass, text: Text, area: &U::Area) -> 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.
Sourcefn before_exit(&mut self, pa: &mut Pass, text: Text, area: &U::Area)
fn before_exit(&mut self, pa: &mut Pass, text: Text, area: &U::Area)
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.
Sourcefn once()
fn once()
Things to do when this PromptMode is first instantiated
Sourcefn return_handle(&self) -> Option<Handle<Self::ExitWidget, U>>
fn return_handle(&self) -> Option<Handle<Self::ExitWidget, U>>
An optional returning Handle for the ExitWidget
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.