non-persistent history

main
Jordan Orelli 10 months ago
parent 447dd3edc1
commit 437afd9d76

@ -193,6 +193,11 @@ pub struct Editor {
/// this thing draws our prompt /// this thing draws our prompt
prompt: Prompt, prompt: Prompt,
/// Our current position when scrolling through the history. A hist_pos of 0 means that we're
/// not looking at the history. hist_pos of 1 means that we're looking at the first most-recent
/// command entered.
hist_pos: usize,
} }
impl Editor { impl Editor {
@ -202,6 +207,7 @@ impl Editor {
buffer: Buffer::new(), buffer: Buffer::new(),
display: output::Writer::stdout()?, display: output::Writer::stdout()?,
prompt: Prompt::new(), prompt: Prompt::new(),
hist_pos: 0,
}) })
} }
@ -223,7 +229,7 @@ impl Editor {
Status::Ongoing Status::Ongoing
} }
Control(cc) => self.handle_control(cc)?, Control(cc) => self.handle_control(cc)?,
Escape(escape) => self.handle_escape(escape)?, Escape(escape) => self.handle_escape(escape, history)?,
Size => { Size => {
debug!("ignoring size event"); debug!("ignoring size event");
Status::Ongoing Status::Ongoing
@ -240,7 +246,10 @@ impl Editor {
match status { match status {
Status::Ongoing => {} Status::Ongoing => {}
Status::Done => return Ok(None), Status::Done => return Ok(None),
Status::Submit(e) => return Ok(Some(e)), Status::Submit(e) => {
self.hist_pos = 0;
return Ok(Some(e));
}
} }
} }
} }
@ -261,12 +270,13 @@ impl Editor {
Ok(Status::Ongoing) Ok(Status::Ongoing)
} }
fn handle_escape(&mut self, esc: Escape) -> Result<Status> { fn handle_escape(&mut self, esc: Escape, history: &History) -> Result<Status> {
use Escape::*; use Escape::*;
match esc { match esc {
Left => self.back(1)?, Left => self.back(1)?,
Right => self.forward(1)?, Right => self.forward(1)?,
Up => self.prev_history()?, Up => self.prev_history(history)?,
Down => self.next_history(history)?,
Home => self.seek_left()?, Home => self.seek_left()?,
End => self.seek_right()?, End => self.seek_right()?,
esc => debug!(" Ignored escape: {esc:?}"), esc => debug!(" Ignored escape: {esc:?}"),
@ -424,7 +434,41 @@ impl Editor {
Ok(()) Ok(())
} }
fn prev_history(&mut self) -> Result<()> { fn prev_history(&mut self, history: &History) -> Result<()> {
let text = match history.look_back(self.hist_pos + 1) {
Some(s) => {
self.hist_pos += 1;
s
}
None => return Ok(()),
};
self.seek_right()?;
self.clear_left()?;
for c in text.chars() {
self.insert(c)?;
}
Ok(())
}
fn next_history(&mut self, history: &History) -> Result<()> {
if self.hist_pos <= 1 {
self.seek_right()?;
self.clear_left()?;
self.hist_pos = 0;
return Ok(());
}
let text = match history.look_back(self.hist_pos - 1) {
Some(s) => {
self.hist_pos -= 1;
s
}
None => return Ok(()),
};
self.seek_right()?;
self.clear_left()?;
for c in text.chars() {
self.insert(c)?;
}
Ok(()) Ok(())
} }
} }

@ -10,4 +10,11 @@ impl History {
pub fn add<S: Into<String>>(&mut self, event: S) { pub fn add<S: Into<String>>(&mut self, event: S) {
self.events.push(event.into()); self.events.push(event.into());
} }
pub fn look_back(&self, idx: usize) -> Option<&str> {
if idx > self.events.len() {
return None;
}
Some(&self.events[self.events.len() - idx])
}
} }

@ -49,6 +49,7 @@ impl Session {
Some(text) => text, Some(text) => text,
None => break, None => break,
}; };
history.add(text.clone());
let command = match parse(&text) { let command = match parse(&text) {
Ok(ast) => ast, Ok(ast) => ast,
Err(e) => { Err(e) => {

@ -15,6 +15,11 @@ impl Prompt {
// | | +-------- clear styles // | | +-------- clear styles
// | | | +- show cursor // | | | +- show cursor
// v v v v // v v v v
#[cfg(debug_assertions)]
s: String::from("\x1b[?25l\x1b[31m ▷ \x1b[0m\x1b[?25h"),
#[allow(dead_code)]
#[cfg(not(debug_assertions))]
s: String::from("\x1b[?25l\x1b[32m ▷ \x1b[0m\x1b[?25h"), s: String::from("\x1b[?25l\x1b[32m ▷ \x1b[0m\x1b[?25h"),
} }
} }

Loading…
Cancel
Save