Introduce warn<S...>(S...) for yellow output, uses compile time coloring

This commit is contained in:
rhpidfyre 2025-01-03 20:49:47 -05:00
parent e0f539b9b0
commit 31b16fca17
5 changed files with 113 additions and 77 deletions

8
Cargo.lock generated
View File

@ -16,9 +16,9 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "bstr"
version = "1.11.1"
version = "1.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "786a307d683a5bf92e6fd5fd69a7eb613751668d1d8d67d802846dfe367c62c8"
checksum = "531a9155a481e2ee699d4f98f43c0ca4ff8ee1bfd55c31e9e98fb29d2b176fe0"
dependencies = [
"memchr",
"serde",
@ -32,9 +32,9 @@ checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
[[package]]
name = "cc"
version = "1.2.6"
version = "1.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d6dbb628b8f8555f86d0323c2eb39e3ec81901f4b83e091db8a6a76d316a333"
checksum = "a012a0df96dd6d06ba9a1b29d6402d1a5d77c6befd2566afdc26e10603dc93d7"
dependencies = [
"shlex",
]

View File

@ -1,15 +1,35 @@
use mlua::{Result as lResult, Table};
use whoami::fallible;
use crate::vm::Vm;
use crate::vm::LuauVm;
const DEFAULT_HOSTNAME: &str = "hostname";
pub trait System {
fn details(&self, shell: &Table) -> lResult<()>;
fn global_shell(&self) -> lResult<()>;
pub trait PsPrompt {
fn ps_prompt(&self) -> lResult<Table>;
}
impl System for Vm {
fn details(&self, shell: &Table) -> lResult<()> {
impl PsPrompt for LuauVm {
fn ps_prompt(&self) -> lResult<Table> {
let prompt_table = self.0.create_table()?;
let prompt_metatable = self.0.create_table()?;
prompt_metatable.set("__index", self.0.create_function(|_, (lua_self, index): (String, String)| -> lResult<()> {
println!("lua_self={} index={}", lua_self, index);
Ok(())
})?)?;
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));
Ok(prompt_table)
}
}
pub trait System {
fn sys_details(&self, shell: &Table) -> lResult<()>;
fn shell_globals(&self, luau_globals: &Table) -> lResult<()>;
}
impl System for LuauVm {
fn sys_details(&self, shell: &Table) -> lResult<()> {
let system = self.0.create_table()?;
system.set("DESKTOP_ENV", whoami::desktop_env().to_string())?;
system.set("DEVICENAME", whoami::devicename().to_string())?;
@ -22,10 +42,12 @@ impl System for Vm {
Ok(())
}
fn global_shell(&self) -> lResult<()> {
fn shell_globals(&self, luau_globals: &Table) -> lResult<()> {
let shell = self.0.create_table()?;
self.details(&shell)?;
self.0.globals().set("SHELL", shell)?;
let ps_prompt = self.ps_prompt()?;
self.sys_details(&shell)?;
shell.set("PROMPT", ps_prompt)?;
luau_globals.set("SHELL", shell)?;
Ok(())
}
}

View File

@ -1,6 +1,6 @@
use mlua::{Result as lResult, Table};
use crossterm::style::Stylize;
use crate::vm::Vm;
use crate::vm::LuauVm;
macro_rules! foreground_styles_luau {
($self:expr, $style_table:expr, $($color:ident)+) => {
@ -29,9 +29,9 @@ pub trait TerminalColors {
fn background(&self, style_table: &Table) -> lResult<()>;
fn foreground(&self, style_table: &Table) -> lResult<()>;
fn styling(&self) -> lResult<Table>;
fn terminal(&self) -> lResult<()>;
fn terminal(&self, luau_globals: &Table) -> lResult<()>;
}
impl TerminalColors for Vm {
impl TerminalColors for LuauVm {
fn background(&self, style_table: &Table) -> lResult<()> {
let foreground_table = self.0.create_table()?;
foreground_styles_luau!(self, foreground_table,
@ -71,10 +71,10 @@ impl TerminalColors for Vm {
Ok(style_table)
}
fn terminal(&self) -> lResult<()> {
fn terminal(&self, luau_globals: &Table) -> lResult<()> {
let term_table = self.0.create_table()?;
term_table.set("OUT", self.styling()?)?;
self.0.globals().set("TERMINAL", &term_table)?;
luau_globals.set("TERMINAL", &term_table)?;
Ok(())
}
}

View File

@ -1,52 +1,76 @@
use mlua::{
Lua as Luau,
Result as lResult
Result as lResult,
Function,
MultiValue,
Table
};
use crate::VERSION;
use crate::terminal::TerminalColors;
use crate::sytem::System;
use crate::{sytem::System, terminal::TerminalColors, VERSION};
use color_print::{cformat, cprintln};
use core::fmt;
use color_print::cprintln;
fn display_none<T, E: fmt::Display>(err: E) -> Option<T> {
println!("{err}");
None
}
fn luau_error<T>(err: mlua::Error) -> Option<T> {
cprintln!("<bold>====</>\n<r><bold>[!]</> {err}</>\n<bold>====</>");
None
}
trait Globals {
fn version(&self) -> lResult<()>;
fn warn(&self, luau_globals: &Table) -> lResult<()>;
fn version(&self, luau_globals: &Table) -> lResult<()>;
}
impl Globals for Vm {
fn version(&self) -> lResult<()> {
let luau_info = self.0.globals().get::<String>("_VERSION")?;
self.0.globals().set("_VERSION", format!("{}, liblambdashell {}", luau_info, VERSION))
impl Globals for LuauVm {
fn warn(&self, luau_globals: &Table) -> lResult<()> {
let luau_print = luau_globals.get::<Function>("print")?;
luau_globals.set("warn", self.0.create_function(move |this, args: MultiValue| -> lResult<()> {
let luau_multi_values = args.into_iter()
.map(|value| cformat!("<y>{}</>", value.to_string().unwrap_or("<SHELL CONVERSION ERROR>".to_owned())))
.collect::<Vec<String>>();
let back_to_luau_multi = luau_multi_values.into_iter()
.map(|arg_v| mlua::Value::String(this.create_string(&arg_v).unwrap()))
.collect::<MultiValue>();
luau_print.call::<()>(back_to_luau_multi).unwrap();
Ok(())
})?)
}
fn version(&self, luau_globals: &Table) -> lResult<()> {
let luau_info = luau_globals.get::<String>("_VERSION")?;
luau_globals.set("_VERSION", format!("{}, liblambdashell {}", luau_info, VERSION))
}
}
pub struct Vm(pub Luau);
impl Vm {
trait Helpers {
fn option_display_none<T, E: fmt::Display>(&self, err: E) -> Option<T>;
fn luau_error<T>(&self, err: mlua::Error) -> Option<T>;
}
impl Helpers for LuauVm {
fn option_display_none<T, E: fmt::Display>(&self, err: E) -> Option<T> {
println!("{err}");
None
}
fn luau_error<T>(&self, err: mlua::Error) -> Option<T> {
cprintln!("<bold>====</>\n<r><bold>[!]:</> {err}</>\n<bold>====</>");
None
}
}
pub struct LuauVm(pub Luau);
impl LuauVm {
pub fn new() -> Self {
Self(Luau::new())
}
fn set_shell_globals(&self) -> mlua::Result<()> {
self.version()?;
self.terminal()?;
self.global_shell()?;
self.0.globals().set("getfenv", mlua::Nil)?;
self.0.globals().set("setfenv", mlua::Nil)?;
fn set_shell_globals(&self) -> lResult<()> {
let luau_globals = self.0.globals();
self.warn(&luau_globals)?;
self.version(&luau_globals)?;
self.terminal(&luau_globals)?;
self.shell_globals(&luau_globals)?;
luau_globals.set("getfenv", mlua::Nil)?;
luau_globals.set("setfenv", mlua::Nil)?;
self.0.sandbox(true)?;
Ok(())
}
pub fn exec(&self, source: String) {
self.set_shell_globals().map_or_else(display_none, |()| {
self.0.load(source).exec().map_or_else(luau_error, Some)
});
match self.set_shell_globals() {
Ok(()) => self.0.load(source).exec().map_or_else(|exec_err| self.luau_error(exec_err), Some),
Err(globals_err) => self.option_display_none(globals_err),
};
}
}

View File

@ -1,20 +1,12 @@
use crate::{ps, commands, rc, vm};
use crate::{commands, ps, rc, vm::{self, LuauVm}};
use std::{fs, io::{self}};
pub struct Config {
pub norc: bool
}
trait ShellLuauVm {
fn shell_vm_exec(&self, source: String);
}
impl ShellLuauVm for LambdaShell {
fn shell_vm_exec(&self, source: String) {
vm::Vm::new().exec(source);
}
}
pub struct LambdaShell {
vm: LuauVm,
ps1: String,
config: Config,
terminating: bool,
@ -22,6 +14,7 @@ pub struct LambdaShell {
impl LambdaShell {
pub fn create(config: Config) -> Self {
Self {
vm: vm::LuauVm::new(),
ps1: ps::DEFAULT_PS.to_owned(),
terminating: false,
config,
@ -41,30 +34,27 @@ impl LambdaShell {
})
}
fn rc_parse(&self) {
if !self.config.norc {
if let Some(conf_file) = rc::config_file() {
match fs::read_to_string(conf_file) {
Ok(luau_conf) => self.shell_vm_exec(luau_conf),
Err(read_err) => println!("{read_err}"),
}
}
}
pub fn vm_exec(&self, source: String) {
self.vm.exec(source);
}
pub fn start(&mut self) {
self.rc_parse();
ps::display(&self.ps1);
if !self.config.norc {
if let Some(conf_file) = rc::config_file() {
fs::read_to_string(conf_file).map_or_else(|e| println!("{e}"), |luau_conf| self.vm_exec(luau_conf));
}
}
loop {
match self.terminating {
true => break,
false => match self.wait() {
Ok(()) => ps::display(&self.ps1),
Err(flush_error) => {
println!("{flush_error}");
break;
false => {
match self.wait() {
Ok(()) => {},
Err(flush_error) => {
println!("{flush_error}");
break;
}
}
},
}