removing useless escape sequences

parse-tree
Jordan Orelli 2 years ago
parent 891ed46af9
commit a6fa189490

@ -14,6 +14,7 @@ use std::io::Write;
use anyhow::Result; use anyhow::Result;
use crate::log::*; use crate::log::*;
use shell::Shell;
fn main() -> Result<()> { fn main() -> Result<()> {
match Log::file("C:\\Users\\JordanOrelli\\wash.log") { match Log::file("C:\\Users\\JordanOrelli\\wash.log") {
@ -30,6 +31,7 @@ fn main() -> Result<()> {
let prompt = Prompt::new(); let prompt = Prompt::new();
let mut input = input::Reader::new()?; let mut input = input::Reader::new()?;
let mut output = output::Writer::new()?; let mut output = output::Writer::new()?;
let shell = Shell::new();
prompt.print(&mut output)?; prompt.print(&mut output)?;
info!("» enter"); info!("» enter");
@ -58,7 +60,7 @@ fn main() -> Result<()> {
vec![] vec![]
}; };
debug!("◇ {} {}", cmd.clone(), args.join(" ")); debug!("◇ {} {}", cmd.clone(), args.join(" "));
match shell::eval(&mut output, cmd.clone(), args.clone()) { match shell.eval(&mut output, cmd.clone(), args.clone()) {
Ok(true) => info!("▷ {} {}", cmd, args.join(" ")), Ok(true) => info!("▷ {} {}", cmd, args.join(" ")),
Ok(false) => warn!("▷ {} {}", cmd, args.join(" ")), Ok(false) => warn!("▷ {} {}", cmd, args.join(" ")),
Err(e) => { Err(e) => {
@ -78,8 +80,7 @@ fn main() -> Result<()> {
if event.code == key::BACKSPACE { if event.code == key::BACKSPACE {
if line.backspace() { if line.backspace() {
// move cursor back two spaces // move cursor back two spaces
output.write(b"\x1b[2D")?; output.back(2)?;
// output.back(2)?;
let tail = format!("{} ", line.tail()); let tail = format!("{} ", line.tail());
let n = tail.chars().count(); let n = tail.chars().count();
output.write(tail.as_bytes())?; output.write(tail.as_bytes())?;
@ -87,8 +88,9 @@ fn main() -> Result<()> {
// after writing out the tail, rewind by the number of characters in // after writing out the tail, rewind by the number of characters in
// the tail // the tail
if n > 1 { if n > 1 {
let text = format!("\x1b[{}D", n - 1); // let text = format!("\x1b[{}D", n - 1);
output.write(text.as_bytes())?; output.back(n-1)?;
// output.write(text.as_bytes())?;
} else { } else {
// honestly I can't remember how I figured this out // honestly I can't remember how I figured this out
output.write(b" \x1b[1D")?; output.write(b" \x1b[1D")?;
@ -115,7 +117,7 @@ fn main() -> Result<()> {
// CTRL-L to clear the screen // CTRL-L to clear the screen
if event.ctrl && event.code == key::L { if event.ctrl && event.code == key::L {
info!("» clear"); info!("» clear");
output.write(b"\x1b[2J\x1b[0;0H")?; output.clear()?;
prompt.print(&mut output)?; prompt.print(&mut output)?;
output.write(line.show().as_bytes())?; output.write(line.show().as_bytes())?;
continue; continue;
@ -127,15 +129,13 @@ fn main() -> Result<()> {
let n = line.clear_left(); let n = line.clear_left();
if n > 0 { if n > 0 {
// move left by the number of elements removed // move left by the number of elements removed
let text = format!("\x1b[{}D", n); output.back(n)?;
output.write(text.as_bytes())?;
// draw the elements remaining, followed by a space for each removed // draw the elements remaining, followed by a space for each removed
// element // element
let kept = line.show(); let kept = line.show();
let text = format!("{}{:width$}", kept, "", width = n); let text = format!("{}{:width$}", kept, "", width = n);
output.write(text.as_bytes())?; output.write(text.as_bytes())?;
let text = format!("\x1b[{}D", n + kept.chars().count()); output.back(n + kept.chars().count())?;
output.write(text.as_bytes())?;
} }
continue; continue;
} }
@ -146,8 +146,7 @@ fn main() -> Result<()> {
let n = line.seek_left(); let n = line.seek_left();
if n > 0 { if n > 0 {
// move left by the distance seeked // move left by the distance seeked
let text = format!("\x1b[{}D", n); output.back(n)?;
output.write(text.as_bytes())?;
} }
continue; continue;
} }
@ -158,8 +157,7 @@ fn main() -> Result<()> {
let n = line.seek_right(); let n = line.seek_right();
if n > 0 { if n > 0 {
// move right by the distance seeked // move right by the distance seeked
let text = format!("\x1b[{}C", n); output.forward(n)?;
output.write(text.as_bytes())?;
} }
continue; continue;
} }
@ -177,8 +175,7 @@ fn main() -> Result<()> {
if n > 1 { if n > 1 {
// if we wrote more than one character, because we weren't at the end, we // if we wrote more than one character, because we weren't at the end, we
// need to rewind the terminal cursor to where it was. // need to rewind the terminal cursor to where it was.
let text = format!("\x1b[{}D", n - 1); output.back(n-1)?;
output.write(text.as_bytes())?;
} }
continue; continue;
} }
@ -188,13 +185,13 @@ fn main() -> Result<()> {
input::Event::Left => { input::Event::Left => {
debug!("⛬ ←"); debug!("⛬ ←");
if line.back() { if line.back() {
output.write(b"\x1b[D")?; // lol this sucks output.back(1)?;
} }
} }
input::Event::Right => { input::Event::Right => {
debug!("⛬ →"); debug!("⛬ →");
if line.forward() { if line.forward() {
output.write(b"\x1b[C")?; // lol this sucks output.forward(1)?;
} }
} }
input::Event::Up => { input::Event::Up => {

@ -143,6 +143,23 @@ impl Writer {
self.write(b"\r\n")?; self.write(b"\r\n")?;
Ok(()) Ok(())
} }
pub fn back(&mut self, n: usize) -> Result<()> {
let text = format!("\x1b[{}D", n);
self.write(text.as_bytes())?;
Ok(())
}
pub fn forward(&mut self, n: usize) -> Result<()> {
let text = format!("\x1b[{}C", n);
self.write(text.as_bytes())?;
Ok(())
}
pub fn clear(&mut self) -> Result<()> {
self.write(b"\x1b[2J\x1b[0;0H")?;
Ok(())
}
} }
impl Write for Writer { impl Write for Writer {

@ -4,7 +4,15 @@ use crate::output;
use anyhow::Result; use anyhow::Result;
pub fn eval(output: &mut output::Writer, cmd: String, args: Vec<&str>) -> Result<bool> { pub struct Shell {
}
impl Shell {
pub fn new() -> Self {
Self {}
}
pub fn eval(&self, output: &mut output::Writer, cmd: String, args: Vec<&str>) -> Result<bool> {
match cmd.as_str() { match cmd.as_str() {
"pwd" => { "pwd" => {
let pb = std::env::current_dir()?; let pb = std::env::current_dir()?;
@ -114,4 +122,4 @@ pub fn eval(output: &mut output::Writer, cmd: String, args: Vec<&str>) -> Result
} }
} }
} }
}

Loading…
Cancel
Save