From e40fb9249a6fe0f3dc3fef0c01bd7a58bccc4cb7 Mon Sep 17 00:00:00 2001 From: Vincent Ollivier Date: Mon, 16 Sep 2024 17:56:35 +0200 Subject: [PATCH] Use prompt library --- src/api/prompt.rs | 14 +++++++-- src/usr/edit.rs | 79 +++++++++++++++-------------------------------- 2 files changed, 36 insertions(+), 57 deletions(-) diff --git a/src/api/prompt.rs b/src/api/prompt.rs index 334204c3b..9119d9c0d 100644 --- a/src/api/prompt.rs +++ b/src/api/prompt.rs @@ -7,6 +7,7 @@ use vte::{Params, Parser, Perform}; pub struct Prompt { pub completion: Completion, pub history: History, + pub eol: bool, offset: usize, // Offset line by the length of the prompt string cursor: usize, line: Vec, // UTF-32 @@ -17,6 +18,7 @@ impl Prompt { Self { completion: Completion::new(), history: History::new(), + eol: true, offset: 0, cursor: 0, line: Vec::with_capacity(80), @@ -33,18 +35,24 @@ impl Prompt { match c { console::ETX_KEY => { // End of Text (^C) self.update_completion(); - println!(); + if self.eol { + println!(); + } return Some(String::new()); } console::EOT_KEY => { // End of Transmission (^D) self.update_completion(); - println!(); + if self.eol { + println!(); + } return None; } '\n' => { // New Line self.update_completion(); self.update_history(); - println!(); + if self.eol { + println!(); + } return Some(self.line.iter().collect()); } c => { diff --git a/src/usr/edit.rs b/src/usr/edit.rs index 2d95164ea..c52b300e7 100644 --- a/src/usr/edit.rs +++ b/src/usr/edit.rs @@ -1,5 +1,6 @@ use crate::api::console::Style; use crate::api::process::ExitCode; +use crate::api::prompt::Prompt; use crate::api::{console, fs, io}; use crate::api; @@ -34,25 +35,29 @@ struct Coords { pub struct Editor { pathname: String, - query: String, clipboard: Vec, lines: Vec, cursor: Coords, offset: Coords, highlighted: Vec<(usize, usize, char)>, config: EditorConfig, + search_query: String, + search_prompt: Prompt, } impl Editor { pub fn new(pathname: &str) -> Self { let cursor = Coords { x: 0, y: 0 }; let offset = Coords { x: 0, y: 0 }; - let query = String::new(); let highlighted = Vec::new(); let clipboard = Vec::new(); let mut lines = Vec::new(); let config = EditorConfig { tab_size: 4 }; + let search_query = String::new(); + let mut search_prompt = Prompt::new(); + search_prompt.eol = false; + match fs::read_to_string(pathname) { Ok(contents) => { for line in contents.lines() { @@ -71,13 +76,14 @@ impl Editor { Self { pathname, - query, clipboard, lines, cursor, offset, highlighted, config, + search_query, + search_prompt, } } @@ -589,60 +595,25 @@ impl Editor { } pub fn find(&mut self) { - self.query = String::new(); - let status = format!("Find: "); + let label = format!("Find: "); let color = Style::color("black").with_background("silver"); let reset = Style::reset(); + + // Set up bottom line + print!("\x1b[{};1H", self.rows() + 1); + print!("{}{}", color, " ".repeat(self.cols())); print!("\x1b[{};1H", self.rows() + 1); - print!("{}{:cols$}{}", color, status, reset, cols = self.cols()); - print!("\x1b[{};{}H", self.rows() + 1, status.len() + 1); print!("\x1b[?25h"); // Enable cursor - let mut escape = false; - let mut csi = false; - loop { - let c = io::stdin().read_char().unwrap_or('\0'); - match c { - '\x1B' => { // ESC - escape = true; - continue; - } - '[' if escape => { - csi = true; - continue; - } - '\n' => { // Newline - self.find_next(); - return; - } - '\x03' => { // Ctrl C - return; - } - '\x08' => { // Backspace - if !self.query.is_empty() { - self.query.pop(); - print!("\x1b[{};1H", self.rows() + 1); - print!("{}{:cols$}{}", color, status, reset, cols = self.cols()); - print!("\x1b[{};{}H", self.rows() + 1, status.len() + 1); - print!("{}{}{}", color, self.query, reset); - } - } - c => { - if csi { - match c { - '0'..'9' | ';' => { - } - _ => { - escape = false; - csi = false; - } - } - continue; - } - if let Some(s) = self.render_char(c) { - print!("{}{}{}", color, s, reset); - self.query.push_str(&s); - } - } + + let res = self.search_prompt.input(&label); + + print!("{}", reset); + + if let Some(query) = res { + if !query.is_empty() { + self.search_prompt.history.add(&query); + self.search_query = query; + self.find_next(); } } } @@ -658,7 +629,7 @@ impl Editor { if y == dy { o = cmp::min(dx + 1, line.len()); } - if let Some(i) = line[o..].find(&self.query) { + if let Some(i) = line[o..].find(&self.search_query) { let x = o + i; self.cursor.x = x % self.cols(); self.cursor.y = y % self.rows();