Compare commits

...

3 Commits

Author SHA1 Message Date
e578f408c5 SHELL.SYSTEM 2025-01-10 12:55:59 -05:00
d95366bebe Working SHELL.PROMPT = and SHELL.PROMPT, SHELL is now a userdata 2025-01-10 12:05:35 -05:00
97b08d24e3 update cargo 2025-01-08 18:41:00 -05:00
7 changed files with 81 additions and 95 deletions

16
Cargo.lock generated
View File

@ -194,9 +194,9 @@ dependencies = [
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.4.14" version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
@ -384,9 +384,9 @@ checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497"
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.42" version = "0.38.43"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85" checksum = "a78891ee6bf2340288408954ac787aa063d8e8817e9f53abb37c695c6d834ef6"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"errno", "errno",
@ -476,18 +476,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "2.0.9" version = "2.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f072643fd0190df67a8bab670c20ef5d8737177d6ac6b2e9a236cb096206b2cc" checksum = "a3ac7f54ca534db81081ef1c1e7f6ea8a3ef428d2fc069097c079443d24124d3"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "2.0.9" version = "2.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b50fa271071aae2e6ee85f842e2e28ba8cd2c5fb67f11fcb1fd70b276f9e7d4" checksum = "9e9465d30713b56a37ede7185763c3492a91be2f5fa68d958c44e41ab9248beb"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@ -11,10 +11,4 @@ home = "0.5.9"
mlua = { version = "0.10.0", features = ["luau-jit", "vendored"] } mlua = { version = "0.10.0", features = ["luau-jit", "vendored"] }
thiserror = "2.0.9" thiserror = "2.0.9"
uzers = "0.12.1" uzers = "0.12.1"
whoami = "1.5.2" whoami = "1.5.2"
[profile.release]
strip = true
opt-level = "z"
lto = true
codegen-units = 1

View File

@ -1,27 +1,28 @@
pub const DEFAULT_PS: &str = concat!("lambdashell-", env!("CARGO_PKG_VERSION"), " "); pub const DEFAULT_PS: &str = concat!("lambdashell-", env!("CARGO_PKG_VERSION"), " ");
#[derive(Debug, Clone)] #[derive(Debug)]
pub struct Ps(String); pub struct Ps(String);
impl Ps { impl Ps {
pub fn set(prompt: String) -> Self { pub const fn set(prompt: String) -> Self {
Self(prompt) Self(prompt)
} }
pub fn get(self) -> String { //rustc: `std::string::String::as_str` is not yet stable as a const fn
self.0 pub fn get(&self) -> &str {
self.0.as_str()
}
pub fn modify(&mut self, prompt: String) {
self.0 = prompt
} }
pub fn display(&self) { pub fn display(&self) {
print!("{}", self.0); print!("{}", self.0);
} }
pub fn working_dir_name(&self) -> String { pub fn working_dir_name(&self) -> String {
std::env::current_dir().map_or("?".to_owned(), |path| { std::env::current_dir().map_or("?".to_owned(), |path| path.file_name().map_or("?".to_owned(), |name| {
path.file_name().map_or("?".to_owned(), |name| { match name.to_os_string() == whoami::username_os() {
let name_os_string = name.to_os_string(); true => "~".to_owned(),
match name_os_string == whoami::username_os() && name_os_string != "root" { false => name.to_string_lossy().to_string(),
true => "~".to_owned(), }
false => name.to_string_lossy().to_string(), }))
}
})
})
} }
} }

View File

@ -1,4 +1,4 @@
use std::{fs, io::{self}}; use std::{cell::RefCell, fs, io::{self}, rc::Rc};
use core::fmt; use core::fmt;
use crate::{ use crate::{
@ -14,16 +14,16 @@ pub struct LambdaShell {
terminate: bool, terminate: bool,
config: Config, config: Config,
vm: LuauVm, vm: LuauVm,
ps: Ps, ps: Rc<RefCell<Ps>>,
} }
impl LambdaShell { impl LambdaShell {
pub fn create(config: Config) -> Self { pub fn create(config: Config) -> Self {
let ps = Ps::set(ps::DEFAULT_PS.to_owned()); let ps = Rc::new(RefCell::new(Ps::set(ps::DEFAULT_PS.to_owned())));
Self { Self {
vm: vm::LuauVm::new(ps.to_owned()), ps: Rc::clone(&ps),
vm: vm::LuauVm::new(ps),
terminate: false, terminate: false,
config, config,
ps
} }
} }
@ -48,12 +48,12 @@ impl LambdaShell {
fs::read_to_string(conf_file).map_or_display(|luau_conf| self.vm_exec(luau_conf)); fs::read_to_string(conf_file).map_or_display(|luau_conf| self.vm_exec(luau_conf));
} }
} }
self.ps.display();
self.ps.borrow().display();
loop { loop {
if self.terminate { break } else { if self.terminate { break } else {
match self.wait() { match self.wait() {
Ok(()) => self.ps.display(), Ok(()) => self.ps.borrow().display(),
Err(flush_err) => self.error(flush_err), Err(flush_err) => self.error(flush_err),
} }
} }

View File

@ -1,5 +1,6 @@
use mlua::{Function, Lua as Luau, MultiValue, Result as lResult, Table, Value}; use mlua::{Function, Lua as Luau, MultiValue, Result as lResult, Table, Value};
use color_print::{cformat, ceprintln}; use color_print::{cformat, ceprintln};
use std::{cell::RefCell, rc::Rc};
use core::fmt; use core::fmt;
use shell::Shell; use shell::Shell;
@ -34,7 +35,7 @@ impl Globals for LuauVm {
fn global_warn(&self, luau_globals: &Table) -> lResult<()> { fn global_warn(&self, luau_globals: &Table) -> lResult<()> {
let luau_print = luau_globals.get::<Function>("print")?; let luau_print = luau_globals.get::<Function>("print")?;
luau_globals.set("warn", self.vm.create_function(move |this, args: MultiValue| -> lResult<()> { luau_globals.raw_set("warn", self.vm.create_function(move |this, args: MultiValue| -> lResult<()> {
let luau_multi_values = args.into_iter() let luau_multi_values = args.into_iter()
.map(|value| cformat!("<bold,y>{}</>", value.to_string().unwrap_or(Self::CONV_ERROR.to_owned()))) .map(|value| cformat!("<bold,y>{}</>", value.to_string().unwrap_or(Self::CONV_ERROR.to_owned())))
.map(|arg_v| Value::String(this.create_string(arg_v).unwrap())) .map(|arg_v| Value::String(this.create_string(arg_v).unwrap()))
@ -46,27 +47,27 @@ impl Globals for LuauVm {
fn global_version(&self, luau_globals: &Table) -> lResult<()> { fn global_version(&self, luau_globals: &Table) -> lResult<()> {
let luau_info = luau_globals.get::<String>("_VERSION")?; let luau_info = luau_globals.get::<String>("_VERSION")?;
luau_globals.set("_VERSION", format!("{luau_info}, liblambdashell {}", Self::LIB_VERSION)) luau_globals.raw_set("_VERSION", format!("{luau_info}, liblambdashell {}", Self::LIB_VERSION))
} }
} }
pub struct LuauVm { pub struct LuauVm {
vm: Luau, vm: Luau,
ps: Ps, ps: Rc<RefCell<Ps>>
} }
impl LuauVm { impl LuauVm {
pub(crate) fn new(ps: Ps) -> Self { pub(crate) fn new(ps: Rc<RefCell<Ps>>) -> Self {
Self { vm: Luau::new(), ps } Self { vm: Luau::new(), ps }
} }
fn set_shell_globals(&self) -> lResult<()> { fn set_shell_globals(&self) -> lResult<()> {
let luau_globals = self.vm.globals(); let luau_globals = self.vm.globals();
self.global_shell(&luau_globals)?;
self.global_terminal(&luau_globals)?;
self.global_warn(&luau_globals)?; self.global_warn(&luau_globals)?;
self.global_version(&luau_globals)?; self.global_version(&luau_globals)?;
self.global_terminal(&luau_globals)?; luau_globals.raw_set("getfenv", mlua::Nil)?;
self.global_shell(&luau_globals)?; luau_globals.raw_set("setfenv", mlua::Nil)?;
luau_globals.set("getfenv", mlua::Nil)?;
luau_globals.set("setfenv", mlua::Nil)?;
self.vm.sandbox(true)?; self.vm.sandbox(true)?;
Ok(()) Ok(())
} }

View File

@ -1,46 +1,38 @@
use mlua::{Result as lResult, Table}; use mlua::{Lua as Luau, MetaMethod, Result as lResult, Table, UserData, UserDataFields, UserDataMethods};
use std::{cell::RefCell, rc::Rc};
use whoami::fallible; use whoami::fallible;
use crate::vm::LuauVm; use crate::{ps::Ps, vm::LuauVm};
trait PsPrompt { const DEFAULT_HOSTNAME: &str = "hostname";
fn ps_prompt(&self) -> lResult<Table>;
fn luau_sys_details(luau: &Luau) -> lResult<Table> {
let system = luau.create_table()?;
system.raw_set("DESKTOP_ENV", whoami::desktop_env().to_string())?;
system.raw_set("DEVICENAME", whoami::devicename().to_string())?;
system.raw_set("USERNAME", whoami::username().to_string())?;
system.raw_set("REALNAME", whoami::realname().to_string())?;
system.raw_set("PLATFORM", whoami::platform().to_string())?;
system.raw_set("DISTRO", whoami::distro().to_string())?;
system.raw_set("ARCH", whoami::arch().to_string())?;
system.raw_set("HOSTNAME", fallible::hostname().unwrap_or(DEFAULT_HOSTNAME.to_owned()))?;
Ok(system)
} }
impl PsPrompt for LuauVm {
fn ps_prompt(&self) -> lResult<Table> { struct ShellUserdata(Rc<RefCell<Ps>>);
let prompt_table = self.vm.create_table()?; impl UserData for ShellUserdata {
let prompt_metatable = self.vm.create_table()?; fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
let ps_owned = self.ps.to_owned(); fields.add_field_method_get("PROMPT", |_, this| Ok(this.0.borrow().get().to_owned()));
prompt_metatable.set("__index", self.vm.create_function(move |_, (s, s1): (Table, String)| -> lResult<String> { fields.add_field_method_get("SYSTEM", |luau, _| luau_sys_details(luau));
Ok(ps_owned.clone().get())
})?)?;
prompt_metatable.set("__newindex", self.vm.create_function(|_, _: String| -> lResult<String> {
Ok("placeholder".to_owned())
})?)?;
// prompt_table.set("__metatable", mlua::Nil)?;
prompt_table.set_metatable(Some(prompt_metatable));
prompt_table.set_readonly(false);
Ok(prompt_table)
} }
}
trait System { fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
const DEFAULT_HOSTNAME: &str; methods.add_meta_method_mut(MetaMethod::NewIndex, |_, this, (tindex, tvalue): (String, String)| -> lResult<()> {
fn sys_details(&self) -> lResult<Table>; if tindex == "PROMPT" {
} this.0.borrow_mut().modify(tvalue);
impl System for LuauVm { }
const DEFAULT_HOSTNAME: &str = "hostname"; Ok(())
});
fn sys_details(&self) -> lResult<Table> {
let system = self.vm.create_table()?;
system.set("DESKTOP_ENV", whoami::desktop_env().to_string())?;
system.set("DEVICENAME", whoami::devicename().to_string())?;
system.set("USERNAME", whoami::username().to_string())?;
system.set("REALNAME", whoami::realname().to_string())?;
system.set("PLATFORM", whoami::platform().to_string())?;
system.set("DISTRO", whoami::distro().to_string())?;
system.set("HOSTNAME", fallible::hostname().unwrap_or(Self::DEFAULT_HOSTNAME.to_owned()))?;
Ok(system)
} }
} }
@ -49,10 +41,10 @@ pub trait Shell {
} }
impl Shell for LuauVm { impl Shell for LuauVm {
fn global_shell(&self, luau_globals: &Table) -> lResult<()> { fn global_shell(&self, luau_globals: &Table) -> lResult<()> {
let shell = self.vm.create_table()?; // let shell = self.vm.create_table()?;
shell.set("SYSTEM", self.sys_details()?)?; // shell.set("SYSTEM", self.sys_details()?)?;
shell.set("PROMPT", self.ps_prompt()?)?; // shell.set("PROMPT", self.ps_prompt()?)?;
luau_globals.set("SHELL", shell)?; luau_globals.set("SHELL", ShellUserdata(Rc::clone(&self.ps)))?;
Ok(()) Ok(())
} }
} }

View File

@ -7,7 +7,7 @@ use crate::vm::LuauVm;
macro_rules! foreground_styles_luau { macro_rules! foreground_styles_luau {
($self:expr, $style_table:expr, $($color:ident)+) => { ($self:expr, $style_table:expr, $($color:ident)+) => {
$( $(
$style_table.set(stringify!($color).to_ascii_uppercase(), $self.vm.create_function(|_, text: String| -> lResult<String> { $style_table.raw_set(stringify!($color).to_ascii_uppercase(), $self.vm.create_function(|_, text: String| -> lResult<String> {
Ok(text.$color().to_string()) Ok(text.$color().to_string())
})?)?; })?)?;
)+ )+
@ -16,7 +16,7 @@ macro_rules! foreground_styles_luau {
macro_rules! background_styles_luau { macro_rules! background_styles_luau {
($self:expr, $style_table:expr, $($color:ident)+) => { ($self:expr, $style_table:expr, $($color:ident)+) => {
$( $(
$style_table.set( $style_table.raw_set(
str_split!(stringify!($color), "_")[1..].join("_").to_ascii_uppercase(), str_split!(stringify!($color), "_")[1..].join("_").to_ascii_uppercase(),
$self.vm.create_function(|_, text: String| -> lResult<String> { $self.vm.create_function(|_, text: String| -> lResult<String> {
Ok(text.$color().to_string()) Ok(text.$color().to_string())
@ -44,7 +44,7 @@ impl Colors for LuauVm {
underline_blue underline_magenta underline_cyan underline_white underline_blue underline_magenta underline_cyan underline_white
bold bold
); );
term_out_table.set("FOREGROUND", foreground_table) term_out_table.raw_set("FOREGROUND", foreground_table)
} }
fn foreground(&self, term_out_table: &Table) -> lResult<()> { fn foreground(&self, term_out_table: &Table) -> lResult<()> {
@ -57,7 +57,7 @@ impl Colors for LuauVm {
on_blue on_magenta on_blue on_magenta
on_cyan on_white on_cyan on_white
); );
term_out_table.set("BACKGROUND", background_table) term_out_table.raw_set("BACKGROUND", background_table)
} }
} }
@ -73,14 +73,12 @@ impl Write for LuauVm {
Ok(()) Ok(())
}) })
} }
fn write_error(&self) -> lResult<Function> { fn write_error(&self) -> lResult<Function> {
self.vm.create_function(|_, s: String| -> lResult<()> { self.vm.create_function(|_, s: String| -> lResult<()> {
eprint!("{s}"); eprint!("{s}");
Ok(()) Ok(())
}) })
} }
fn write_error_ln(&self) -> lResult<Function> { fn write_error_ln(&self) -> lResult<Function> {
self.vm.create_function(|_, s: String| -> lResult<()> { self.vm.create_function(|_, s: String| -> lResult<()> {
eprintln!("{s}"); eprintln!("{s}");
@ -103,10 +101,10 @@ impl Terminal for LuauVm {
fn global_terminal(&self, luau_globals: &Table) -> lResult<()> { fn global_terminal(&self, luau_globals: &Table) -> lResult<()> {
let term_table = self.vm.create_table()?; let term_table = self.vm.create_table()?;
term_table.set("OUT", self.out()?)?; term_table.raw_set("OUT", self.out()?)?;
term_table.set("WRITE", self.write()?)?; term_table.raw_set("WRITE", self.write()?)?;
term_table.set("WRITE_ERROR", self.write_error()?)?; term_table.raw_set("WRITE_ERROR", self.write_error()?)?;
term_table.set("WRITE_ERROR_LN", self.write_error_ln()?)?; term_table.raw_set("WRITE_ERROR_LN", self.write_error_ln()?)?;
luau_globals.set("TERMINAL", term_table) luau_globals.raw_set("TERMINAL", term_table)
} }
} }