|
|
@ -18,6 +18,7 @@ use anyhow::{Context, Result};
|
|
|
|
|
|
|
|
|
|
|
|
use crate::{error::Error, log::*};
|
|
|
|
use crate::{error::Error, log::*};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn log_output_mode(mode: Console::CONSOLE_MODE) {
|
|
|
|
fn log_output_mode(mode: Console::CONSOLE_MODE) {
|
|
|
|
// Characters written by the WriteFile or WriteConsole function or echoed by the ReadFile or
|
|
|
|
// Characters written by the WriteFile or WriteConsole function or echoed by the ReadFile or
|
|
|
|
// ReadConsole function are parsed for ASCII control sequences, and the correct action is
|
|
|
|
// ReadConsole function are parsed for ASCII control sequences, and the correct action is
|
|
|
@ -122,8 +123,8 @@ fn setup_stdout() -> Result<()> {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
let handle = stdout_handle()?;
|
|
|
|
let handle = stdout_handle()?;
|
|
|
|
if Console::GetConsoleMode(handle, &mut mode).as_bool() {
|
|
|
|
if Console::GetConsoleMode(handle, &mut mode).as_bool() {
|
|
|
|
debug!("Stdout details:");
|
|
|
|
// debug!("Stdout details:");
|
|
|
|
log_output_mode(mode);
|
|
|
|
// log_output_mode(mode);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return Err(Error::last_error().into());
|
|
|
|
return Err(Error::last_error().into());
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -143,58 +144,61 @@ fn newline(stdout: HANDLE) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn eval(line: String) -> Result<()> {
|
|
|
|
fn eval(cmd: String, args: Vec<&str>) -> Result<bool> {
|
|
|
|
let parts: Vec<&str> = line.split_whitespace().collect();
|
|
|
|
match cmd.as_str() {
|
|
|
|
if parts.len() == 0 {
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let cmd = parts[0];
|
|
|
|
|
|
|
|
match cmd {
|
|
|
|
|
|
|
|
"pwd" => {
|
|
|
|
"pwd" => {
|
|
|
|
let pb = std::env::current_dir()?;
|
|
|
|
let pb = std::env::current_dir()?;
|
|
|
|
println!("{}", pb.as_path().as_os_str().to_str().unwrap());
|
|
|
|
println!("{}", pb.as_path().as_os_str().to_str().unwrap());
|
|
|
|
return Ok(());
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"cd" => {
|
|
|
|
"cd" => {
|
|
|
|
let cwd = std::env::current_dir()?;
|
|
|
|
let cwd = std::env::current_dir()?;
|
|
|
|
if parts.len() > 1 {
|
|
|
|
if args.len() > 0 {
|
|
|
|
let target = cwd.join(parts[1]);
|
|
|
|
let target = cwd.join(args[0]);
|
|
|
|
std::env::set_current_dir(target)?;
|
|
|
|
std::env::set_current_dir(target)?;
|
|
|
|
return Ok(());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"printenv" => {
|
|
|
|
"printenv" => {
|
|
|
|
if parts.len() > 1 {
|
|
|
|
if args.len() > 0 {
|
|
|
|
let name = parts[1];
|
|
|
|
let name = args[0];
|
|
|
|
match std::env::var(name) {
|
|
|
|
match std::env::var(name) {
|
|
|
|
Ok(val) => println!("{}", val),
|
|
|
|
Ok(val) => {
|
|
|
|
Err(e) => println!("ERROR {}", e),
|
|
|
|
println!("{}", val);
|
|
|
|
|
|
|
|
return Ok(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
|
|
println!("ERROR {}", e);
|
|
|
|
|
|
|
|
return Ok(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
println!("which variable you fucking dork");
|
|
|
|
println!("which variable you fucking dork");
|
|
|
|
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"which" => {
|
|
|
|
"which" => {
|
|
|
|
if parts.len() > 1 {
|
|
|
|
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(parts[1]).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(());
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("not found: {}", parts[1]);
|
|
|
|
println!("not found: {}", args[0]);
|
|
|
|
return Ok(());
|
|
|
|
return Ok(false);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
println!("what do you want to look for?");
|
|
|
|
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"tail" => {
|
|
|
|
"tail" => {
|
|
|
|
if parts.len() > 1 {
|
|
|
|
if args.len() > 0 {
|
|
|
|
let fname = parts[1];
|
|
|
|
let fname = args[0];
|
|
|
|
let stdout = stdout_handle()?;
|
|
|
|
let stdout = stdout_handle()?;
|
|
|
|
match File::options().read(true).open(fname) {
|
|
|
|
match File::options().read(true).open(fname) {
|
|
|
|
Ok(mut f) => {
|
|
|
|
Ok(mut f) => {
|
|
|
@ -218,37 +222,40 @@ fn eval(line: String) -> Result<()> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => { println!("failed to open file: {}", e); }
|
|
|
|
Err(e) => {
|
|
|
|
|
|
|
|
println!("failed to open file: {}", e);
|
|
|
|
|
|
|
|
return Err(e.into());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
println!("need a file name");
|
|
|
|
println!("need a file name");
|
|
|
|
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"echo" => {
|
|
|
|
"echo" => {
|
|
|
|
let rest: Vec<&str> = parts[1..].to_vec();
|
|
|
|
println!("{}", args.join(" "));
|
|
|
|
println!("{:?}", rest);
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
_ => {
|
|
|
|
let mut proc = std::process::Command::new(cmd);
|
|
|
|
let mut proc = std::process::Command::new(cmd);
|
|
|
|
if parts.len() > 1 {
|
|
|
|
if args.len() > 0 {
|
|
|
|
let args = parts[1..].to_vec();
|
|
|
|
|
|
|
|
proc.args(args);
|
|
|
|
proc.args(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
match proc.spawn() {
|
|
|
|
match proc.spawn() {
|
|
|
|
Ok(mut child) => {
|
|
|
|
Ok(mut child) => {
|
|
|
|
if let Err(e) = child.wait() {
|
|
|
|
if let Err(e) = child.wait() {
|
|
|
|
println!("error: {}", e)
|
|
|
|
println!("error: {}", e);
|
|
|
|
|
|
|
|
return Err(e.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
Err(e) => {
|
|
|
|
println!("error: {}", e)
|
|
|
|
println!("error: {}", e);
|
|
|
|
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// println!("Error: no such command: {}", cmd);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
|
|
|
fn main() -> Result<()> {
|
|
|
@ -270,24 +277,36 @@ fn main() -> Result<()> {
|
|
|
|
let mut input = input::Reader::new()?;
|
|
|
|
let mut input = input::Reader::new()?;
|
|
|
|
|
|
|
|
|
|
|
|
prompt.print()?;
|
|
|
|
prompt.print()?;
|
|
|
|
|
|
|
|
info!("» enter");
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
// debug!("----------------------------------------");
|
|
|
|
|
|
|
|
match input.next()? {
|
|
|
|
match input.next()? {
|
|
|
|
input::Event::Key(event) => {
|
|
|
|
input::Event::Key(event) => {
|
|
|
|
if event.down {
|
|
|
|
if event.down {
|
|
|
|
if event.code.val == 0 {
|
|
|
|
if event.code.val == 0 {
|
|
|
|
debug!("{}", event);
|
|
|
|
debug!(" {}", event);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
warn!("{}", event);
|
|
|
|
warn!(" {}", event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
info!("{}", event);
|
|
|
|
info!(" {}", event);
|
|
|
|
|
|
|
|
|
|
|
|
if event.code == key::ENTER {
|
|
|
|
if event.code == key::ENTER {
|
|
|
|
newline(stdout)?;
|
|
|
|
newline(stdout)?;
|
|
|
|
if let Err(e) = eval(line.pop()) {
|
|
|
|
let s = line.pop();
|
|
|
|
println!("error: {}", e);
|
|
|
|
let parts: Vec<&str> = s.split_whitespace().collect();
|
|
|
|
|
|
|
|
if parts.len() > 0 {
|
|
|
|
|
|
|
|
let cmd = parts[0].to_string();
|
|
|
|
|
|
|
|
let args = if parts.len() > 1 { parts[1..].to_vec() } else { vec![] };
|
|
|
|
|
|
|
|
debug!("◇ {}", cmd.clone());
|
|
|
|
|
|
|
|
match eval(cmd.clone(), args) {
|
|
|
|
|
|
|
|
Ok(true) => info!("▷ {}", cmd),
|
|
|
|
|
|
|
|
Ok(false) => warn!("▷ {}", cmd),
|
|
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
|
|
error!("▷ {}: {}", cmd, e);
|
|
|
|
|
|
|
|
println!("error: {}", e);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prompt.print()?;
|
|
|
|
prompt.print()?;
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
@ -321,7 +340,7 @@ fn main() -> Result<()> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if event.ctrl && event.code == key::D {
|
|
|
|
if event.ctrl && event.code == key::D {
|
|
|
|
debug!("⎈ d: exit");
|
|
|
|
info!("» exit");
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
CloseHandle(stdout);
|
|
|
|
CloseHandle(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -381,6 +400,7 @@ fn main() -> Result<()> {
|
|
|
|
warn!("‽ {}", event);
|
|
|
|
warn!("‽ {}", event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
input::Event::Left => {
|
|
|
|
input::Event::Left => {
|
|
|
|
|
|
|
|
debug!("⛬ ←");
|
|
|
|
if line.back() {
|
|
|
|
if line.back() {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
let text = "\x1b[D"; // lol this sucks
|
|
|
|
let text = "\x1b[D"; // lol this sucks
|
|
|
@ -389,6 +409,7 @@ fn main() -> Result<()> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input::Event::Right => {
|
|
|
|
input::Event::Right => {
|
|
|
|
|
|
|
|
debug!("⛬ →");
|
|
|
|
if line.forward() {
|
|
|
|
if line.forward() {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
let text = "\x1b[C"; // lol this sucks
|
|
|
|
let text = "\x1b[C"; // lol this sucks
|
|
|
@ -396,8 +417,12 @@ fn main() -> Result<()> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
input::Event::Up => {},
|
|
|
|
input::Event::Up => {
|
|
|
|
input::Event::Down => {},
|
|
|
|
debug!("⛬ ↑");
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
input::Event::Down => {
|
|
|
|
|
|
|
|
debug!("⛬ ↓");
|
|
|
|
|
|
|
|
},
|
|
|
|
input::Event::Home => {},
|
|
|
|
input::Event::Home => {},
|
|
|
|
input::Event::End => {},
|
|
|
|
input::Event::End => {},
|
|
|
|
input::Event::Focus(true) => {},
|
|
|
|
input::Event::Focus(true) => {},
|
|
|
|