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,114 +4,122 @@ 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 {
match cmd.as_str() { }
"pwd" => {
let pb = std::env::current_dir()?; impl Shell {
println!("{}", pb.as_path().as_os_str().to_str().unwrap()); pub fn new() -> Self {
return Ok(true); Self {}
} }
"cd" => {
let cwd = std::env::current_dir()?; pub fn eval(&self, output: &mut output::Writer, cmd: String, args: Vec<&str>) -> Result<bool> {
if args.len() > 0 { match cmd.as_str() {
let target = cwd.join(args[0]); "pwd" => {
std::env::set_current_dir(target)?; let pb = std::env::current_dir()?;
println!("{}", pb.as_path().as_os_str().to_str().unwrap());
return Ok(true);
} }
return Ok(true); "cd" => {
} let cwd = std::env::current_dir()?;
"printenv" => { if args.len() > 0 {
if args.len() > 0 { let target = cwd.join(args[0]);
let name = args[0]; std::env::set_current_dir(target)?;
match std::env::var(name) { }
Ok(val) => { return Ok(true);
println!("{}", val); }
return Ok(true); "printenv" => {
} if args.len() > 0 {
Err(e) => { let name = args[0];
println!("ERROR {}", e); match std::env::var(name) {
return Ok(false); Ok(val) => {
println!("{}", val);
return Ok(true);
}
Err(e) => {
println!("ERROR {}", e);
return Ok(false);
}
} }
} else {
println!("which variable you fucking dork");
return Ok(false);
} }
} else {
println!("which variable you fucking dork");
return Ok(false);
} }
} "which" => {
"which" => { if args.len() > 0 {
if args.len() > 0 { let path = std::env::var("path").unwrap();
let path = std::env::var("path").unwrap(); let dirs: Vec<&str> = path.split(";").collect();
let dirs: Vec<&str> = path.split(";").collect(); for d in dirs {
for d in dirs { let dir = std::path::Path::new(d);
let dir = std::path::Path::new(d); let fname = dir.join(args[0]).with_extension("exe");
let fname = dir.join(args[0]).with_extension("exe"); if fname.exists() && fname.is_file() {
if fname.exists() && fname.is_file() { println!("{}", fname.to_str().unwrap());
println!("{}", fname.to_str().unwrap()); return Ok(true);
return Ok(true); }
} }
println!("not found: {}", args[0]);
return Ok(false);
} else {
println!("what do you want to look for?");
return Ok(false);
} }
println!("not found: {}", args[0]);
return Ok(false);
} else {
println!("what do you want to look for?");
return Ok(false);
} }
} "tail" => {
"tail" => { if args.len() > 0 {
if args.len() > 0 { let fname = args[0];
let fname = args[0]; match File::options().read(true).open(fname) {
match File::options().read(true).open(fname) { Ok(mut f) => {
Ok(mut f) => { _ = f.seek(SeekFrom::End(0));
_ = f.seek(SeekFrom::End(0)); let mut one_byte: [u8; 1] = [0; 1];
let mut one_byte: [u8; 1] = [0; 1]; let mut buf: Vec<u8> = vec![];
let mut buf: Vec<u8> = vec![]; loop {
loop { match f.read(&mut one_byte) {
match f.read(&mut one_byte) { Ok(n) => {
Ok(n) => { if n == 1 {
if n == 1 { buf.push(one_byte[0]);
buf.push(one_byte[0]); if let Ok(s) = std::str::from_utf8(&buf) {
if let Ok(s) = std::str::from_utf8(&buf) { output.write(s.as_bytes())?;
output.write(s.as_bytes())?; buf.clear();
buf.clear(); }
} }
} }
Err(_) => {}
} }
Err(_) => {}
} }
} }
Err(e) => {
println!("failed to open file: {}", e);
return Err(e.into());
}
} }
Err(e) => { } else {
println!("failed to open file: {}", e); println!("need a file name");
return Err(e.into()); return Ok(false);
}
} }
} else {
println!("need a file name");
return Ok(false);
} }
} "echo" => {
"echo" => { println!("{}", args.join(" "));
println!("{}", args.join(" ")); return Ok(true);
return Ok(true);
}
_ => {
let mut proc = std::process::Command::new(cmd);
if args.len() > 0 {
proc.args(args);
} }
match proc.spawn() { _ => {
Ok(mut child) => { let mut proc = std::process::Command::new(cmd);
if let Err(e) = child.wait() { if args.len() > 0 {
proc.args(args);
}
match proc.spawn() {
Ok(mut child) => {
if let Err(e) = child.wait() {
println!("error: {}", e);
return Err(e.into());
}
}
Err(e) => {
println!("error: {}", e); println!("error: {}", e);
return Err(e.into()); return Ok(false);
} }
} }
Err(e) => { return Ok(true);
println!("error: {}", e);
return Ok(false);
}
} }
return Ok(true);
} }
} }
} }

Loading…
Cancel
Save