From e578f408c55b40c13994857360e71a75f8e5a670 Mon Sep 17 00:00:00 2001 From: rhpidfyre Date: Fri, 10 Jan 2025 12:19:51 -0500 Subject: [PATCH] `SHELL.SYSTEM` --- src/vm/mod.rs | 5 +---- src/vm/shell.rs | 34 +++++++++++++++------------------- src/vm/terminal.rs | 20 +++++++++----------- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 5e4e8cb..c464351 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -57,10 +57,7 @@ pub struct LuauVm { } impl LuauVm { pub(crate) fn new(ps: Rc>) -> Self { - Self { - vm: Luau::new(), - ps - } + Self { vm: Luau::new(), ps } } fn set_shell_globals(&self) -> lResult<()> { diff --git a/src/vm/shell.rs b/src/vm/shell.rs index 3335d30..5a3b72c 100644 --- a/src/vm/shell.rs +++ b/src/vm/shell.rs @@ -1,35 +1,31 @@ -use mlua::{MetaMethod, Result as lResult, Table, UserData, UserDataFields, UserDataMethods}; +use mlua::{Lua as Luau, MetaMethod, Result as lResult, Table, UserData, UserDataFields, UserDataMethods}; use std::{cell::RefCell, rc::Rc}; use whoami::fallible; use crate::{ps::Ps, vm::LuauVm}; -trait System { - const DEFAULT_HOSTNAME: &str; - fn sys_details(&self) -> lResult; -} -impl System for LuauVm { - const DEFAULT_HOSTNAME: &str = "hostname"; +const DEFAULT_HOSTNAME: &str = "hostname"; - fn sys_details(&self) -> lResult
{ - let system = self.vm.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(Self::DEFAULT_HOSTNAME.to_owned()))?; - Ok(system) - } +fn luau_sys_details(luau: &Luau) -> lResult
{ + 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) } struct ShellUserdata(Rc>); impl UserData for ShellUserdata { fn add_fields>(fields: &mut F) { fields.add_field_method_get("PROMPT", |_, this| Ok(this.0.borrow().get().to_owned())); + fields.add_field_method_get("SYSTEM", |luau, _| luau_sys_details(luau)); } + fn add_methods>(methods: &mut M) { methods.add_meta_method_mut(MetaMethod::NewIndex, |_, this, (tindex, tvalue): (String, String)| -> lResult<()> { if tindex == "PROMPT" { diff --git a/src/vm/terminal.rs b/src/vm/terminal.rs index 9cb24b0..e11c14c 100644 --- a/src/vm/terminal.rs +++ b/src/vm/terminal.rs @@ -7,7 +7,7 @@ use crate::vm::LuauVm; macro_rules! foreground_styles_luau { ($self:expr, $style_table:expr, $($color:ident)+) => { $( - $style_table.set(stringify!($color).to_ascii_uppercase(), $self.vm.create_function(|_, text: String| -> lResult { + $style_table.raw_set(stringify!($color).to_ascii_uppercase(), $self.vm.create_function(|_, text: String| -> lResult { Ok(text.$color().to_string()) })?)?; )+ @@ -16,7 +16,7 @@ macro_rules! foreground_styles_luau { macro_rules! background_styles_luau { ($self:expr, $style_table:expr, $($color:ident)+) => { $( - $style_table.set( + $style_table.raw_set( str_split!(stringify!($color), "_")[1..].join("_").to_ascii_uppercase(), $self.vm.create_function(|_, text: String| -> lResult { Ok(text.$color().to_string()) @@ -44,7 +44,7 @@ impl Colors for LuauVm { underline_blue underline_magenta underline_cyan underline_white bold ); - term_out_table.set("FOREGROUND", foreground_table) + term_out_table.raw_set("FOREGROUND", foreground_table) } fn foreground(&self, term_out_table: &Table) -> lResult<()> { @@ -57,7 +57,7 @@ impl Colors for LuauVm { on_blue on_magenta 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(()) }) } - fn write_error(&self) -> lResult { self.vm.create_function(|_, s: String| -> lResult<()> { eprint!("{s}"); Ok(()) }) } - fn write_error_ln(&self) -> lResult { self.vm.create_function(|_, s: String| -> lResult<()> { eprintln!("{s}"); @@ -103,10 +101,10 @@ impl Terminal for LuauVm { fn global_terminal(&self, luau_globals: &Table) -> lResult<()> { let term_table = self.vm.create_table()?; - term_table.set("OUT", self.out()?)?; - term_table.set("WRITE", self.write()?)?; - term_table.set("WRITE_ERROR", self.write_error()?)?; - term_table.set("WRITE_ERROR_LN", self.write_error_ln()?)?; - luau_globals.set("TERMINAL", term_table) + term_table.raw_set("OUT", self.out()?)?; + term_table.raw_set("WRITE", self.write()?)?; + term_table.raw_set("WRITE_ERROR", self.write_error()?)?; + term_table.raw_set("WRITE_ERROR_LN", self.write_error_ln()?)?; + luau_globals.raw_set("TERMINAL", term_table) } } \ No newline at end of file