7 Commits

Author SHA1 Message Date
edced4ddf1 add const_format with rust_1_64, and vendored feature for mlua 2025-01-06 15:26:20 -05:00
7fba40e8d5 insane person macro 2025-01-06 03:02:14 -05:00
eaac5a34bd LICENSE 2025-01-05 15:03:41 -05:00
492a789109 mod.rs for vm 2025-01-04 18:50:24 -05:00
e06a4a6c72 OUT.WRITE<T...>(T...), and some cleanup 2025-01-04 12:14:18 -05:00
bf92ed7bad shell.rs -> session.rs and luau/system.rs -> shell.rs 2025-01-03 22:21:27 -05:00
54ae2f4b1c restructure ps.rs 2025-01-03 20:50:27 -05:00
10 changed files with 179 additions and 93 deletions

59
Cargo.lock generated
View File

@ -73,6 +73,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "126f97965c8ad46d6d9163268ff28432e8f6a1196a55578867832e3049df63dd"
dependencies = [
"const_format_proc_macros",
"konst",
]
[[package]]
@ -146,6 +147,21 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "konst"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330f0e13e6483b8c34885f7e6c9f19b1a7bd449c673fbb948a51c99d66ef74f4"
dependencies = [
"konst_macro_rules",
]
[[package]]
name = "konst_macro_rules"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4933f3f57a8e9d9da04db23fb153356ecaf00cbd14aee46279c33dc80925c37"
[[package]]
name = "libc"
version = "0.2.169"
@ -198,6 +214,25 @@ version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "lua-src"
version = "547.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edaf29e3517b49b8b746701e5648ccb5785cde1c119062cbabbc5d5cd115e42"
dependencies = [
"cc",
]
[[package]]
name = "luajit-src"
version = "210.5.11+97813fb"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3015551c284515db7c30c559fc1080f9cb9ee990d1f6fca315451a107c7540bb"
dependencies = [
"cc",
"which",
]
[[package]]
name = "luau0-src"
version = "0.11.2+luau653"
@ -254,6 +289,8 @@ checksum = "63a11d485edf0f3f04a508615d36c7d50d299cf61a7ee6d3e2530651e0a31771"
dependencies = [
"cc",
"cfg-if",
"lua-src",
"luajit-src",
"luau0-src",
"pkg-config",
]
@ -428,9 +465,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "syn"
version = "2.0.94"
version = "2.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "987bc0be1cdea8b10216bd06e2ca407d40b9543468fafd3ddfb02f36e77f71f3"
checksum = "46f71c0377baf4ef1cc3e3402ded576dccc315800fbc62dfc7fe04b009773b4a"
dependencies = [
"proc-macro2",
"quote",
@ -555,6 +592,18 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "which"
version = "6.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4ee928febd44d98f2f459a4a79bd4d928591333a494a10a868418ac1b39cf1f"
dependencies = [
"either",
"home",
"rustix",
"winsafe",
]
[[package]]
name = "whoami"
version = "1.5.2"
@ -669,3 +718,9 @@ name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winsafe"
version = "0.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904"

View File

@ -5,10 +5,10 @@ edition = "2021"
[dependencies]
color-print = "0.3.7"
const_format = "0.2.33"
const_format = { version = "0.2.34", features = ["rust_1_64"] }
crossterm = "0.28.1"
home = "0.5.9"
mlua = { version = "0.10.0", features = ["luau-jit"] }
mlua = { version = "0.10.0", features = ["luau-jit", "vendored"] }
thiserror = "2.0.9"
uzers = "0.12.1"
whoami = "1.5.2"

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 rhpidfyre
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

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

View File

@ -1,9 +1,14 @@
use const_format::formatcp;
use color_print::{cformat, cprint};
pub const DEFAULT_PS: &str = formatcp!("lambdashell-{}", env!("CARGO_PKG_VERSION"));
pub fn working_dir_name() -> String {
struct Ps(String);
impl Ps {
fn set(prompt: String) -> Self {
Self(prompt)
}
fn working_dir_name(&self) -> String {
std::env::current_dir().map_or("?".to_owned(), |path| {
path.file_name().map_or("?".to_owned(), |name| {
let name_os_string = name.to_os_string();
@ -13,9 +18,9 @@ pub fn working_dir_name() -> String {
}
})
})
}
}
pub fn display(ps1: &String) {
let working_dir_name = cformat!(" <bold>{}</> ", working_dir_name());
cprint!("{}{}λ ", ps1, working_dir_name);
fn display(&self) {
print!("{}", self.0);
}
}

View File

@ -1,4 +1,4 @@
use crate::{commands, ps, rc, vm::{self, LuauVm}};
use crate::{commands, ps, rc, vm::{LuauVm, self}};
use std::{fs, io::{self}};
pub struct Config {

View File

@ -1,38 +1,14 @@
use mlua::{
Lua as Luau,
Result as lResult,
Function,
MultiValue,
Table
Function, Lua as Luau, MultiValue, Result as lResult, Table, Value
};
use crate::{sytem::System, terminal::TerminalColors, VERSION};
use crate::vm::{shell::System, terminal::Terminal};
use crate::VERSION;
use color_print::{cformat, cprintln};
use core::fmt;
trait Globals {
fn warn(&self, luau_globals: &Table) -> lResult<()>;
fn version(&self, luau_globals: &Table) -> lResult<()>;
}
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))
}
}
mod shell;
mod terminal;
mod alias;
trait Helpers {
fn option_display_none<T, E: fmt::Display>(&self, err: E) -> Option<T>;
@ -43,23 +19,47 @@ impl Helpers for LuauVm {
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 fn new() -> Self {
pub(crate) fn new() -> Self {
Self(Luau::new())
}
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.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)?;

View File

@ -1,4 +1,4 @@
use mlua::{Result as lResult, Table};
use mlua::{Result as lResult, Table, Value};
use whoami::fallible;
use crate::vm::LuauVm;
@ -11,9 +11,8 @@ 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("__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())

View File

@ -1,4 +1,5 @@
use mlua::{Result as lResult, Table};
use const_format::str_split;
use mlua::{Function, Result as lResult, Table};
use crossterm::style::Stylize;
use crate::vm::LuauVm;
@ -11,27 +12,23 @@ macro_rules! foreground_styles_luau {
)+
};
}
macro_rules! background_styles_luau {
($self:expr, $style_table:expr, $($color:ident)+) => {
$(
match stringify!($color).split_once("_") {
Some((_, color_name)) => $style_table.set(color_name.to_ascii_uppercase(), $self.0.create_function(|_, text: String| -> lResult<String> {
$style_table.set(str_split!(stringify!($color), "_")[1..].join("_").to_ascii_uppercase(), $self.0.create_function(|_, text: String| -> lResult<String> {
Ok(text.$color().to_string())
})?)?,
None => panic!("Luau set error: {:?}. There was nothing to split from delimiter: \"_\"", stringify!($color)),
}
})?)?;
)+
};
}
pub trait TerminalColors {
#[allow(dead_code)]
trait Colors {
fn background(&self, style_table: &Table) -> lResult<()>;
fn foreground(&self, style_table: &Table) -> lResult<()>;
fn styling(&self) -> lResult<Table>;
fn terminal(&self, luau_globals: &Table) -> lResult<()>;
fn styling(&self, term_out_table: &Table) -> lResult<()>;
}
impl TerminalColors for LuauVm {
impl Colors for LuauVm {
fn background(&self, style_table: &Table) -> lResult<()> {
let foreground_table = self.0.create_table()?;
foreground_styles_luau!(self, foreground_table,
@ -46,8 +43,7 @@ impl TerminalColors for LuauVm {
underline_blue underline_magenta underline_cyan underline_white
bold
);
style_table.set("FOREGROUND", foreground_table)?;
Ok(())
style_table.set("FOREGROUND", foreground_table)
}
fn foreground(&self, style_table: &Table) -> lResult<()> {
@ -60,21 +56,38 @@ impl TerminalColors for LuauVm {
on_blue on_magenta
on_cyan on_white
);
style_table.set("BACKGROUND", background_table)?;
Ok(())
style_table.set("BACKGROUND", background_table)
}
fn styling(&self) -> lResult<Table> {
fn styling(&self, term_out_table: &Table) -> lResult<()> {
let style_table = self.0.create_table()?;
self.foreground(&style_table)?;
self.background(&style_table)?;
Ok(style_table)
}
fn terminal(&self, luau_globals: &Table) -> lResult<()> {
let term_table = self.0.create_table()?;
term_table.set("OUT", self.styling()?)?;
luau_globals.set("TERMINAL", &term_table)?;
Ok(())
term_out_table.set("STYLE", style_table)
}
}
#[allow(dead_code)]
trait Write {
fn write(&self, term_out_table: &Table) -> lResult<()>;
}
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)
}
}