Initial Cargo fmt

master
itycodes 4 hours ago
parent 12824e638c
commit 6156b73e20

@ -1,6 +1,9 @@
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Direction { pub enum Direction {
Up, Down, Right, Left, Up,
Down,
Right,
Left,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
@ -43,7 +46,7 @@ macro_rules! bad_seq {
impl SequenceState { impl SequenceState {
pub fn new() -> Self { pub fn new() -> Self {
return Self::default() return Self::default();
} }
pub fn has_next(&self) -> bool { pub fn has_next(&self) -> bool {
@ -71,7 +74,7 @@ impl SequenceState {
self.seq_buf.push(chr); self.seq_buf.push(chr);
Result::Ok(()) Result::Ok(())
} }
_ => bad_seq!(self.seq_buf.clone()) _ => bad_seq!(self.seq_buf.clone()),
} }
// CSI parsing // CSI parsing
} else if self.in_seq && self.in_csi { } else if self.in_seq && self.in_csi {
@ -80,15 +83,17 @@ impl SequenceState {
b'0'..b'9' => bad_seq!(self.seq_buf.clone()), b'0'..b'9' => bad_seq!(self.seq_buf.clone()),
b'A'..=b'D' => { b'A'..=b'D' => {
assert_eq!(self.seq_buf, format!("[{}", chr as char).as_bytes()); assert_eq!(self.seq_buf, format!("[{}", chr as char).as_bytes());
let dir = [Direction::Up, let dir = [
Direction::Down, Direction::Up,
Direction::Right, Direction::Down,
Direction::Left]; Direction::Right,
let dir = dir[(chr-b'A') as usize].clone(); Direction::Left,
];
let dir = dir[(chr - b'A') as usize].clone();
self.out_buf.push(InputChar::CursorMotion(dir)); self.out_buf.push(InputChar::CursorMotion(dir));
self.clean_escape() self.clean_escape()
} }
_ => bad_seq!(self.seq_buf.clone()) _ => bad_seq!(self.seq_buf.clone()),
} }
// ASCII printable characters // ASCII printable characters
@ -104,8 +109,7 @@ impl SequenceState {
else if chr == 0x7F { else if chr == 0x7F {
self.out_buf.push(InputChar::Backspace); self.out_buf.push(InputChar::Backspace);
Result::Ok(()) Result::Ok(())
} } else if chr == 0x04 {
else if chr == 0x04 {
self.out_buf.push(InputChar::EoT); self.out_buf.push(InputChar::EoT);
Result::Ok(()) Result::Ok(())
} }
@ -166,7 +170,6 @@ pub fn clear_line() {
pub fn hide_cursor() { pub fn hide_cursor() {
eprint!("\x1b[?25l"); eprint!("\x1b[?25l");
} }
pub fn show_cursor() { pub fn show_cursor() {
@ -175,4 +178,4 @@ pub fn show_cursor() {
pub fn bell() { pub fn bell() {
eprint!("\x07"); eprint!("\x07");
} }

@ -20,7 +20,7 @@ pub struct ReaderState {
#[derive(Debug)] #[derive(Debug)]
pub enum InitTTYError { pub enum InitTTYError {
NotATTY NotATTY,
} }
#[derive(Debug)] #[derive(Debug)]
@ -51,7 +51,11 @@ macro_rules! edit_cmd {
impl ReaderState { impl ReaderState {
pub fn new(prompt: &str) -> Self { pub fn new(prompt: &str) -> Self {
ReaderState { prompt: String::from(prompt), max_len: prompt.len(), ..Default::default() } ReaderState {
prompt: String::from(prompt),
max_len: prompt.len(),
..Default::default()
}
} }
pub fn print(&mut self, txt: String) { pub fn print(&mut self, txt: String) {
eprint!("{}", txt); eprint!("{}", txt);
@ -66,7 +70,7 @@ impl ReaderState {
} }
pub fn init_tty(&mut self) -> Result<(), InitTTYError> { pub fn init_tty(&mut self) -> Result<(), InitTTYError> {
unsafe { unsafe {
use libc::{isatty, termios, ECHO, ICANON, INPCK, TCSANOW, tcgetattr, tcsetattr}; use libc::{ECHO, ICANON, INPCK, TCSANOW, isatty, tcgetattr, tcsetattr, termios};
if !(isatty(0) == 1) { if !(isatty(0) == 1) {
return Result::Err(InitTTYError::NotATTY); return Result::Err(InitTTYError::NotATTY);
} }
@ -82,7 +86,7 @@ impl ReaderState {
pub fn restore_tty(&self) -> Result<(), ()> { pub fn restore_tty(&self) -> Result<(), ()> {
unsafe { unsafe {
use libc::{tcsetattr, TCSANOW}; use libc::{TCSANOW, tcsetattr};
if self.saved_termios.is_none() { if self.saved_termios.is_none() {
Err(()) Err(())
} else { } else {
@ -101,18 +105,20 @@ impl ReaderState {
fn current_hist(&mut self) -> Option<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.map(|s| s.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) {
let i: usize = self.history_inx.unwrap(); let i: usize = self.history_inx.unwrap();
let len = self.history.len(); let len = self.history.len();
self.history[len.saturating_sub(i+1)] = str; self.history[len.saturating_sub(i + 1)] = str;
} }
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()
.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());
@ -133,7 +139,7 @@ impl ReaderState {
let mut read_stat = esc_parse::SequenceState::new(); let mut read_stat = esc_parse::SequenceState::new();
let mut out_buf: Vec<esc_parse::InputChar> = Vec::new(); let mut out_buf: Vec<esc_parse::InputChar> = Vec::new();
unsafe { unsafe {
use libc::{read, c_void}; use libc::{c_void, read};
self.redraw(); self.redraw();
loop { loop {
read(0, esc_buf.as_mut_ptr() as *mut c_void, 1); read(0, esc_buf.as_mut_ptr() as *mut c_void, 1);
@ -154,7 +160,7 @@ impl ReaderState {
Down => edit_cmd!(HistoryNext), Down => edit_cmd!(HistoryNext),
Left => edit_cmd!(CursorBack), Left => edit_cmd!(CursorBack),
Right => edit_cmd!(CursorForward), Right => edit_cmd!(CursorForward),
} };
} }
if chr == InputChar::NewLine { if chr == InputChar::NewLine {
break; break;
@ -203,24 +209,22 @@ impl ReaderState {
let line = self.line.clone(); let line = self.line.clone();
self.submit_line(line.clone()); self.submit_line(line.clone());
println!("{}", line); println!("{}", line);
return Ok(line) return Ok(line);
}, }
CursorBack => { CursorBack => self.move_left(),
self.move_left()
},
CursorForward => { CursorForward => {
self.move_right(); self.move_right();
}, }
DeleteBack => { DeleteBack => {
self.backspace(); self.backspace();
}, }
HistoryPrevious => { HistoryPrevious => {
self.history_back(); self.history_back();
}, }
HistoryNext => { HistoryNext => {
self.history_front(); self.history_front();
}, }
Exit => return Err(ReadError::Exited) Exit => return Err(ReadError::Exited),
} }
} }
} }
@ -249,7 +253,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(h.len() == 0, |v| v + 1 == h.len()) {
bell(); bell();
return; return;
} }
@ -257,11 +261,11 @@ impl ReaderState {
i = Some(0); i = Some(0);
self.history_saved = Some(self.line.clone()); self.history_saved = Some(self.line.clone());
} else { } else {
i = i.map(|v| { v + 1 }); i = i.map(|v| v + 1);
} }
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() { if line.is_some() {
self.set_line(line.unwrap().to_string()); self.set_line(line.unwrap().to_string());
} else { } else {
@ -285,11 +289,11 @@ impl ReaderState {
self.history_reset(); self.history_reset();
return; return;
} else { } else {
i = i.map(|v| { v - 1 }); i = i.map(|v| v - 1);
self.history_inx = i; self.history_inx = i;
} }
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() { if line.is_some() {
self.set_line(line.unwrap().to_string()); self.set_line(line.unwrap().to_string());
} else { } else {
@ -299,7 +303,7 @@ impl ReaderState {
} }
pub fn move_left(&mut self) { pub fn move_left(&mut self) {
use esc_parse::{cursor_left, bell}; use esc_parse::{bell, cursor_left};
if self.cursor > 0 { if self.cursor > 0 {
self.cursor -= 1; self.cursor -= 1;
cursor_left(); cursor_left();
@ -310,7 +314,7 @@ impl ReaderState {
} }
pub fn move_right(&mut self) { pub fn move_right(&mut self) {
use esc_parse::{cursor_right, bell}; use esc_parse::{bell, cursor_right};
if self.cursor < self.line.len() { if self.cursor < self.line.len() {
self.cursor += 1; self.cursor += 1;
cursor_right(); cursor_right();
@ -326,7 +330,7 @@ impl ReaderState {
self.max_len = inx; self.max_len = inx;
} }
esc_parse::cursor_left_nr(self.max_len); esc_parse::cursor_left_nr(self.max_len);
esc_parse::cursor_right_nr(inx+self.prompt.len()); esc_parse::cursor_right_nr(inx + self.prompt.len());
} }
pub fn cursor_at(&mut self, inx: usize) { pub fn cursor_at(&mut self, inx: usize) {
@ -337,8 +341,10 @@ 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
line.remove(self.cursor-1); .current_hist()
.expect("Trying to modify a non-existent history entry");
line.remove(self.cursor - 1);
self.move_left(); self.move_left();
self.history_reset(); self.history_reset();
self.set_line(line); self.set_line(line);
@ -347,11 +353,11 @@ impl ReaderState {
} }
} else { } else {
if self.cursor > 0 { if self.cursor > 0 {
self.line.remove(self.cursor-1); self.line.remove(self.cursor - 1);
self.move_left(); self.move_left();
} else { } else {
bell(); bell();
} }
} }
} }
} }

Loading…
Cancel
Save