Fixed prompt cursor adjustment

pull/2/head
Mark 2023-04-04 10:22:32 -07:00
parent e876244f09
commit aa90d798c9
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 32 additions and 12 deletions

View File

@ -162,26 +162,44 @@ pub fn parse(
pub fn substitute( pub fn substitute(
s: &String, // The string to subsitute s: &String, // The string to substitute
c: usize // Location of the cursor right now c: usize // Location of the cursor right now
) -> String{ ) -> (
if s == "" { return s.clone() } usize, // Location of cursor in substituted string
String // String with substitutions
) {
if s == "" { return (c, s.clone()) }
let mut new_s = s.clone(); let mut new_s = s.clone();
let l = s.chars().count();
let tokens = tokenize(s); let tokens = tokenize(s);
let (subs, _) = find_subs(tokens); let (subs, _) = find_subs(tokens);
let mut new_c = l - c;
for r in subs.iter() { for r in subs.iter() {
if { // Don't subsitute if our cursor is inside the substitution // find_subs gives substitutions in reverse order.
if { // Don't substitute if our cursor is inside the substitution
c >= r.0.pos && c >= r.0.pos &&
c < r.0.pos+r.0.len c < r.0.pos+r.0.len
} { continue; } } { continue; }
if c < r.0.pos {
let ct = r.1.chars().count();
if ct >= r.0.len {
if new_c >= ct - r.0.len {
new_c += ct - r.0.len
}
} else {
new_c -= r.0.len - ct
}
}
new_s.replace_range( new_s.replace_range(
r.0.pos..r.0.pos+r.0.len, r.0.pos..r.0.pos+r.0.len,
&r.1[..] &r.1[..]
) )
} }
return new_s; return (new_c, new_s);
} }

View File

@ -39,11 +39,12 @@ impl PromptBuffer {
pub fn write_prompt(&mut self, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> { pub fn write_prompt(&mut self, stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io::Error> {
let i = self.buffer.chars().count(); let l = self.buffer.chars().count();
let i = if i == 0 {0} else {i - self.cursor}; let i = if l == 0 {0} else {l - self.cursor};
// Draw prettyprinted expression // Draw prettyprinted expression
let s = substitute(&self.get_contents(), i); let (display_cursor, s) = substitute(&self.get_contents(), i);
write!( write!(
stdout, "\r{}{}==>{}{} {}", stdout, "\r{}{}==>{}{} {}",
style::Bold, style::Bold,
@ -52,7 +53,7 @@ impl PromptBuffer {
style::Reset, style::Reset,
s s
)?; )?;
// If this string is shorter, clear the remaining old one. // If this string is shorter, clear the remaining old one.
if s.chars().count() < self.last_print_len { if s.chars().count() < self.last_print_len {
write!( write!(
@ -61,12 +62,13 @@ impl PromptBuffer {
termion::cursor::Left((self.last_print_len - s.chars().count()) as u16) termion::cursor::Left((self.last_print_len - s.chars().count()) as u16)
)?; )?;
} }
// Move cursor to correct position // Move cursor to correct position
if self.cursor != 0 { if display_cursor != 0 {
write!( write!(
stdout, "{}", stdout, "{}",
termion::cursor::Left(self.cursor as u16) termion::cursor::Left(display_cursor as u16)
)?; )?;
stdout.flush()?; stdout.flush()?;
} }