This commit is contained in:
2024-12-28 15:34:14 -05:00
parent 4a031d01e9
commit f52217ed9d
8 changed files with 287 additions and 32 deletions

View File

@ -28,10 +28,8 @@ trait ChangeDirectory {
impl PathBufIsValid for PathBuf {
fn is_valid(&self) -> Result<PathBuf, ValidStatus> {
match self.try_exists() {
Ok(root_folder_exist) => match root_folder_exist {
true => Ok(self.to_path_buf()),
false => Err(ValidStatus::NoRootFolder)
},
Ok(true) => Ok(self.to_path_buf()),
Ok(false) => Err(ValidStatus::NoRootFolder),
Err(trye_error) => Err(ValidStatus::TryExists(trye_error))
}
}
@ -52,20 +50,11 @@ impl PathBufIsValid for PathBuf {
impl ChangeDirectory for Command {
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf> {
match std::env::set_current_dir(new_path) {
Ok(()) => Some(new_path.to_path_buf()),
Err(set_cd_err) => {
println!("{set_cd_err}");
None
},
}
std::env::set_current_dir(new_path).map_or_else(|cd_err| {println!("{cd_err}"); None}, |()| Some(new_path.to_path_buf()))
}
fn home_dir(&self) -> Option<PathBuf> {
match home::home_dir() {
Some(home_path_buf) => self.set_current_dir(&home_path_buf),
None => self.set_current_dir(Path::new("/")),
}
home::home_dir().map_or(self.set_current_dir(Path::new("/")), |home_pathbuf| self.set_current_dir(&home_pathbuf))
}
fn previous_dir(&self) -> Option<PathBuf> {
@ -107,6 +96,7 @@ impl ChangeDirectory for Command {
fn change_directory(&self, args: SplitWhitespace) -> Option<PathBuf> {
let vec_args: Vec<String> = args.map(|arg| arg.to_owned()).collect();
match vec_args.first() {
None => self.home_dir(),
Some(arg) => match arg.as_str() {
"/" => self.set_current_dir(Path::new("/")),
"-" => self.previous_dir(),
@ -121,7 +111,6 @@ impl ChangeDirectory for Command {
}
}
},
None => self.home_dir(),
}
}
}
@ -134,18 +123,18 @@ impl Command {
}
pub fn spawn(&self, command_process: io::Result<process::Child>) -> ProcessExitStatus {
match command_process {
match command_process {
Err(e) => {
println!("{e}");
None
},
Ok(mut child) => Some(match child.wait() {
Ok(exit_status) => exit_status,
Err(exit_status_err) => {
println!("{exit_status_err}");
return None;
return None
}
}),
Err(e) => {
println!("{e}");
return None;
}
})
}
}
@ -153,7 +142,7 @@ impl Command {
let mut args = self.0.split_whitespace();
args.next().and_then(|command| match command {
"cd" => {
Self::change_directory(self, args);
self.change_directory(args);
None
}
command => self.spawn(process::Command::new(command).args(args).spawn()),

View File

@ -4,4 +4,7 @@ pub mod shell;
mod commands;
mod ps;
mod rc;
mod rc;
#[path = "./luau/vm.rs"]
mod vm;

0
src/luau/alias.rs Normal file
View File

0
src/luau/print.rs Normal file
View File

60
src/luau/vm.rs Normal file
View File

@ -0,0 +1,60 @@
use mlua::{
Lua as Luau,
Result as lResult,
MultiValue
};
fn new_instance() -> lResult<Luau> {
let instance = Luau::new();
instance.sandbox(true)?;
instance.globals().set("getfenv", mlua::Nil)?;
instance.globals().set("setfenv", mlua::Nil)?;
Ok(instance)
}
fn out(args: MultiValue) -> String {
let mut print: Vec<String> = Vec::new();
let mut print_append = |v: String| {
if !print.is_empty() {
print.push(" ".to_owned());
}
print.push(v)
};
args.iter().for_each(|arg|
arg.to_string().map_or(print_append("<SHELL CONVERSION ERROR>".to_owned()),
|s_arg| print_append(s_arg)
)
);
print.concat()
}
trait Globals {
fn print(&self) -> lResult<()>;
}
impl Globals for Vm {
fn print(&self) -> lResult<()> {
self.0.globals().set("print", self.0.create_function(|_, args: MultiValue| -> lResult<()> {
color_print::cprintln!("{}", out(args));
Ok(())
})?)?;
self.0.globals().set("printraw", self.0.create_function(|_, args: MultiValue| -> lResult<()> {
println!("{}", out(args));
Ok(())
})?)
}
}
struct Vm(Luau);
impl Vm {
pub fn new() -> Option<Self> {
new_instance().map_or(None, |l| Some(Self(l)))
}
fn set_shell_globals(&self) -> mlua::Result<()> {
todo!()
}
pub fn exec(&self, source: String) -> mlua::Result<()> {
self.set_shell_globals().and(self.0.load(source).exec())
}
}

View File

@ -1,5 +1,5 @@
use crate::{ps, commands, rc};
use std::io;
use std::{fs, io::{self}};
pub struct Config {
pub norc: bool
@ -49,9 +49,10 @@ impl LambdaShell {
pub fn start(&mut self) {
let rc_file = match self.config.norc {
true => rc::config_file(),
false => rc::none(),
true => rc::none(),
false => rc::config_file(),
};
ps::display(&self.storage.ps1);
loop {