use polymorphism for command spawning

This commit is contained in:
rhpidfyre 2025-01-28 14:07:11 -05:00
parent c080a25603
commit d4ccf3b061
2 changed files with 32 additions and 45 deletions

View File

@ -1,7 +1,7 @@
use std::{io, process, str::SplitWhitespace, path::{Path, PathBuf}};
use std::{process, str::SplitWhitespace, path::{Path, PathBuf}};
use uzers::User;
use crate::{history::History, session::MapDisplay, valid_pbuf::IsValid};
use crate::{session::{MapDisplay, Pse}, valid_pbuf::IsValid};
trait PathBufIsValid {
fn is_valid_or_home(&self) -> Option<PathBuf>;
@ -12,15 +12,8 @@ impl PathBufIsValid for PathBuf {
}
}
trait ChangeDirectory<'a> {
fn change_directory(&self, args: SplitWhitespace) -> Option<PathBuf>;
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf>;
fn specific_user_dir(&self, user: String) -> Option<PathBuf>;
fn cd_args(&self, vec_args: Vec<String>) -> Option<PathBuf>;
fn previous_dir(&self) -> Option<PathBuf>;
fn home_dir(&self) -> Option<PathBuf>;
}
impl<'a> ChangeDirectory<'a> for Command<'a> {
struct ChangeDirectory;
impl ChangeDirectory {
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf> {
std::env::set_current_dir(new_path).map_or_display_none(|()| Some(new_path.to_path_buf()))
}
@ -36,10 +29,10 @@ impl<'a> ChangeDirectory<'a> for Command<'a> {
fn specific_user_dir(&self, requested_user: String) -> Option<PathBuf> {
match requested_user.as_str() {
"root" => PathBuf::from("/root").is_valid_or_home(),
_ => {
u => {
for user in unsafe { uzers::all_users().collect::<Vec<User>>() } {
let user_name = user.name();
if *requested_user == *user_name {
if *u == *user_name {
let mut user_dir = PathBuf::from("/home");
user_dir.push(user_name);
return user_dir.is_valid_or_home();
@ -72,8 +65,8 @@ impl<'a> ChangeDirectory<'a> for Command<'a> {
Some(arg) => match arg.as_str() {
"/" => self.set_current_dir(Path::new("/")),
"-" => self.previous_dir(),
_ => {
let mut arg_chars = arg.chars();
arg_str => {
let mut arg_chars = arg_str.chars();
match arg_chars.next() {
Some(char) => match char == '~' {
true => self.specific_user_dir(arg_chars.collect::<String>()),
@ -87,28 +80,21 @@ impl<'a> ChangeDirectory<'a> for Command<'a> {
}
}
pub struct Command<'a>(&'a String);
impl<'a> Command<'a> {
pub const fn new(input: &'a String) -> Self {
Self(input)
}
pub fn spawn_sys_cmd(&mut self, history: &mut History, command_process: io::Result<process::Child>) {
match command_process {
Ok(mut child) => {
history.add(self.0.as_str());
child.wait().ok();
},
Err(_) => println!("\npse: Unknown command: {}", self.0),
}
}
pub fn exec(&mut self, history: &mut History) {
let mut args = self.0.split_whitespace();
pub trait Command {
fn spawn_sys_cmd(&mut self);
}
impl Command for Pse {
fn spawn_sys_cmd(&mut self) {
let mut args = self.rt.input.split_whitespace();
if let Some(command) = args.next() {
match command {
"cd" => if self.change_directory(args).is_some() { history.add(self.0.as_str()) },
command => { self.spawn_sys_cmd(history, process::Command::new(command).args(args).spawn()); }
"cd" => if ChangeDirectory.change_directory(args).is_some() { self.history.add(&self.rt.input.as_str()) },
command => if let Ok(mut child) = process::Command::new(command).args(args).spawn() {
self.history.add(self.rt.input.as_str());
child.wait().ok();
} else {
println!("\npse: Unknown command: {}", self.rt.input)
}
}
}
}

View File

@ -24,7 +24,7 @@ pub enum InputHandleError {
type InputResult<T> = Result<T, InputHandleError>;
trait SpecificKeybinds {
const TERM_ID_1: &str;
const EXIT_1: &str;
const KEY_SPACE: char;
fn key_literal(&mut self, keycode: KeyCode) -> InputResult<()>;
fn key_ctrl(&mut self, input_key: KeyEvent, keycode: KeyCode) -> InputResult<()>;
@ -34,7 +34,7 @@ trait SpecificKeybinds {
fn key_arrow_left(&mut self) -> InputResult<()>;
}
impl SpecificKeybinds for Pse {
const TERM_ID_1: &str = "exit";
const EXIT_1: &str = "exit";
const KEY_SPACE: char = ' ';
fn key_literal(&mut self, keycode: KeyCode) -> InputResult<()> {
@ -55,10 +55,10 @@ impl SpecificKeybinds for Pse {
}
fn key_enter(&mut self) -> InputResult<()> {
if self.rt.input == Self::TERM_ID_1 { return Err(InputHandleError::UserExit) };
if self.rt.input == Self::EXIT_1 { return Err(InputHandleError::UserExit) };
terminal::disable_raw_mode().map_err(InputHandleError::DisableRaw)?;
Command::new(&self.rt.input).exec(&mut self.history);
self.spawn_sys_cmd();
self.rt.input.clear();
self.term_render_ps()
}
@ -124,13 +124,14 @@ impl TermProcessor for Pse {
fn term_input_mainthread(&mut self) -> io::Result<()> {
execute!(io::stdout(), event::EnableBracketedPaste)?;
self.term_render_ps();
loop {
terminal::enable_raw_mode()?;
if let Event::Key(event) = event::read()? {
if self.term_input_handler(event).is_none() { break Ok(()) }
self.term_render_ps().map_or_else(|_| Ok(()), |()| {
loop {
terminal::enable_raw_mode()?;
if let Event::Key(event) = event::read()? {
if self.term_input_handler(event).is_none() { break Ok(()) }
}
}
}
})
}
fn term_input_processor(&mut self) -> io::Result<()> {