From 9a391480b9547fe622897110c1742bb450fb437f Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 4 Aug 2023 12:24:58 -0700 Subject: [PATCH] Minor prompt fixes --- src/entry/unix/promptbuffer.rs | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/entry/unix/promptbuffer.rs b/src/entry/unix/promptbuffer.rs index 18c03aa..2be13b5 100644 --- a/src/entry/unix/promptbuffer.rs +++ b/src/entry/unix/promptbuffer.rs @@ -36,6 +36,33 @@ impl PromptBuffer { }; } + // Same as write_primpt, but pretends there is no cursor + pub fn write_prompt_nocursor(&mut self, stdout: &mut RawTerminal) -> Result<(), std::io::Error> { + // Draw prettyprinted expression + let (_, s) = substitute_cursor(&self.get_contents(), self.buffer.chars().count()); + + write!( + stdout, "\r{}{}==>{}{} {}", + style::Bold, + color::Fg(color::Blue), + color::Fg(color::Reset), + style::Reset, + s + )?; + + // If this string is shorter, clear the remaining old one. + if s.chars().count() < self.last_print_len { + write!( + stdout, "{}{}", + " ".repeat(self.last_print_len - s.chars().count()), + termion::cursor::Left((self.last_print_len - s.chars().count()) as u16) + )?; + } + + self.last_print_len = s.chars().count(); + stdout.flush()?; + return Ok(()); + } pub fn write_prompt(&mut self, stdout: &mut RawTerminal) -> Result<(), std::io::Error> { let l = self.buffer.chars().count(); @@ -82,7 +109,9 @@ impl PromptBuffer { pub fn get_contents(&self) -> &String {&self.buffer} pub fn enter(&mut self) -> String { - let s = String::from(self.buffer.trim()); + // Don't trim input string so that linelocations are correct + //let s = String::from(self.buffer.trim()); + let s = self.buffer.clone(); self.buffer.clear(); self.hist_cursor = 0; self.cursor = 0;