Skip to content

Commit

Permalink
Use prompt library
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc committed Sep 16, 2024
1 parent 285b905 commit e40fb92
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 57 deletions.
14 changes: 11 additions & 3 deletions src/api/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>, // UTF-32
Expand All @@ -17,6 +18,7 @@ impl Prompt {
Self {
completion: Completion::new(),
history: History::new(),
eol: true,
offset: 0,
cursor: 0,
line: Vec::with_capacity(80),
Expand All @@ -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 => {
Expand Down
79 changes: 25 additions & 54 deletions src/usr/edit.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -34,25 +35,29 @@ struct Coords {

pub struct Editor {
pathname: String,
query: String,
clipboard: Vec<String>,
lines: Vec<String>,
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() {
Expand All @@ -71,13 +76,14 @@ impl Editor {

Self {
pathname,
query,
clipboard,
lines,
cursor,
offset,
highlighted,
config,
search_query,
search_prompt,
}
}

Expand Down Expand Up @@ -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();
}
}
}
Expand All @@ -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();
Expand Down

0 comments on commit e40fb92

Please sign in to comment.