Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
492a789109 | |||
e06a4a6c72 | |||
bf92ed7bad | |||
54ae2f4b1c |
17
src/lib.rs
17
src/lib.rs
@ -1,15 +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;
|
|
||||||
#[path = "./luau/system.rs"]
|
|
||||||
mod sytem;
|
|
35
src/ps.rs
35
src/ps.rs
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -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}};
|
use std::{fs, io::{self}};
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
@ -1,38 +1,14 @@
|
|||||||
use mlua::{
|
use mlua::{
|
||||||
Lua as Luau,
|
Function, Lua as Luau, MultiValue, Result as lResult, Table, Value
|
||||||
Result as lResult,
|
|
||||||
Function,
|
|
||||||
MultiValue,
|
|
||||||
Table
|
|
||||||
};
|
};
|
||||||
use crate::{sytem::System, terminal::TerminalColors, VERSION};
|
use crate::vm::{shell::System, terminal::Terminal};
|
||||||
|
use crate::VERSION;
|
||||||
use color_print::{cformat, cprintln};
|
use color_print::{cformat, cprintln};
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
|
||||||
trait Globals {
|
mod shell;
|
||||||
fn warn(&self, luau_globals: &Table) -> lResult<()>;
|
mod terminal;
|
||||||
fn version(&self, luau_globals: &Table) -> lResult<()>;
|
mod alias;
|
||||||
}
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trait Helpers {
|
trait Helpers {
|
||||||
fn option_display_none<T, E: fmt::Display>(&self, err: E) -> Option<T>;
|
fn option_display_none<T, E: fmt::Display>(&self, err: E) -> Option<T>;
|
||||||
@ -43,23 +19,47 @@ impl Helpers for LuauVm {
|
|||||||
println!("{err}");
|
println!("{err}");
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn luau_error<T>(&self, err: mlua::Error) -> Option<T> {
|
fn luau_error<T>(&self, err: mlua::Error) -> Option<T> {
|
||||||
cprintln!("<bold>====</>\n<r><bold>[!]:</> {err}</>\n<bold>====</>");
|
cprintln!("<bold>====</>\n<r><bold>[!]:</> {err}</>\n<bold>====</>");
|
||||||
None
|
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);
|
pub struct LuauVm(pub Luau);
|
||||||
impl LuauVm {
|
impl LuauVm {
|
||||||
pub fn new() -> Self {
|
pub(crate) fn new() -> Self {
|
||||||
Self(Luau::new())
|
Self(Luau::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_shell_globals(&self) -> lResult<()> {
|
fn set_shell_globals(&self) -> lResult<()> {
|
||||||
let luau_globals = self.0.globals();
|
let luau_globals = self.0.globals();
|
||||||
self.warn(&luau_globals)?;
|
self.global_warn(&luau_globals)?;
|
||||||
self.version(&luau_globals)?;
|
self.global_version(&luau_globals)?;
|
||||||
self.terminal(&luau_globals)?;
|
self.global_terminal(&luau_globals)?;
|
||||||
self.shell_globals(&luau_globals)?;
|
self.shell_globals(&luau_globals)?;
|
||||||
luau_globals.set("getfenv", mlua::Nil)?;
|
luau_globals.set("getfenv", mlua::Nil)?;
|
||||||
luau_globals.set("setfenv", mlua::Nil)?;
|
luau_globals.set("setfenv", mlua::Nil)?;
|
@ -1,4 +1,4 @@
|
|||||||
use mlua::{Result as lResult, Table};
|
use mlua::{Result as lResult, Table, Value};
|
||||||
use whoami::fallible;
|
use whoami::fallible;
|
||||||
use crate::vm::LuauVm;
|
use crate::vm::LuauVm;
|
||||||
|
|
||||||
@ -11,9 +11,8 @@ impl PsPrompt for LuauVm {
|
|||||||
fn ps_prompt(&self) -> lResult<Table> {
|
fn ps_prompt(&self) -> lResult<Table> {
|
||||||
let prompt_table = self.0.create_table()?;
|
let prompt_table = self.0.create_table()?;
|
||||||
let prompt_metatable = 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<()> {
|
prompt_metatable.set("__index", self.0.create_function(|_, (table, index): (Table, Value)| -> lResult<String> {
|
||||||
println!("lua_self={} index={}", lua_self, index);
|
table.raw_get::<String>(index)
|
||||||
Ok(())
|
|
||||||
})?)?;
|
})?)?;
|
||||||
prompt_metatable.set("__newindex", self.0.create_function(|_, _: String| -> lResult<String> {
|
prompt_metatable.set("__newindex", self.0.create_function(|_, _: String| -> lResult<String> {
|
||||||
Ok("placeholder".to_owned())
|
Ok("placeholder".to_owned())
|
@ -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, luau_globals: &Table) -> lResult<()>;
|
|
||||||
}
|
}
|
||||||
impl TerminalColors for LuauVm {
|
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 LuauVm {
|
|||||||
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 LuauVm {
|
|||||||
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, luau_globals: &Table) -> lResult<()> {
|
|
||||||
let term_table = self.0.create_table()?;
|
#[allow(dead_code)]
|
||||||
term_table.set("OUT", self.styling()?)?;
|
trait Write {
|
||||||
luau_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)
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user