|
|
|
@ -1,6 +1,6 @@
|
|
|
|
#![allow(unused)]
|
|
|
|
#![allow(unused)]
|
|
|
|
|
|
|
|
|
|
|
|
use std::mem::MaybeUninit;
|
|
|
|
use std::{io::Read, 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,7 +24,8 @@ 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)]
|
|
|
|
@ -35,6 +36,7 @@ pub enum EditCommand {
|
|
|
|
CursorBack,
|
|
|
|
CursorBack,
|
|
|
|
CursorForward,
|
|
|
|
CursorForward,
|
|
|
|
DeleteBack,
|
|
|
|
DeleteBack,
|
|
|
|
|
|
|
|
Exit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
macro_rules! edit_cmd {
|
|
|
|
macro_rules! edit_cmd {
|
|
|
|
@ -82,10 +84,10 @@ impl ReaderState {
|
|
|
|
self.redraw();
|
|
|
|
self.redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn current_hist(&mut self) -> String {
|
|
|
|
fn current_hist(&mut self) -> Option<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.unwrap().to_string();
|
|
|
|
return line.map(|s| s.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn set_current_hist(&mut self, str: String) {
|
|
|
|
fn set_current_hist(&mut self, str: String) {
|
|
|
|
@ -95,7 +97,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();
|
|
|
|
let mut line = self.current_hist().expect("Trying to modify a non-existent history entry");
|
|
|
|
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());
|
|
|
|
@ -145,6 +147,9 @@ 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -172,6 +177,7 @@ 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());
|
|
|
|
@ -197,7 +203,8 @@ impl ReaderState {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
HistoryNext => {
|
|
|
|
HistoryNext => {
|
|
|
|
self.history_front();
|
|
|
|
self.history_front();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Exit => return Err(ReadError::Exited)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -226,7 +233,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(false, |v| v+1 == h.len()) {
|
|
|
|
if i.map_or(h.len() == 0, |v| v+1 == h.len()) {
|
|
|
|
bell();
|
|
|
|
bell();
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -239,7 +246,12 @@ 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));
|
|
|
|
self.set_line(line.unwrap().to_string());
|
|
|
|
if line.is_some() {
|
|
|
|
|
|
|
|
self.set_line(line.unwrap().to_string());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
bell();
|
|
|
|
|
|
|
|
self.redraw();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn saved_restore(&mut self) {
|
|
|
|
fn saved_restore(&mut self) {
|
|
|
|
@ -262,7 +274,12 @@ 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));
|
|
|
|
self.set_line(line.unwrap().to_string());
|
|
|
|
if line.is_some() {
|
|
|
|
|
|
|
|
self.set_line(line.unwrap().to_string());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
bell();
|
|
|
|
|
|
|
|
self.redraw();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn move_left(&mut self) {
|
|
|
|
pub fn move_left(&mut self) {
|
|
|
|
@ -304,7 +321,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();
|
|
|
|
let mut line = self.current_hist().expect("Trying to modify a non-existent history entry");
|
|
|
|
line.remove(self.cursor-1);
|
|
|
|
line.remove(self.cursor-1);
|
|
|
|
self.move_left();
|
|
|
|
self.move_left();
|
|
|
|
self.history_reset();
|
|
|
|
self.history_reset();
|
|
|
|
|