chiark / gitweb /
Move prompt into SingleLineEditor.
authorSimon Tatham <anakin@pobox.com>
Wed, 3 Jan 2024 06:55:03 +0000 (06:55 +0000)
committerSimon Tatham <anakin@pobox.com>
Wed, 3 Jan 2024 06:55:03 +0000 (06:55 +0000)
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.

src/editor.rs

index 3d874bb31aef8ebbec7f365e304d6956ba507103..cc379c7e69fb897ba0121b901bf879ef7ba78057 100644 (file)
@@ -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<usize>) {
-        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<dyn Fn(&str, &mut Client) -> LogicalAction>,
 }
@@ -605,11 +620,8 @@ impl BottomLineEditorOverlay {
     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,
         }
     }
@@ -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<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) ->