Skip to content

Commit

Permalink
implemented prefix string for input field of readline
Browse files Browse the repository at this point in the history
* added Buffer.prefix field
* implemented Buffer.set_prefix method to specify prefix string
* fixed cursor move to the left direction with prefix-considered distances
  • Loading branch information
g1eng committed Aug 24, 2023
1 parent 3f058ca commit 677d06c
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/readline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use console::{Key, Term};
use std::io;
use std::io::Write;

const MAX_PREFIX_CAPACITY: usize = 32;
const DEFAULT_TEXT_CAPACITY: usize = 1024;

/// Buffer of a readline instance.
///
pub struct Buffer {
Expand All @@ -39,6 +42,8 @@ pub struct Buffer {
term: Term,
/// Cursor index for the next character input
index: usize,
/// prefix string for the input area
prefix: String,
/// Text payload for the buffer
text: String,
}
Expand Down Expand Up @@ -77,6 +82,7 @@ impl Clone for Buffer {
terminate_on_up_down: self.terminate_on_up_down,
term: self.term.clone(),
index: self.index,
prefix: self.prefix.clone(),
text: self.text.clone(),
}
}
Expand All @@ -92,7 +98,8 @@ impl Buffer {
terminate_on_up_down: false,
term: Term::stdout(),
index: 0,
text: String::new(),
prefix: String::with_capacity(MAX_PREFIX_CAPACITY),
text: String::with_capacity(DEFAULT_TEXT_CAPACITY),
}
}

Expand All @@ -105,6 +112,7 @@ impl Buffer {
terminate_on_up_down: false,
term: Term::stdout(),
index: 0,
prefix: String::with_capacity(MAX_PREFIX_CAPACITY),
text: String::from(text),
}
}
Expand All @@ -123,11 +131,13 @@ impl Buffer {
}
fn home(&mut self) -> io::Result<Key> {
self.term.move_cursor_left(self.text.len())?;
self.term.move_cursor_right(self.prefix.len())?;
self.index = 0;
Ok(Key::Home)
}
fn end(&mut self) -> io::Result<Key> {
self.term.move_cursor_right(self.text.len() - self.index)?;
self.term
.move_cursor_right(self.prefix.len() + self.text.len() - self.index)?;
self.index = self.text.len();
Ok(Key::End)
}
Expand Down Expand Up @@ -273,6 +283,12 @@ impl Buffer {
Ok(Key::ArrowRight)
}

/// set prefix for the input area
pub fn set_prefix(&mut self, prefix: String) {
self.prefix.clear();
self.prefix = prefix;
}

///Buffer.read_line provides interactive line editing functionality for a tty, which supports following basic shortcut keys:
///
/// * C-a (Home)
Expand All @@ -284,6 +300,7 @@ impl Buffer {
pub fn read_line(&mut self) -> io::Result<Key> {
let k: Key;

write!(&self.term, "{}", self.prefix)?;
loop {
match self.term.read_key()? {
Key::Enter => {
Expand Down Expand Up @@ -605,4 +622,13 @@ mod tests {
b.left().unwrap();
assert_eq!(b.index, idx_init);
}

#[test]
fn test_set_prefix() {
let mut b = init_with_word();
let data = "korekara";
assert_eq!(b.prefix.len(), 0);
b.set_prefix(data.to_string());
assert_eq!(b.prefix.len(), data.len());
}
}

0 comments on commit 677d06c

Please sign in to comment.