SHELL.SYSTEM
This commit is contained in:
parent
d95366bebe
commit
e578f408c5
@ -57,10 +57,7 @@ pub struct LuauVm {
|
|||||||
}
|
}
|
||||||
impl LuauVm {
|
impl LuauVm {
|
||||||
pub(crate) fn new(ps: Rc<RefCell<Ps>>) -> Self {
|
pub(crate) fn new(ps: Rc<RefCell<Ps>>) -> Self {
|
||||||
Self {
|
Self { vm: Luau::new(), ps }
|
||||||
vm: Luau::new(),
|
|
||||||
ps
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_shell_globals(&self) -> lResult<()> {
|
fn set_shell_globals(&self) -> lResult<()> {
|
||||||
|
@ -1,18 +1,13 @@
|
|||||||
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 std::{cell::RefCell, rc::Rc};
|
||||||
use whoami::fallible;
|
use whoami::fallible;
|
||||||
|
|
||||||
use crate::{ps::Ps, vm::LuauVm};
|
use crate::{ps::Ps, vm::LuauVm};
|
||||||
|
|
||||||
trait System {
|
const DEFAULT_HOSTNAME: &str = "hostname";
|
||||||
const DEFAULT_HOSTNAME: &str;
|
|
||||||
fn sys_details(&self) -> lResult<Table>;
|
|
||||||
}
|
|
||||||
impl System for LuauVm {
|
|
||||||
const DEFAULT_HOSTNAME: &str = "hostname";
|
|
||||||
|
|
||||||
fn sys_details(&self) -> lResult<Table> {
|
fn luau_sys_details(luau: &Luau) -> lResult<Table> {
|
||||||
let system = self.vm.create_table()?;
|
let system = luau.create_table()?;
|
||||||
system.raw_set("DESKTOP_ENV", whoami::desktop_env().to_string())?;
|
system.raw_set("DESKTOP_ENV", whoami::desktop_env().to_string())?;
|
||||||
system.raw_set("DEVICENAME", whoami::devicename().to_string())?;
|
system.raw_set("DEVICENAME", whoami::devicename().to_string())?;
|
||||||
system.raw_set("USERNAME", whoami::username().to_string())?;
|
system.raw_set("USERNAME", whoami::username().to_string())?;
|
||||||
@ -20,16 +15,17 @@ impl System for LuauVm {
|
|||||||
system.raw_set("PLATFORM", whoami::platform().to_string())?;
|
system.raw_set("PLATFORM", whoami::platform().to_string())?;
|
||||||
system.raw_set("DISTRO", whoami::distro().to_string())?;
|
system.raw_set("DISTRO", whoami::distro().to_string())?;
|
||||||
system.raw_set("ARCH", whoami::arch().to_string())?;
|
system.raw_set("ARCH", whoami::arch().to_string())?;
|
||||||
system.raw_set("HOSTNAME", fallible::hostname().unwrap_or(Self::DEFAULT_HOSTNAME.to_owned()))?;
|
system.raw_set("HOSTNAME", fallible::hostname().unwrap_or(DEFAULT_HOSTNAME.to_owned()))?;
|
||||||
Ok(system)
|
Ok(system)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ShellUserdata(Rc<RefCell<Ps>>);
|
struct ShellUserdata(Rc<RefCell<Ps>>);
|
||||||
impl UserData for ShellUserdata {
|
impl UserData for ShellUserdata {
|
||||||
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
|
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
|
||||||
fields.add_field_method_get("PROMPT", |_, this| Ok(this.0.borrow().get().to_owned()));
|
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<M: UserDataMethods<Self>>(methods: &mut M) {
|
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||||
methods.add_meta_method_mut(MetaMethod::NewIndex, |_, this, (tindex, tvalue): (String, String)| -> lResult<()> {
|
methods.add_meta_method_mut(MetaMethod::NewIndex, |_, this, (tindex, tvalue): (String, String)| -> lResult<()> {
|
||||||
if tindex == "PROMPT" {
|
if tindex == "PROMPT" {
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user