Compare commits
4 Commits
restructur
...
e985236188
Author | SHA1 | Date | |
---|---|---|---|
e985236188 | |||
d4ccf3b061 | |||
c080a25603 | |||
0282b26265 |
16
Cargo.lock
generated
16
Cargo.lock
generated
@ -118,6 +118,12 @@ version = "1.13.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "env_home"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c7f84e12ccf0a7ddc17a6c41c93326024c42920d7ee630d04950e6926645c0fe"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "errno"
|
name = "errno"
|
||||||
version = "0.3.10"
|
version = "0.3.10"
|
||||||
@ -225,9 +231,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "luajit-src"
|
name = "luajit-src"
|
||||||
version = "210.5.11+97813fb"
|
version = "210.5.12+a4f56a4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3015551c284515db7c30c559fc1080f9cb9ee990d1f6fca315451a107c7540bb"
|
checksum = "b3a8e7962a5368d5f264d045a5a255e90f9aa3fc1941ae15a8d2940d42cac671"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cc",
|
"cc",
|
||||||
"which",
|
"which",
|
||||||
@ -597,12 +603,12 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "which"
|
name = "which"
|
||||||
version = "6.0.3"
|
version = "7.0.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b4ee928febd44d98f2f459a4a79bd4d928591333a494a10a868418ac1b39cf1f"
|
checksum = "fb4a9e33648339dc1642b0e36e21b3385e6148e289226f657c809dee59df5028"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"either",
|
"either",
|
||||||
"home",
|
"env_home",
|
||||||
"rustix",
|
"rustix",
|
||||||
"winsafe",
|
"winsafe",
|
||||||
]
|
]
|
||||||
|
@ -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!("pse: 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
105
src/terminal.rs
105
src/terminal.rs
@ -1,4 +1,4 @@
|
|||||||
use crossterm::{event::{self, Event, KeyCode, KeyEvent, KeyModifiers}, terminal};
|
use crossterm::{cursor, event::{self, Event, KeyCode, KeyEvent, KeyModifiers}, execute, terminal};
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
@ -8,8 +8,8 @@ use crate::{commands::Command, session::{self, Pse}};
|
|||||||
pub enum InputHandleError {
|
pub enum InputHandleError {
|
||||||
#[error("UserExit")]
|
#[error("UserExit")]
|
||||||
UserExit,
|
UserExit,
|
||||||
#[error("Sigterm")]
|
#[error("Sigint")]
|
||||||
Sigterm,
|
Sigint,
|
||||||
#[error("Render failure: {0}")]
|
#[error("Render failure: {0}")]
|
||||||
Write(io::Error),
|
Write(io::Error),
|
||||||
#[error("Flush failure: {0}")]
|
#[error("Flush failure: {0}")]
|
||||||
@ -24,62 +24,83 @@ 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;
|
||||||
|
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<()>;
|
||||||
fn key_enter(&mut self) -> InputResult<()>;
|
fn key_enter(&mut self) -> InputResult<()>;
|
||||||
fn key_backspace(&mut self) -> InputResult<()>;
|
fn key_backspace(&mut self) -> InputResult<()>;
|
||||||
|
fn key_arrow_right(&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 = ' ';
|
||||||
|
|
||||||
|
fn key_literal(&mut self, keycode: KeyCode) -> InputResult<()> {
|
||||||
|
match keycode {
|
||||||
|
KeyCode::Char(Self::KEY_SPACE) => self.term_render(Self::KEY_SPACE.to_string()),
|
||||||
|
keycode => self.term_render(keycode.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn key_ctrl(&mut self, input_key: KeyEvent, keycode: KeyCode) -> InputResult<()> {
|
fn key_ctrl(&mut self, input_key: KeyEvent, keycode: KeyCode) -> InputResult<()> {
|
||||||
if input_key.modifiers.contains(KeyModifiers::CONTROL) {
|
match input_key.modifiers.contains(KeyModifiers::CONTROL) {
|
||||||
match keycode {
|
true => match keycode {
|
||||||
KeyCode::Char('c') => Err(InputHandleError::Sigterm),
|
KeyCode::Char('c') => Err(InputHandleError::Sigint),
|
||||||
_ => Ok(())
|
_ => Ok(())
|
||||||
}
|
},
|
||||||
} else {
|
false => self.key_literal(keycode)
|
||||||
self.term_render(Some(keycode.to_string()))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
Ok(())
|
self.term_render_ps()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn key_backspace(&mut self) -> InputResult<()> {
|
fn key_backspace(&mut self) -> InputResult<()> {
|
||||||
match self.rt.input.pop() {
|
if self.rt.input.pop().is_some() {
|
||||||
Some(_) => self.term_render(None),
|
execute!(
|
||||||
None => {
|
io::stdout(),
|
||||||
//the string is empty, do terminal beep
|
cursor::MoveLeft(1),
|
||||||
Ok(())
|
terminal::Clear(terminal::ClearType::UntilNewLine)
|
||||||
},
|
).map_err(InputHandleError::Flush)
|
||||||
|
} else {
|
||||||
|
//the string is empty, do terminal beep
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn key_arrow_right(&mut self) -> InputResult<()> {
|
||||||
|
execute!(io::stdout(), cursor::MoveRight(1)).map_err(InputHandleError::Flush)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn key_arrow_left(&mut self) -> InputResult<()> {
|
||||||
|
execute!(io::stdout(), cursor::MoveLeft(1)).map_err(InputHandleError::Flush)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait TermProcessor {
|
pub trait TermProcessor {
|
||||||
fn term_render(&mut self, def: Option<String>) -> InputResult<()>;
|
fn term_render(&mut self, def_string: String) -> InputResult<()>;
|
||||||
|
fn term_render_ps(&self) -> InputResult<()>;
|
||||||
fn term_input_handler(&mut self, input_key: KeyEvent) -> Option<()>;
|
fn term_input_handler(&mut self, input_key: KeyEvent) -> Option<()>;
|
||||||
fn term_input_mainthread(&mut self) -> io::Result<()>;
|
fn term_input_mainthread(&mut self) -> io::Result<()>;
|
||||||
fn term_input_processor(&mut self) -> io::Result<()>;
|
fn term_input_processor(&mut self) -> io::Result<()>;
|
||||||
}
|
}
|
||||||
impl TermProcessor for Pse {
|
impl TermProcessor for Pse {
|
||||||
fn term_render(&mut self, def: Option<String>) -> InputResult<()> {
|
fn term_render(&mut self, def_string: String) -> InputResult<()> {
|
||||||
match def {
|
self.rt.input.push_str(&def_string);
|
||||||
Some(def_string) => {
|
write!(io::stdout(), "{def_string}").map_err(InputHandleError::Write)?;
|
||||||
self.rt.input.push_str(&def_string);
|
io::stdout().flush().map_err(InputHandleError::Flush)
|
||||||
write!(io::stdout(), "{def_string}").map_err(InputHandleError::Write)?;
|
}
|
||||||
},
|
|
||||||
None => {
|
fn term_render_ps(&self) -> InputResult<()> {
|
||||||
write!(io::stdout(), "{}", self.rt.input).map_err(InputHandleError::Write)?
|
print!("{}", self.rt.ps.borrow());
|
||||||
}
|
|
||||||
};
|
|
||||||
io::stdout().flush().map_err(InputHandleError::Flush)
|
io::stdout().flush().map_err(InputHandleError::Flush)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,32 +109,34 @@ impl TermProcessor for Pse {
|
|||||||
KeyCode::Enter => self.key_enter(),
|
KeyCode::Enter => self.key_enter(),
|
||||||
KeyCode::Backspace => self.key_backspace(),
|
KeyCode::Backspace => self.key_backspace(),
|
||||||
KeyCode::Tab => todo!(),
|
KeyCode::Tab => todo!(),
|
||||||
KeyCode::Right => todo!(),
|
KeyCode::Right => self.key_arrow_right(),
|
||||||
KeyCode::Left => todo!(),
|
KeyCode::Left => self.key_arrow_left(),
|
||||||
KeyCode::Up => todo!(),
|
KeyCode::Up => todo!(),
|
||||||
KeyCode::Down => todo!(),
|
KeyCode::Down => todo!(),
|
||||||
keycode => self.key_ctrl(input_key, keycode)
|
keycode => self.key_ctrl(input_key, keycode)
|
||||||
};
|
};
|
||||||
input_handle.map_or_else(|inp_err| match inp_err {
|
input_handle.map_or_else(|inp_err| match inp_err {
|
||||||
InputHandleError::UserExit => None,
|
InputHandleError::UserExit => None,
|
||||||
InputHandleError::Sigterm => self.term_render(Some("^C".to_owned())).ok(),
|
InputHandleError::Sigint => self.term_render("^C".to_owned()).ok(),
|
||||||
input_err => session::shell_error_none(input_err)
|
input_err => session::shell_error_none(input_err)
|
||||||
}, Some)
|
}, Some)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn term_input_mainthread(&mut self) -> io::Result<()> {
|
fn term_input_mainthread(&mut self) -> io::Result<()> {
|
||||||
crossterm::execute!(io::stdout(), event::EnableBracketedPaste)?;
|
execute!(io::stdout(), event::EnableBracketedPaste)?;
|
||||||
loop {
|
self.term_render_ps().map_or_else(|_| Ok(()), |()| {
|
||||||
terminal::enable_raw_mode()?;
|
loop {
|
||||||
if let Event::Key(event) = event::read()? {
|
terminal::enable_raw_mode()?;
|
||||||
if self.term_input_handler(event).is_none() { break Ok(()) }
|
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<()> {
|
fn term_input_processor(&mut self) -> io::Result<()> {
|
||||||
self.term_input_mainthread()?;
|
self.term_input_mainthread()?;
|
||||||
terminal::disable_raw_mode()?;
|
terminal::disable_raw_mode()?;
|
||||||
crossterm::execute!(io::stdout(), event::DisableBracketedPaste)
|
execute!(io::stdout(), event::DisableBracketedPaste)
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user