6 Commits

9 changed files with 207 additions and 157 deletions

8
Cargo.lock generated
View File

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

View File

@ -1,13 +1,8 @@
pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub mod shell; pub mod session;
mod commands; pub mod commands;
mod ps; pub mod ps;
mod rc; pub mod rc;
#[path = "./luau/vm.rs"] pub mod vm;
mod vm;
#[path = "./luau/alias.rs"]
mod alias;
#[path = "./luau/terminal.rs"]
mod terminal;

View File

@ -1,84 +0,0 @@
use mlua::{
Lua as Luau,
Result as lResult,
MultiValue,
};
use crate::VERSION;
use crate::terminal::TerminalColors;
use core::fmt;
use color_print::cprintln;
fn display_none<T, E>(err: E) -> Option<T>
where
E: fmt::Display
{
println!("{err}");
None
}
fn luau_error<T>(err: mlua::Error) -> Option<T> {
cprintln!("<bold>====</>\n<r><bold>[!]</> {err}</>\n<bold>====</>");
None
}
fn luau_out(luau_args: MultiValue) -> String {
let mut print = String::new();
luau_args.iter()
.map(|arg| arg.to_string().unwrap_or("<SHELL CONVERSION ERROR>".to_owned()))
.for_each(|arg| {
if !print.is_empty() {
print.push('\u{0009}');
};
print.push_str(&arg);
}
);
print
}
trait Globals {
fn print(&self) -> lResult<()>;
fn printraw(&self) -> lResult<()>;
fn version(&self) -> lResult<()>;
}
impl Globals for Vm {
fn print(&self) -> lResult<()> {
self.0.globals().set("print", self.0.create_function(|_this, args: MultiValue| -> lResult<()> {
println!("{}", luau_out(args));
Ok(())
})?)
}
fn printraw(&self) -> lResult<()> {
self.0.globals().set("printraw", self.0.create_function(|_this, args: MultiValue| -> lResult<()> {
println!("{}", luau_out(args));
Ok(())
})?)
}
fn version(&self) -> lResult<()> {
let luau_info = self.0.globals().get::<String>("_VERSION")?;
self.0.globals().set("_VERSION", format!("{}, liblambdashell {}", luau_info, VERSION))
}
}
pub struct Vm(pub Luau);
impl Vm {
pub fn new() -> Self {
Self(Luau::new())
}
fn set_shell_globals(&self) -> mlua::Result<()> {
self.print()?;
self.printraw()?;
self.version()?;
self.terminal()?;
self.0.globals().set("getfenv", mlua::Nil)?;
self.0.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(()))
});
}
}

View File

@ -1,21 +1,26 @@
use const_format::formatcp; use const_format::formatcp;
use color_print::{cformat, cprint};
pub const DEFAULT_PS: &str = formatcp!("lambdashell-{}", env!("CARGO_PKG_VERSION")); pub const DEFAULT_PS: &str = formatcp!("lambdashell-{}", env!("CARGO_PKG_VERSION"));
pub fn working_dir_name() -> String { struct Ps(String);
std::env::current_dir().map_or("?".to_owned(), |path| { impl Ps {
path.file_name().map_or("?".to_owned(), |name| { fn set(prompt: String) -> Self {
let name_os_string = name.to_os_string(); Self(prompt)
match name_os_string == whoami::username_os() && name_os_string != "root" { }
true => "~".to_owned(),
false => name.to_string_lossy().to_string(),
}
})
})
}
pub fn display(ps1: &String) { fn working_dir_name(&self) -> String {
let working_dir_name = cformat!(" <bold>{}</> ", working_dir_name()); std::env::current_dir().map_or("?".to_owned(), |path| {
cprint!("{}{}λ ", ps1, working_dir_name); 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(),
}
})
})
}
fn display(&self) {
print!("{}", self.0);
}
} }

View File

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

76
src/vm/mod.rs Normal file
View File

@ -0,0 +1,76 @@
use mlua::{
Function, Lua as Luau, MultiValue, Result as lResult, Table, Value
};
use crate::vm::{shell::System, terminal::Terminal};
use crate::VERSION;
use color_print::{cformat, cprintln};
use core::fmt;
mod shell;
mod terminal;
mod alias;
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
}
}
trait Globals {
fn global_warn(&self, luau_globals: &Table) -> lResult<()>;
fn global_version(&self, luau_globals: &Table) -> lResult<()>;
}
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.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())))
.map(|arg_v| Value::String(this.create_string(arg_v).unwrap()))
.collect::<MultiValue>();
luau_print.call::<()>(luau_multi_values).unwrap();
Ok(())
})?)
}
fn global_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 LuauVm(pub Luau);
impl LuauVm {
pub(crate) fn new() -> Self {
Self(Luau::new())
}
fn set_shell_globals(&self) -> lResult<()> {
let luau_globals = self.0.globals();
self.global_warn(&luau_globals)?;
self.global_version(&luau_globals)?;
self.global_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) {
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),
};
}
}

52
src/vm/shell.rs Normal file
View File

@ -0,0 +1,52 @@
use mlua::{Result as lResult, Table, Value};
use whoami::fallible;
use crate::vm::LuauVm;
const DEFAULT_HOSTNAME: &str = "hostname";
pub trait PsPrompt {
fn ps_prompt(&self) -> lResult<Table>;
}
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(|_, (table, index): (Table, Value)| -> lResult<String> {
table.raw_get::<String>(index)
})?)?;
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())?;
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()))?;
shell.set("SYSTEM", system)?;
Ok(())
}
fn shell_globals(&self, luau_globals: &Table) -> lResult<()> {
let shell = self.0.create_table()?;
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 mlua::{Result as lResult, Table};
use crossterm::style::Stylize; use crossterm::style::Stylize;
use crate::vm::Vm; 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)+) => {
@ -25,13 +25,13 @@ macro_rules! background_styles_luau {
}; };
} }
pub trait TerminalColors { #[allow(dead_code)]
trait Colors {
fn background(&self, style_table: &Table) -> lResult<()>; fn background(&self, style_table: &Table) -> lResult<()>;
fn foreground(&self, style_table: &Table) -> lResult<()>; fn foreground(&self, style_table: &Table) -> lResult<()>;
fn styling(&self) -> lResult<Table>; fn styling(&self, term_out_table: &Table) -> lResult<()>;
fn terminal(&self) -> lResult<()>;
} }
impl TerminalColors for Vm { impl Colors for LuauVm {
fn background(&self, style_table: &Table) -> lResult<()> { fn background(&self, style_table: &Table) -> lResult<()> {
let foreground_table = self.0.create_table()?; let foreground_table = self.0.create_table()?;
foreground_styles_luau!(self, foreground_table, foreground_styles_luau!(self, foreground_table,
@ -46,8 +46,7 @@ impl TerminalColors for Vm {
underline_blue underline_magenta underline_cyan underline_white underline_blue underline_magenta underline_cyan underline_white
bold bold
); );
style_table.set("FOREGROUND", foreground_table)?; style_table.set("FOREGROUND", foreground_table)
Ok(())
} }
fn foreground(&self, style_table: &Table) -> lResult<()> { fn foreground(&self, style_table: &Table) -> lResult<()> {
@ -60,21 +59,38 @@ impl TerminalColors for Vm {
on_blue on_magenta on_blue on_magenta
on_cyan on_white on_cyan on_white
); );
style_table.set("BACKGROUND", background_table)?; style_table.set("BACKGROUND", background_table)
Ok(())
} }
fn styling(&self) -> lResult<Table> { fn styling(&self, term_out_table: &Table) -> lResult<()> {
let style_table = self.0.create_table()?; let style_table = self.0.create_table()?;
self.foreground(&style_table)?; self.foreground(&style_table)?;
self.background(&style_table)?; self.background(&style_table)?;
Ok(style_table) term_out_table.set("STYLE", style_table)
} }
}
fn terminal(&self) -> lResult<()> {
let term_table = self.0.create_table()?; #[allow(dead_code)]
term_table.set("OUT", self.styling()?)?; trait Write {
self.0.globals().set("TERMINAL", &term_table)?; fn write(&self, term_out_table: &Table) -> lResult<()>;
Ok(()) }
impl Write for LuauVm {
fn write(&self, term_out_table: &Table) -> lResult<()> {
term_out_table.set("WRITE", self.0.create_function(|_, s: String| -> lResult<()> {
print!("{s}");
Ok(())
})?)
}
}
pub trait Terminal {
fn global_terminal(&self, luau_globals: &Table) -> lResult<()>;
}
impl Terminal for LuauVm {
fn global_terminal(&self, luau_globals: &Table) -> lResult<()> {
let term_table = self.0.create_table()?;
let term_out_table = self.0.create_table()?;
term_table.set("OUT", term_out_table)?;
luau_globals.set("TERMINAL", &term_table)
} }
} }