core: EditorCore,
width: usize,
first_visible: usize,
+ prompt: ColouredString,
+ promptwidth: usize,
}
impl SingleLineEditor {
- pub fn new() -> Self {
+ pub fn new(text: String) -> Self {
+ Self::new_with_prompt(text, ColouredString::plain(""))
+ }
+
+ pub fn new_with_prompt(text: String, prompt: ColouredString) -> Self {
+ let point = text.len();
+ let promptwidth = prompt.width();
SingleLineEditor {
core: EditorCore {
- text: "".to_owned(),
- point: 0,
+ text,
+ point,
paste_buffer: "".to_owned(),
},
width: 0,
first_visible: 0,
+ prompt,
+ promptwidth,
}
}
+ pub fn set_prompt(&mut self, prompt: ColouredString) {
+ self.prompt = prompt;
+ self.promptwidth = self.prompt.width();
+ self.update_first_visible();
+ }
+
fn update_first_visible(&mut self) {
if self.first_visible > self.core.point {
self.first_visible = self.core.point;
} else {
- let mut avail_width = self.width.saturating_sub(1);
+ let mut avail_width = self.width.saturating_sub(
+ self.promptwidth + 1);
let mut counted_initial_trunc_marker = false;
if self.first_visible > 0 {
counted_initial_trunc_marker = true;
}
pub fn draw(&self, width: usize) -> (ColouredString, Option<usize>) {
- let mut s = ColouredString::plain("");
+ let mut s = self.prompt.clone();
if self.first_visible > 0 {
s.push_str(&ColouredString::uniform("<", '>').slice());
}
}
struct BottomLineEditorOverlay {
- prompt: ColouredString,
- promptwidth: usize,
ed: SingleLineEditor,
result: Box<dyn Fn(&str, &mut Client) -> LogicalAction>,
}
fn new(prompt: ColouredString,
result: Box<dyn Fn(&str, &mut Client) -> LogicalAction>) -> Self
{
- let promptwidth = prompt.width();
BottomLineEditorOverlay {
- prompt,
- promptwidth,
- ed: SingleLineEditor::new(),
+ ed: SingleLineEditor::new_with_prompt("".to_owned(), prompt),
result,
}
}
impl ActivityState for BottomLineEditorOverlay {
fn resize(&mut self, w: usize, _h: usize) {
- self.ed.resize(w.saturating_sub(self.promptwidth));
+ self.ed.resize(w);
}
fn draw(&self, w: usize, _h: usize) ->
(Vec<ColouredString>, CursorPosition)
{
- let (buffer, cursorpos) = self.ed.draw(
- w.saturating_sub(self.promptwidth));
+ let (text, cursorpos) = self.ed.draw(w);
let cursorpos = match cursorpos {
- Some(x) => CursorPosition::At(x + self.promptwidth, 0),
+ Some(x) => CursorPosition::At(x, 0),
None => CursorPosition::None,
};
- (vec! { &self.prompt + buffer }, cursorpos)
+ (vec! { text }, cursorpos)
}
fn handle_keypress(&mut self, key: OurKey, client: &mut Client) ->