This commit is contained in:
Raptorox 2025-09-20 23:18:12 +02:00
parent d7fc72e65d
commit 03131fda71
No known key found for this signature in database
GPG key ID: 8B3556FC3ED1F6D8
3 changed files with 83 additions and 45 deletions

View file

@ -1,12 +1,12 @@
use std::{
env,
io::{self, stdin, stdout, Read, Stdin, Stdout, Write},
io::{self, Read, Stdin, Stdout, Write, stdin, stdout},
process::Command,
str::SplitWhitespace,
};
mod raw;
use raw::{enable_raw_mode, disable_raw_mode};
use raw::{disable_raw_mode, enable_raw_mode};
mod key;
use key::Key;
@ -16,7 +16,11 @@ use data::Data;
fn print_prompt(pre_input: &str, data: &mut Data, newline: bool) -> io::Result<()> {
if newline {
print!("\r\n{}\r\n> {}", data.get_current_path().display(), pre_input);
print!(
"\r\n{}\r\n> {}",
data.get_current_path().display(),
pre_input
);
} else {
print!("\r> {}", pre_input);
}
@ -55,21 +59,23 @@ fn handle_input(stdin: &mut Stdin, stdout: &mut Stdout, data: &mut Data) -> io::
Key::Escape => continue,
Key::ArrowUp => {
if let Some(hist_command) = data.get_prev_hist_item() {
if data.at_hist_end() { data.save_command(input.clone()); }
if data.at_hist_end() {
data.save_command(input.clone());
}
flush_prompt(input.len())?;
input = hist_command.clone();
print_prompt(&input, data, false)?;
}
},
}
Key::ArrowDown => {
if let Some(hist_command) = data.get_next_hist_item() {
flush_prompt(input.len())?;
input = hist_command.clone();
print_prompt(&input, data, false)?;
}
},
Key::ArrowLeft => {},
Key::ArrowRight => {},
}
Key::ArrowLeft => {}
Key::ArrowRight => {}
Key::Char(c) => {
input.push(c);
write!(stdout, "{c}")?;
@ -94,13 +100,15 @@ where
fn run_command(command: &str, args: SplitWhitespace, data: &mut Data) -> Result<u8, String> {
match command {
"exit" => return Ok(1),
"exit" => Ok(1),
"cd" => {
let path = args.peekable().peek().map_or("~", |dir| *dir);
let mut new_path = data.get_current_path();
if path.chars().nth(0).unwrap() == '/' { new_path.push("/"); }
if path.starts_with('/') {
new_path.push("/");
}
for subpath in path.split("/") {
match subpath {
"-" => {
@ -129,8 +137,11 @@ fn run_command(command: &str, args: SplitWhitespace, data: &mut Data) -> Result<
let child = Command::new(command).args(args).spawn();
match child {
Ok(mut child) => { child.wait().unwrap(); Ok(0) }
Err(e) => { return Err(e.to_string()); }
Ok(mut child) => {
child.wait().unwrap();
Ok(0)
}
Err(e) => Err(e.to_string()),
}
}
}
@ -145,17 +156,20 @@ fn rush_loop(data: &mut Data, orig: libc::termios) -> io::Result<()> {
let input = handle_input(&mut stdin, &mut stdout, data)?;
let input = input.trim();
if input.is_empty() { continue; }
if input.is_empty() {
continue;
}
let input = input.split_whitespace();
let (command, args) = parse_input(input);
disable_raw_mode(&orig);
match run_command(command, args, data) {
Ok(status) => match status {
1 => return Ok(()),
_ => {}
},
Err(e) => eprint!("{e}\r\n")
Ok(status) => {
if status == 1 {
return Ok(());
}
}
Err(e) => eprint!("{e}\r\n"),
}
enable_raw_mode();
}