Working SHELL.PROMPT =
and SHELL.PROMPT
, SHELL is now a userdata
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
use mlua::{Function, Lua as Luau, MultiValue, Result as lResult, Table, Value};
|
||||
use color_print::{cformat, ceprintln};
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use core::fmt;
|
||||
use shell::Shell;
|
||||
|
||||
@ -34,7 +35,7 @@ impl Globals for LuauVm {
|
||||
|
||||
fn global_warn(&self, luau_globals: &Table) -> lResult<()> {
|
||||
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()
|
||||
.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()))
|
||||
@ -46,27 +47,30 @@ impl Globals for LuauVm {
|
||||
|
||||
fn global_version(&self, luau_globals: &Table) -> lResult<()> {
|
||||
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 {
|
||||
vm: Luau,
|
||||
ps: Ps,
|
||||
ps: Rc<RefCell<Ps>>
|
||||
}
|
||||
impl LuauVm {
|
||||
pub(crate) fn new(ps: Ps) -> Self {
|
||||
Self { vm: Luau::new(), ps }
|
||||
pub(crate) fn new(ps: Rc<RefCell<Ps>>) -> Self {
|
||||
Self {
|
||||
vm: Luau::new(),
|
||||
ps
|
||||
}
|
||||
}
|
||||
|
||||
fn set_shell_globals(&self) -> lResult<()> {
|
||||
let luau_globals = self.vm.globals();
|
||||
self.global_shell(&luau_globals)?;
|
||||
self.global_terminal(&luau_globals)?;
|
||||
self.global_warn(&luau_globals)?;
|
||||
self.global_version(&luau_globals)?;
|
||||
self.global_terminal(&luau_globals)?;
|
||||
self.global_shell(&luau_globals)?;
|
||||
luau_globals.set("getfenv", mlua::Nil)?;
|
||||
luau_globals.set("setfenv", mlua::Nil)?;
|
||||
luau_globals.raw_set("getfenv", mlua::Nil)?;
|
||||
luau_globals.raw_set("setfenv", mlua::Nil)?;
|
||||
self.vm.sandbox(true)?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,28 +1,8 @@
|
||||
use mlua::{Result as lResult, Table};
|
||||
use mlua::{MetaMethod, Result as lResult, Table, UserData, UserDataFields, UserDataMethods};
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use whoami::fallible;
|
||||
|
||||
use crate::vm::LuauVm;
|
||||
|
||||
trait PsPrompt {
|
||||
fn ps_prompt(&self) -> lResult<Table>;
|
||||
}
|
||||
impl PsPrompt for LuauVm {
|
||||
fn ps_prompt(&self) -> lResult<Table> {
|
||||
let prompt_table = self.vm.create_table()?;
|
||||
let prompt_metatable = self.vm.create_table()?;
|
||||
let ps_owned = self.ps.to_owned();
|
||||
prompt_metatable.set("__index", self.vm.create_function(move |_, (s, s1): (Table, String)| -> lResult<String> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
use crate::{ps::Ps, vm::LuauVm};
|
||||
|
||||
trait System {
|
||||
const DEFAULT_HOSTNAME: &str;
|
||||
@ -33,26 +13,42 @@ impl System for LuauVm {
|
||||
|
||||
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()))?;
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
struct ShellUserdata(Rc<RefCell<Ps>>);
|
||||
impl UserData for ShellUserdata {
|
||||
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
|
||||
fields.add_field_method_get("PROMPT", |_, this| Ok(this.0.borrow().get().to_owned()));
|
||||
}
|
||||
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_meta_method_mut(MetaMethod::NewIndex, |_, this, (tindex, tvalue): (String, String)| -> lResult<()> {
|
||||
if tindex == "PROMPT" {
|
||||
this.0.borrow_mut().modify(tvalue);
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Shell {
|
||||
fn global_shell(&self, luau_globals: &Table) -> lResult<()>;
|
||||
}
|
||||
impl Shell for LuauVm {
|
||||
fn global_shell(&self, luau_globals: &Table) -> lResult<()> {
|
||||
let shell = self.vm.create_table()?;
|
||||
shell.set("SYSTEM", self.sys_details()?)?;
|
||||
shell.set("PROMPT", self.ps_prompt()?)?;
|
||||
luau_globals.set("SHELL", shell)?;
|
||||
// let shell = self.vm.create_table()?;
|
||||
// shell.set("SYSTEM", self.sys_details()?)?;
|
||||
// shell.set("PROMPT", self.ps_prompt()?)?;
|
||||
luau_globals.set("SHELL", ShellUserdata(Rc::clone(&self.ps)))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user