From: Simon Tatham Date: Wed, 3 Jan 2024 06:55:03 +0000 (+0000) Subject: Move prompt into SingleLineEditor. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=f63bb9d75b83d0a630e4057ee14f2491b95d69a3;p=mastodonochrome.git Move prompt into SingleLineEditor. It was a mistake to put it in the wrapping BottomLineEditorOverlay, because I'm going to want to put single-line editors elsewhere on the screen with the same kind of prompt. --- diff --git a/src/editor.rs b/src/editor.rs index 3d874bb..cc379c7 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -358,26 +358,43 @@ pub struct SingleLineEditor { 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; @@ -439,7 +456,7 @@ impl SingleLineEditor { } pub fn draw(&self, width: usize) -> (ColouredString, Option) { - let mut s = ColouredString::plain(""); + let mut s = self.prompt.clone(); if self.first_visible > 0 { s.push_str(&ColouredString::uniform("<", '>').slice()); } @@ -595,8 +612,6 @@ fn test_single_line_visibility() { } struct BottomLineEditorOverlay { - prompt: ColouredString, - promptwidth: usize, ed: SingleLineEditor, result: Box LogicalAction>, } @@ -605,11 +620,8 @@ impl BottomLineEditorOverlay { fn new(prompt: ColouredString, result: Box LogicalAction>) -> Self { - let promptwidth = prompt.width(); BottomLineEditorOverlay { - prompt, - promptwidth, - ed: SingleLineEditor::new(), + ed: SingleLineEditor::new_with_prompt("".to_owned(), prompt), result, } } @@ -617,21 +629,20 @@ impl BottomLineEditorOverlay { 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, 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) ->