|
|
|
@ -1,6 +1,6 @@
|
|
|
|
#![allow(unused)]
|
|
|
|
#![allow(unused)]
|
|
|
|
|
|
|
|
|
|
|
|
use std::{io::Read, mem::MaybeUninit};
|
|
|
|
use std::mem::MaybeUninit;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::reader::esc_parse::{InputChar, bell, hide_cursor, show_cursor};
|
|
|
|
use crate::reader::esc_parse::{InputChar, bell, hide_cursor, show_cursor};
|
|
|
|
|
|
|
|
|
|
|
|
@ -24,8 +24,7 @@ pub enum InitTTYError {
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ReadError {
|
|
|
|
pub enum ReadError {
|
|
|
|
EscapeError(esc_parse::AcceptError),
|
|
|
|
EscapeError(esc_parse::AcceptError)
|
|
|
|
Exited,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
@ -36,7 +35,6 @@ pub enum EditCommand {
|
|
|
|
CursorBack,
|
|
|
|
CursorBack,
|
|
|
|
CursorForward,
|
|
|
|
CursorForward,
|
|
|
|
DeleteBack,
|
|
|
|
DeleteBack,
|
|
|
|
Exit,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
macro_rules! edit_cmd {
|
|
|
|
macro_rules! edit_cmd {
|
|
|
|
@ -84,10 +82,10 @@ impl ReaderState {
|
|
|
|
self.redraw();
|
|
|
|
self.redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn current_hist(&mut self) -> Option<String> {
|
|
|
|
fn current_hist(&mut self) -> String {
|
|
|
|
let i: usize = self.history_inx.unwrap();
|
|
|
|
let i: usize = self.history_inx.unwrap();
|
|
|
|
let line = self.history.get(self.history.len().saturating_sub(i+1));
|
|
|
|
let line = self.history.get(self.history.len().saturating_sub(i+1));
|
|
|
|
return line.map(|s| s.to_string());
|
|
|
|
return line.unwrap().to_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn set_current_hist(&mut self, str: String) {
|
|
|
|
fn set_current_hist(&mut self, str: String) {
|
|
|
|
@ -97,7 +95,7 @@ impl ReaderState {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn insert_at_cursor_hist(&mut self, txt: String) {
|
|
|
|
fn insert_at_cursor_hist(&mut self, txt: String) {
|
|
|
|
let mut line = self.current_hist().expect("Trying to modify a non-existent history entry");
|
|
|
|
let mut line = self.current_hist();
|
|
|
|
line.insert_str(self.cursor, txt.as_str());
|
|
|
|
line.insert_str(self.cursor, txt.as_str());
|
|
|
|
self.history_reset();
|
|
|
|
self.history_reset();
|
|
|
|
self.set_line(line.clone());
|
|
|
|
self.set_line(line.clone());
|
|
|
|
@ -147,9 +145,6 @@ impl ReaderState {
|
|
|
|
if chr == InputChar::Backspace {
|
|
|
|
if chr == InputChar::Backspace {
|
|
|
|
return edit_cmd!(DeleteBack);
|
|
|
|
return edit_cmd!(DeleteBack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if chr == InputChar::EoT {
|
|
|
|
|
|
|
|
return edit_cmd!(Exit);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -177,7 +172,6 @@ impl ReaderState {
|
|
|
|
pub fn read_line(&mut self) -> Result<String, ReadError> {
|
|
|
|
pub fn read_line(&mut self) -> Result<String, ReadError> {
|
|
|
|
use EditCommand::*;
|
|
|
|
use EditCommand::*;
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
self.set_prompt(format!("{:?}> ", self.history_inx));
|
|
|
|
|
|
|
|
let cmd = self.input_line();
|
|
|
|
let cmd = self.input_line();
|
|
|
|
if cmd.is_err() {
|
|
|
|
if cmd.is_err() {
|
|
|
|
return Err(cmd.err().unwrap());
|
|
|
|
return Err(cmd.err().unwrap());
|
|
|
|
@ -203,8 +197,7 @@ impl ReaderState {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HistoryNext => {
|
|
|
|
HistoryNext => {
|
|
|
|
self.history_front();
|
|
|
|
self.history_front();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Exit => return Err(ReadError::Exited)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -233,7 +226,7 @@ impl ReaderState {
|
|
|
|
pub fn history_back(&mut self) {
|
|
|
|
pub fn history_back(&mut self) {
|
|
|
|
let h = self.history.clone();
|
|
|
|
let h = self.history.clone();
|
|
|
|
let mut i = self.history_inx;
|
|
|
|
let mut i = self.history_inx;
|
|
|
|
if i.map_or(h.len() == 0, |v| v+1 == h.len()) {
|
|
|
|
if i.map_or(false, |v| v+1 == h.len()) {
|
|
|
|
bell();
|
|
|
|
bell();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -246,12 +239,7 @@ impl ReaderState {
|
|
|
|
self.history_inx = i;
|
|
|
|
self.history_inx = i;
|
|
|
|
let i: usize = i.unwrap();
|
|
|
|
let i: usize = i.unwrap();
|
|
|
|
let line = h.get(h.len().saturating_sub(i+1));
|
|
|
|
let line = h.get(h.len().saturating_sub(i+1));
|
|
|
|
if line.is_some() {
|
|
|
|
self.set_line(line.unwrap().to_string());
|
|
|
|
self.set_line(line.unwrap().to_string());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
bell();
|
|
|
|
|
|
|
|
self.redraw();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn saved_restore(&mut self) {
|
|
|
|
fn saved_restore(&mut self) {
|
|
|
|
@ -274,12 +262,7 @@ impl ReaderState {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let i = i.unwrap();
|
|
|
|
let i = i.unwrap();
|
|
|
|
let line = h.get(h.len().saturating_sub(i+1));
|
|
|
|
let line = h.get(h.len().saturating_sub(i+1));
|
|
|
|
if line.is_some() {
|
|
|
|
self.set_line(line.unwrap().to_string());
|
|
|
|
self.set_line(line.unwrap().to_string());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
bell();
|
|
|
|
|
|
|
|
self.redraw();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn move_left(&mut self) {
|
|
|
|
pub fn move_left(&mut self) {
|
|
|
|
@ -321,7 +304,7 @@ impl ReaderState {
|
|
|
|
pub fn backspace(&mut self) {
|
|
|
|
pub fn backspace(&mut self) {
|
|
|
|
if self.history_inx.is_some() {
|
|
|
|
if self.history_inx.is_some() {
|
|
|
|
if self.cursor > 0 {
|
|
|
|
if self.cursor > 0 {
|
|
|
|
let mut line = self.current_hist().expect("Trying to modify a non-existent history entry");
|
|
|
|
let mut line = self.current_hist();
|
|
|
|
line.remove(self.cursor-1);
|
|
|
|
line.remove(self.cursor-1);
|
|
|
|
self.move_left();
|
|
|
|
self.move_left();
|
|
|
|
self.history_reset();
|
|
|
|
self.history_reset();
|
|
|
|
|