use polymorphism for command spawning
This commit is contained in:
parent
c080a25603
commit
d4ccf3b061
@ -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 uzers::User;
|
||||||
|
|
||||||
use crate::{history::History, session::MapDisplay, valid_pbuf::IsValid};
|
use crate::{session::{MapDisplay, Pse}, valid_pbuf::IsValid};
|
||||||
|
|
||||||
trait PathBufIsValid {
|
trait PathBufIsValid {
|
||||||
fn is_valid_or_home(&self) -> Option<PathBuf>;
|
fn is_valid_or_home(&self) -> Option<PathBuf>;
|
||||||
@ -12,15 +12,8 @@ impl PathBufIsValid for PathBuf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait ChangeDirectory<'a> {
|
struct ChangeDirectory;
|
||||||
fn change_directory(&self, args: SplitWhitespace) -> Option<PathBuf>;
|
impl ChangeDirectory {
|
||||||
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> {
|
|
||||||
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf> {
|
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()))
|
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> {
|
fn specific_user_dir(&self, requested_user: String) -> Option<PathBuf> {
|
||||||
match requested_user.as_str() {
|
match requested_user.as_str() {
|
||||||
"root" => PathBuf::from("/root").is_valid_or_home(),
|
"root" => PathBuf::from("/root").is_valid_or_home(),
|
||||||
_ => {
|
u => {
|
||||||
for user in unsafe { uzers::all_users().collect::<Vec<User>>() } {
|
for user in unsafe { uzers::all_users().collect::<Vec<User>>() } {
|
||||||
let user_name = user.name();
|
let user_name = user.name();
|
||||||
if *requested_user == *user_name {
|
if *u == *user_name {
|
||||||
let mut user_dir = PathBuf::from("/home");
|
let mut user_dir = PathBuf::from("/home");
|
||||||
user_dir.push(user_name);
|
user_dir.push(user_name);
|
||||||
return user_dir.is_valid_or_home();
|
return user_dir.is_valid_or_home();
|
||||||
@ -72,8 +65,8 @@ impl<'a> ChangeDirectory<'a> for Command<'a> {
|
|||||||
Some(arg) => match arg.as_str() {
|
Some(arg) => match arg.as_str() {
|
||||||
"/" => self.set_current_dir(Path::new("/")),
|
"/" => self.set_current_dir(Path::new("/")),
|
||||||
"-" => self.previous_dir(),
|
"-" => self.previous_dir(),
|
||||||
_ => {
|
arg_str => {
|
||||||
let mut arg_chars = arg.chars();
|
let mut arg_chars = arg_str.chars();
|
||||||
match arg_chars.next() {
|
match arg_chars.next() {
|
||||||
Some(char) => match char == '~' {
|
Some(char) => match char == '~' {
|
||||||
true => self.specific_user_dir(arg_chars.collect::<String>()),
|
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);
|
pub trait Command {
|
||||||
impl<'a> Command<'a> {
|
fn spawn_sys_cmd(&mut self);
|
||||||
pub const fn new(input: &'a String) -> Self {
|
}
|
||||||
Self(input)
|
impl Command for Pse {
|
||||||
}
|
fn spawn_sys_cmd(&mut self) {
|
||||||
|
let mut args = self.rt.input.split_whitespace();
|
||||||
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();
|
|
||||||
if let Some(command) = args.next() {
|
if let Some(command) = args.next() {
|
||||||
match command {
|
match command {
|
||||||
"cd" => if self.change_directory(args).is_some() { history.add(self.0.as_str()) },
|
"cd" => if ChangeDirectory.change_directory(args).is_some() { self.history.add(&self.rt.input.as_str()) },
|
||||||
command => { self.spawn_sys_cmd(history, process::Command::new(command).args(args).spawn()); }
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ pub enum InputHandleError {
|
|||||||
type InputResult<T> = Result<T, InputHandleError>;
|
type InputResult<T> = Result<T, InputHandleError>;
|
||||||
|
|
||||||
trait SpecificKeybinds {
|
trait SpecificKeybinds {
|
||||||
const TERM_ID_1: &str;
|
const EXIT_1: &str;
|
||||||
const KEY_SPACE: char;
|
const KEY_SPACE: char;
|
||||||
fn key_literal(&mut self, keycode: KeyCode) -> InputResult<()>;
|
fn key_literal(&mut self, keycode: KeyCode) -> InputResult<()>;
|
||||||
fn key_ctrl(&mut self, input_key: KeyEvent, 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<()>;
|
fn key_arrow_left(&mut self) -> InputResult<()>;
|
||||||
}
|
}
|
||||||
impl SpecificKeybinds for Pse {
|
impl SpecificKeybinds for Pse {
|
||||||
const TERM_ID_1: &str = "exit";
|
const EXIT_1: &str = "exit";
|
||||||
const KEY_SPACE: char = ' ';
|
const KEY_SPACE: char = ' ';
|
||||||
|
|
||||||
fn key_literal(&mut self, keycode: KeyCode) -> InputResult<()> {
|
fn key_literal(&mut self, keycode: KeyCode) -> InputResult<()> {
|
||||||
@ -55,10 +55,10 @@ impl SpecificKeybinds for Pse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn key_enter(&mut self) -> InputResult<()> {
|
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)?;
|
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.rt.input.clear();
|
||||||
self.term_render_ps()
|
self.term_render_ps()
|
||||||
}
|
}
|
||||||
@ -124,13 +124,14 @@ impl TermProcessor for Pse {
|
|||||||
|
|
||||||
fn term_input_mainthread(&mut self) -> io::Result<()> {
|
fn term_input_mainthread(&mut self) -> io::Result<()> {
|
||||||
execute!(io::stdout(), event::EnableBracketedPaste)?;
|
execute!(io::stdout(), event::EnableBracketedPaste)?;
|
||||||
self.term_render_ps();
|
self.term_render_ps().map_or_else(|_| Ok(()), |()| {
|
||||||
loop {
|
loop {
|
||||||
terminal::enable_raw_mode()?;
|
terminal::enable_raw_mode()?;
|
||||||
if let Event::Key(event) = event::read()? {
|
if let Event::Key(event) = event::read()? {
|
||||||
if self.term_input_handler(event).is_none() { break Ok(()) }
|
if self.term_input_handler(event).is_none() { break Ok(()) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn term_input_processor(&mut self) -> io::Result<()> {
|
fn term_input_processor(&mut self) -> io::Result<()> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user