Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
fb2d79c35c | |||
e578f408c5 | |||
d95366bebe | |||
97b08d24e3 | |||
41899480c0 |
16
Cargo.lock
generated
16
Cargo.lock
generated
@ -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",
|
||||||
|
@ -12,9 +12,3 @@ 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
|
|
||||||
|
19
src/lib.rs
19
src/lib.rs
@ -1,22 +1,29 @@
|
|||||||
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
pub mod session;
|
pub mod session;
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod ps;
|
pub mod ps;
|
||||||
pub mod rc;
|
pub mod rc;
|
||||||
pub mod vm;
|
pub mod vm;
|
||||||
|
|
||||||
pub trait MapDisplay<T, E: std::fmt::Display> {
|
#[inline]
|
||||||
|
pub fn shell_error<E: core::fmt::Display>(err: E) {
|
||||||
|
color_print::ceprintln!("<bold,r>[!]:</> {err}")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait MapDisplay<T, E: core::fmt::Display> {
|
||||||
fn map_or_display<F: FnOnce(T)>(self, f: F);
|
fn map_or_display<F: FnOnce(T)>(self, f: F);
|
||||||
fn map_or_display_none<R, F: FnOnce(T) -> Option<R>>(self, f: F) -> Option<R>;
|
fn map_or_display_none<R, F: FnOnce(T) -> Option<R>>(self, f: F) -> Option<R>;
|
||||||
}
|
}
|
||||||
impl<T, E: std::fmt::Display> MapDisplay<T, E> for Result<T, E> {
|
impl<T, E: core::fmt::Display> MapDisplay<T, E> for Result<T, E> {
|
||||||
///Map and display an error
|
///Map but display the error to stdout
|
||||||
#[inline]
|
#[inline]
|
||||||
fn map_or_display<F: FnOnce(T)>(self, f: F) {
|
fn map_or_display<F: FnOnce(T)>(self, f: F) {
|
||||||
self.map_or_else(|e| color_print::ceprintln!("<bold,r>[!]:</> {e}"), f)
|
self.map_or_else(|e| shell_error(e), f)
|
||||||
}
|
}
|
||||||
///Map and display an error but return `None`
|
///Map but display the error to stdout and return `None`
|
||||||
#[inline]
|
#[inline]
|
||||||
fn map_or_display_none<R, F: FnOnce(T) -> Option<R>>(self, f: F) -> Option<R> {
|
fn map_or_display_none<R, F: FnOnce(T) -> Option<R>>(self, f: F) -> Option<R> {
|
||||||
self.map_or_else(|e| { color_print::ceprintln!("<bold,r>[!]:</> {e}"); None }, f)
|
self.map_or_else(|e| { shell_error(e); None }, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
30
src/ps.rs
30
src/ps.rs
@ -1,24 +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)]
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
//rustc: `std::string::String::as_str` is not yet stable as a const fn
|
||||||
pub fn working_dir_name(&self) -> String {
|
pub fn get(&self) -> &str {
|
||||||
std::env::current_dir().map_or("?".to_owned(), |path| {
|
self.0.as_str()
|
||||||
path.file_name().map_or("?".to_owned(), |name| {
|
|
||||||
let name_os_string = name.to_os_string();
|
|
||||||
match name_os_string == whoami::username_os() && name_os_string != "root" {
|
|
||||||
true => "~".to_owned(),
|
|
||||||
false => name.to_string_lossy().to_string(),
|
|
||||||
}
|
}
|
||||||
})
|
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 {
|
||||||
|
std::env::current_dir().map_or("?".to_owned(), |path| path.file_name().map_or("?".to_owned(), |name| {
|
||||||
|
match name.to_os_string() == whoami::username_os() {
|
||||||
|
true => "~".to_owned(),
|
||||||
|
false => name.to_string_lossy().to_string(),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
@ -6,9 +6,12 @@ use crate::MapDisplay;
|
|||||||
pub const DEFAULT_CONFIG_CONTENT: &str = r#"--!strict
|
pub const DEFAULT_CONFIG_CONTENT: &str = r#"--!strict
|
||||||
|
|
||||||
local cyan = TERMINAL.OUT.FOREGROUND.CYAN
|
local cyan = TERMINAL.OUT.FOREGROUND.CYAN
|
||||||
|
local red = TERMINAL.OUT.FOREGROUND.RED
|
||||||
|
|
||||||
local username = cyan(SHELL.SYSTEM.USERNAME)
|
|
||||||
local hostname = SHELL.SYSTEM.HOSTNAME
|
local hostname = SHELL.SYSTEM.HOSTNAME
|
||||||
|
local username = SHELL.SYSTEM.USERNAME
|
||||||
|
|
||||||
|
username = if username == "root" then red(username) else cyan(username)
|
||||||
|
|
||||||
SHELL.PROMPT = `{username}@{hostname} λ `"#;
|
SHELL.PROMPT = `{username}@{hostname} λ `"#;
|
||||||
|
|
||||||
|
@ -1,29 +1,28 @@
|
|||||||
use std::{fs, io::{self}};
|
use std::{cell::RefCell, fs, io::{self}, rc::Rc};
|
||||||
|
use core::fmt;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
vm::{LuauVm, self},
|
commands, ps::{self, Ps}, rc, shell_error, vm::{self, LuauVm}, MapDisplay
|
||||||
commands,
|
|
||||||
ps,
|
|
||||||
rc,
|
|
||||||
MapDisplay,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub norc: bool
|
pub norc: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LambdaShell {
|
pub struct LambdaShell {
|
||||||
vm: LuauVm,
|
terminate: bool,
|
||||||
ps1: String,
|
|
||||||
config: Config,
|
config: Config,
|
||||||
terminating: bool,
|
vm: LuauVm,
|
||||||
|
ps: Rc<RefCell<Ps>>,
|
||||||
}
|
}
|
||||||
impl LambdaShell {
|
impl LambdaShell {
|
||||||
pub fn create(config: Config) -> Self {
|
pub fn create(config: Config) -> Self {
|
||||||
|
let ps = Rc::new(RefCell::new(Ps::set(ps::DEFAULT_PS.to_owned())));
|
||||||
Self {
|
Self {
|
||||||
vm: vm::LuauVm::new(),
|
ps: Rc::clone(&ps),
|
||||||
ps1: ps::DEFAULT_PS.to_owned(),
|
vm: vm::LuauVm::new(ps),
|
||||||
terminating: false,
|
terminate: false,
|
||||||
config,
|
config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -31,18 +30,16 @@ impl LambdaShell {
|
|||||||
pub fn wait(&mut self) -> Result<(), io::Error> {
|
pub fn wait(&mut self) -> Result<(), io::Error> {
|
||||||
io::Write::flush(&mut io::stdout()).map(|()| {
|
io::Write::flush(&mut io::stdout()).map(|()| {
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
io::stdin().read_line(&mut input).map_or_display(|_size| {
|
io::stdin().read_line(&mut input).map_or_display(|_size| match input.trim() {
|
||||||
match input.trim() {
|
"exit" => self.terminate = true,
|
||||||
//special casey
|
|
||||||
"exit" => self.terminating = true,
|
|
||||||
trim => commands::Command::new(trim.to_owned()).exec()
|
trim => commands::Command::new(trim.to_owned()).exec()
|
||||||
};
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn vm_exec(&self, source: String) {
|
pub fn error<E: fmt::Display>(&mut self, err: E) {
|
||||||
self.vm.exec(source);
|
shell_error(err);
|
||||||
|
self.terminate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(&mut self) {
|
pub fn start(&mut self) {
|
||||||
@ -51,20 +48,19 @@ 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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
loop {
|
|
||||||
match self.terminating {
|
|
||||||
true => break,
|
|
||||||
false => {
|
|
||||||
|
|
||||||
|
self.ps.borrow().display();
|
||||||
|
loop {
|
||||||
|
if self.terminate { break } else {
|
||||||
match self.wait() {
|
match self.wait() {
|
||||||
Ok(()) => {},
|
Ok(()) => self.ps.borrow().display(),
|
||||||
Err(flush_error) => {
|
Err(flush_err) => self.error(flush_err),
|
||||||
println!("{flush_error}");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn vm_exec(&self, source: String) {
|
||||||
|
self.vm.exec(source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
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 terminal::TerminalGlobal;
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use shell::Shell;
|
use shell::ShellGlobal;
|
||||||
|
|
||||||
use crate::{vm::terminal::Terminal, MapDisplay};
|
use crate::{ps::Ps, MapDisplay};
|
||||||
|
|
||||||
mod shell;
|
mod shell;
|
||||||
mod terminal;
|
mod terminal;
|
||||||
@ -24,51 +26,54 @@ impl<T, E: fmt::Display> LuauRuntimeErr<T> for Result<T, E> {
|
|||||||
|
|
||||||
trait Globals {
|
trait Globals {
|
||||||
const LIB_VERSION: &str;
|
const LIB_VERSION: &str;
|
||||||
|
const CONV_ERROR: &str;
|
||||||
fn global_warn(&self, luau_globals: &Table) -> lResult<()>;
|
fn global_warn(&self, luau_globals: &Table) -> lResult<()>;
|
||||||
fn global_version(&self, luau_globals: &Table) -> lResult<()>;
|
fn global_version(&self, luau_globals: &Table) -> lResult<()>;
|
||||||
}
|
}
|
||||||
impl Globals for LuauVm {
|
impl Globals for LuauVm {
|
||||||
const LIB_VERSION: &str = env!("CARGO_PKG_VERSION");
|
const LIB_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
const CONV_ERROR: &str = "<SHELL CONVERSION ERROR>";
|
||||||
|
|
||||||
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.0.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("<SHELL CONVERSION 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()))
|
||||||
.collect::<MultiValue>();
|
.collect::<MultiValue>();
|
||||||
luau_print.call::<()>(luau_multi_values).unwrap();
|
luau_print.call::<()>(luau_multi_values)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
})?)
|
})?)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 Luau);
|
pub struct LuauVm {
|
||||||
|
vm: Luau,
|
||||||
|
ps: Rc<RefCell<Ps>>
|
||||||
|
}
|
||||||
impl LuauVm {
|
impl LuauVm {
|
||||||
pub(crate) fn new() -> Self {
|
pub(crate) fn new(ps: Rc<RefCell<Ps>>) -> Self {
|
||||||
Self(Luau::new())
|
Self { vm: Luau::new(), ps }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_shell_globals(&self) -> lResult<()> {
|
fn set_shell_globals(&self) -> lResult<()> {
|
||||||
let luau_globals = self.0.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)?;
|
self.vm.sandbox(true)?;
|
||||||
luau_globals.set("setfenv", mlua::Nil)?;
|
|
||||||
self.0.sandbox(true)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec(&self, source: String) {
|
pub fn exec(&self, source: String) {
|
||||||
self.set_shell_globals().map_or_display_none(|()| {
|
self.set_shell_globals().map_or_display_none(|()| self.vm.load(source).exec().map_or_luau_rt_err(Some));
|
||||||
self.0.load(source).exec().map_or_luau_rt_err(Some)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,57 +1,47 @@
|
|||||||
use mlua::{Result as lResult, Table, Value};
|
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};
|
||||||
|
|
||||||
const DEFAULT_HOSTNAME: &str = "hostname";
|
const DEFAULT_HOSTNAME: &str = "hostname";
|
||||||
|
|
||||||
trait PsPrompt {
|
fn luau_sys_details(luau: &Luau) -> lResult<Table> {
|
||||||
fn ps_prompt(&self) -> lResult<Table>;
|
let system = luau.create_table()?;
|
||||||
}
|
system.raw_set("DESKTOP_ENV", whoami::desktop_env().to_string())?;
|
||||||
impl PsPrompt for LuauVm {
|
system.raw_set("DEVICENAME", whoami::devicename().to_string())?;
|
||||||
fn ps_prompt(&self) -> lResult<Table> {
|
system.raw_set("USERNAME", whoami::username().to_string())?;
|
||||||
let prompt_table = self.0.create_table()?;
|
system.raw_set("REALNAME", whoami::realname().to_string())?;
|
||||||
let prompt_metatable = self.0.create_table()?;
|
system.raw_set("PLATFORM", whoami::platform().to_string())?;
|
||||||
prompt_metatable.set("__index", self.0.create_function(|_, (table, index): (Table, Value)| -> lResult<String> {
|
system.raw_set("DISTRO", whoami::distro().to_string())?;
|
||||||
table.raw_get::<String>(index)
|
system.raw_set("ARCH", whoami::arch().to_string())?;
|
||||||
})?)?;
|
system.raw_set("HOSTNAME", fallible::hostname().unwrap_or(DEFAULT_HOSTNAME.to_owned()))?;
|
||||||
prompt_metatable.set("__newindex", self.0.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 sys_details(&self) -> lResult<Table>;
|
|
||||||
}
|
|
||||||
impl System for LuauVm {
|
|
||||||
fn sys_details(&self) -> lResult<Table> {
|
|
||||||
let system = self.0.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(DEFAULT_HOSTNAME.to_owned()))?;
|
|
||||||
Ok(system)
|
Ok(system)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Shell(Rc<RefCell<Ps>>);
|
||||||
|
impl UserData for Shell {
|
||||||
|
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("SYSTEM", |luau, _| luau_sys_details(luau));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Shell {
|
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 ShellGlobal {
|
||||||
fn global_shell(&self, luau_globals: &Table) -> lResult<()>;
|
fn global_shell(&self, luau_globals: &Table) -> lResult<()>;
|
||||||
}
|
}
|
||||||
impl Shell for LuauVm {
|
impl ShellGlobal for LuauVm {
|
||||||
fn global_shell(&self, luau_globals: &Table) -> lResult<()> {
|
fn global_shell(&self, luau_globals: &Table) -> lResult<()> {
|
||||||
let shell = self.0.create_table()?;
|
luau_globals.set("SHELL", Shell(Rc::clone(&self.ps)))?;
|
||||||
let ps_prompt = self.ps_prompt()?;
|
|
||||||
shell.set("SYSTEM", self.sys_details()?)?;
|
|
||||||
shell.set("PROMPT", ps_prompt)?;
|
|
||||||
luau_globals.set("SHELL", shell)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,38 +1,34 @@
|
|||||||
use mlua::{Function, Result as lResult, Table};
|
use mlua::{UserDataFields, Lua as Luau, Result as lResult, Table, UserData};
|
||||||
use const_format::str_split;
|
use const_format::str_split;
|
||||||
use crossterm::style::Stylize;
|
use crossterm::style::Stylize;
|
||||||
|
|
||||||
use crate::vm::LuauVm;
|
use crate::vm::LuauVm;
|
||||||
|
|
||||||
macro_rules! foreground_styles_luau {
|
macro_rules! foreground_styles_luau {
|
||||||
($self:expr, $style_table:expr, $($color:ident)+) => {
|
($luau:expr, $style_table:expr, $($color:ident)+) => {
|
||||||
$(
|
$(
|
||||||
$style_table.set(stringify!($color).to_ascii_uppercase(), $self.0.create_function(|_, text: String| -> lResult<String> {
|
$style_table.raw_set(stringify!($color).to_ascii_uppercase(), $luau.create_function(|_, text: String| -> lResult<String> {
|
||||||
Ok(text.$color().to_string())
|
Ok(text.$color().to_string())
|
||||||
})?)?;
|
})?)?;
|
||||||
)+
|
)+
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
macro_rules! background_styles_luau {
|
macro_rules! background_styles_luau {
|
||||||
($self:expr, $style_table:expr, $($color:ident)+) => {
|
($luau: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.0.create_function(|_, text: String| -> lResult<String> {
|
$luau.create_function(|_, text: String| -> lResult<String> {
|
||||||
Ok(text.$color().to_string())
|
Ok(text.$color().to_string())
|
||||||
})?)?;
|
})?)?;
|
||||||
)+
|
)+
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Colors {
|
fn text_styles_funcs(luau: &Luau) -> lResult<(Table, Table)> {
|
||||||
fn background(&self, term_out_table: &Table) -> lResult<()>;
|
let foreground_table = luau.create_table()?;
|
||||||
fn foreground(&self, term_out_table: &Table) -> lResult<()>;
|
let background_table = luau.create_table()?;
|
||||||
}
|
foreground_styles_luau!(luau, foreground_table,
|
||||||
impl Colors for LuauVm {
|
|
||||||
fn background(&self, term_out_table: &Table) -> lResult<()> {
|
|
||||||
let foreground_table = self.0.create_table()?;
|
|
||||||
foreground_styles_luau!(self, foreground_table,
|
|
||||||
dark_grey dark_red dark_green dark_cyan
|
dark_grey dark_red dark_green dark_cyan
|
||||||
dark_yellow dark_magenta dark_blue
|
dark_yellow dark_magenta dark_blue
|
||||||
red grey black green yellow
|
red grey black green yellow
|
||||||
@ -44,12 +40,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)
|
background_styles_luau!(luau, background_table,
|
||||||
}
|
|
||||||
|
|
||||||
fn foreground(&self, term_out_table: &Table) -> lResult<()> {
|
|
||||||
let background_table = self.0.create_table()?;
|
|
||||||
background_styles_luau!(self, background_table,
|
|
||||||
on_dark_grey on_dark_red on_dark_green on_dark_cyan
|
on_dark_grey on_dark_red on_dark_green on_dark_cyan
|
||||||
on_dark_yellow on_dark_magenta on_dark_blue
|
on_dark_yellow on_dark_magenta on_dark_blue
|
||||||
on_red on_grey on_black
|
on_red on_grey on_black
|
||||||
@ -57,56 +48,46 @@ 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)
|
Ok((foreground_table, background_table))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
trait Write {
|
fn fields_write_funcs<F: UserDataFields<Terminal>>(fields: &mut F) {
|
||||||
fn write(&self) -> lResult<Function>;
|
fields.add_field_method_get("WRITE", |luau, _| luau.create_function(|_, s: String| -> lResult<()> {
|
||||||
fn write_error(&self) -> lResult<Function>;
|
|
||||||
fn write_error_ln(&self) -> lResult<Function>;
|
|
||||||
}
|
|
||||||
impl Write for LuauVm {
|
|
||||||
fn write(&self) -> lResult<Function> {
|
|
||||||
self.0.create_function(|_, s: String| -> lResult<()> {
|
|
||||||
print!("{s}");
|
print!("{s}");
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
}));
|
||||||
}
|
fields.add_field_method_get("WRITE_ERROR", |luau, _| luau.create_function(|_, s: String| -> lResult<()> {
|
||||||
|
|
||||||
fn write_error(&self) -> lResult<Function> {
|
|
||||||
self.0.create_function(|_, s: String| -> lResult<()> {
|
|
||||||
eprint!("{s}");
|
eprint!("{s}");
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
}));
|
||||||
}
|
fields.add_field_method_get("WRITE_ERROR_LN", |luau, _| luau.create_function(|_, s: String| -> lResult<()> {
|
||||||
|
|
||||||
fn write_error_ln(&self) -> lResult<Function> {
|
|
||||||
self.0.create_function(|_, s: String| -> lResult<()> {
|
|
||||||
eprintln!("{s}");
|
eprintln!("{s}");
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
}));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Terminal {
|
struct Terminal;
|
||||||
fn out(&self) -> lResult<Table>;
|
impl UserData for Terminal {
|
||||||
|
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
|
||||||
|
fields_write_funcs(fields);
|
||||||
|
fields.add_field_method_get("OUT", |luau, _| {
|
||||||
|
let (foreground, background) = text_styles_funcs(luau)?;
|
||||||
|
let out_table = luau.create_table()?;
|
||||||
|
out_table.raw_set("FOREGROUND", foreground)?;
|
||||||
|
out_table.raw_set("BACKGROUND", background)?;
|
||||||
|
Ok(out_table)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||||
|
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait TerminalGlobal {
|
||||||
fn global_terminal(&self, luau_globals: &Table) -> lResult<()>;
|
fn global_terminal(&self, luau_globals: &Table) -> lResult<()>;
|
||||||
}
|
}
|
||||||
impl Terminal for LuauVm {
|
impl TerminalGlobal for LuauVm {
|
||||||
fn out(&self) -> lResult<Table> {
|
|
||||||
let term_out_table = self.0.create_table()?;
|
|
||||||
self.background(&term_out_table)?;
|
|
||||||
self.foreground(&term_out_table)?;
|
|
||||||
Ok(term_out_table)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn global_terminal(&self, luau_globals: &Table) -> lResult<()> {
|
fn global_terminal(&self, luau_globals: &Table) -> lResult<()> {
|
||||||
let term_table = self.0.create_table()?;
|
luau_globals.raw_set("TERMINAL", Terminal)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user