|
|
@ -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(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|