Compare commits
No commits in common. "restructure" and "master" have entirely different histories.
restructur
...
master
65
Cargo.lock
generated
65
Cargo.lock
generated
@ -10,9 +10,9 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.8.0"
|
version = "2.7.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
|
checksum = "1be3f42a67d6d345ecd59f675f3f012d6974981560836e938c22b424b85ce1be"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bstr"
|
name = "bstr"
|
||||||
@ -32,9 +32,9 @@ checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.2.10"
|
version = "1.2.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "13208fcbb66eaeffe09b99fffbe1af420f00a7b35aa99ad683dfc1aa76145229"
|
checksum = "ad0cf6e91fde44c773c6ee7ec6bba798504641a8bc2eb7e37a04ffbf4dfaa55a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"shlex",
|
"shlex",
|
||||||
]
|
]
|
||||||
@ -139,9 +139,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "js-sys"
|
name = "js-sys"
|
||||||
version = "0.3.77"
|
version = "0.3.76"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
|
checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
@ -169,17 +169,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libloading"
|
name = "liblambdashell"
|
||||||
version = "0.8.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"windows-targets",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "libpse"
|
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"color-print",
|
"color-print",
|
||||||
@ -192,6 +182,16 @@ dependencies = [
|
|||||||
"whoami",
|
"whoami",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libloading"
|
||||||
|
version = "0.8.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"windows-targets",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.4.15"
|
version = "0.4.15"
|
||||||
@ -210,9 +210,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "log"
|
name = "log"
|
||||||
version = "0.4.25"
|
version = "0.4.22"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"
|
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lua-src"
|
name = "lua-src"
|
||||||
@ -530,9 +530,9 @@ checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen"
|
name = "wasm-bindgen"
|
||||||
version = "0.2.100"
|
version = "0.2.99"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
|
checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
@ -541,9 +541,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-backend"
|
name = "wasm-bindgen-backend"
|
||||||
version = "0.2.100"
|
version = "0.2.99"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
|
checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bumpalo",
|
"bumpalo",
|
||||||
"log",
|
"log",
|
||||||
@ -555,9 +555,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-macro"
|
name = "wasm-bindgen-macro"
|
||||||
version = "0.2.100"
|
version = "0.2.99"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
|
checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"quote",
|
"quote",
|
||||||
"wasm-bindgen-macro-support",
|
"wasm-bindgen-macro-support",
|
||||||
@ -565,9 +565,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-macro-support"
|
name = "wasm-bindgen-macro-support"
|
||||||
version = "0.2.100"
|
version = "0.2.99"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
|
checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@ -578,18 +578,15 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasm-bindgen-shared"
|
name = "wasm-bindgen-shared"
|
||||||
version = "0.2.100"
|
version = "0.2.99"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
|
checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6"
|
||||||
dependencies = [
|
|
||||||
"unicode-ident",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "web-sys"
|
name = "web-sys"
|
||||||
version = "0.3.77"
|
version = "0.3.76"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2"
|
checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "libpse"
|
name = "liblambdashell"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ impl PathBufIsValid for PathBuf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait ChangeDirectory<'a> {
|
trait ChangeDirectory {
|
||||||
fn change_directory(&self, args: SplitWhitespace) -> Option<PathBuf>;
|
fn change_directory(&self, args: SplitWhitespace) -> Option<PathBuf>;
|
||||||
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf>;
|
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf>;
|
||||||
fn specific_user_dir(&self, user: String) -> Option<PathBuf>;
|
fn specific_user_dir(&self, user: String) -> Option<PathBuf>;
|
||||||
@ -20,7 +20,7 @@ trait ChangeDirectory<'a> {
|
|||||||
fn previous_dir(&self) -> Option<PathBuf>;
|
fn previous_dir(&self) -> Option<PathBuf>;
|
||||||
fn home_dir(&self) -> Option<PathBuf>;
|
fn home_dir(&self) -> Option<PathBuf>;
|
||||||
}
|
}
|
||||||
impl<'a> ChangeDirectory<'a> for Command<'a> {
|
impl ChangeDirectory for Command {
|
||||||
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf> {
|
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf> {
|
||||||
std::env::set_current_dir(new_path).map_or_display_none(|()| Some(new_path.to_path_buf()))
|
std::env::set_current_dir(new_path).map_or_display_none(|()| Some(new_path.to_path_buf()))
|
||||||
}
|
}
|
||||||
@ -87,19 +87,18 @@ impl<'a> ChangeDirectory<'a> for Command<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Command<'a>(&'a String);
|
pub struct Command(String);
|
||||||
impl<'a> Command<'a> {
|
impl Command {
|
||||||
pub const fn new(input: &'a String) -> Self {
|
pub const fn new(input: String) -> Self {
|
||||||
Self(input)
|
Self(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spawn_sys_cmd(&mut self, history: &mut History, command_process: io::Result<process::Child>) {
|
pub fn spawn_sys_cmd(&mut self, history: &mut History, command_process: io::Result<process::Child>) {
|
||||||
match command_process {
|
if let Ok(mut child) = command_process {
|
||||||
Ok(mut child) => {
|
|
||||||
history.add(self.0.as_str());
|
history.add(self.0.as_str());
|
||||||
child.wait().ok();
|
child.wait().ok();
|
||||||
},
|
} else {
|
||||||
Err(_) => println!("pse: Unknown command: {}", self.0),
|
println!("Unknown command: {}", self.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,8 +36,9 @@ impl History {
|
|||||||
.open(history_file.as_path())
|
.open(history_file.as_path())
|
||||||
.map_or_display(|mut file| {
|
.map_or_display(|mut file| {
|
||||||
let newline_maybe = if fs_history.is_empty() { "" } else { "\n" };
|
let newline_maybe = if fs_history.is_empty() { "" } else { "\n" };
|
||||||
let formatted = format!("{newline_maybe}{}", self.history.join("\n"));
|
if let Err(write_err) = file.write_all(format!("{newline_maybe}{}", self.history.join("\n")).as_bytes()) {
|
||||||
file.write_all(formatted.as_bytes()).unwrap_or_else(session::shell_error)
|
session::shell_error(write_err);
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
|||||||
pub mod session;
|
pub mod session;
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod history;
|
pub mod history;
|
||||||
pub mod terminal;
|
pub mod ps;
|
||||||
pub mod rc;
|
pub mod rc;
|
||||||
pub mod vm;
|
pub mod vm;
|
||||||
|
|
||||||
|
28
src/ps.rs
Normal file
28
src/ps.rs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
pub const DEFAULT_PS: &str = concat!("lambdashell-", env!("CARGO_PKG_VERSION"), " ");
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Ps(String);
|
||||||
|
impl Ps {
|
||||||
|
pub const fn set(prompt: String) -> Self {
|
||||||
|
Self(prompt)
|
||||||
|
}
|
||||||
|
//rustc: `std::string::String::as_str` is not yet stable as a const fn
|
||||||
|
pub fn get(&self) -> &str {
|
||||||
|
self.0.as_str()
|
||||||
|
}
|
||||||
|
pub fn modify(&mut self, prompt: String) {
|
||||||
|
self.0 = prompt
|
||||||
|
}
|
||||||
|
pub fn display(&self) {
|
||||||
|
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(),
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
@ -18,7 +18,7 @@ pub fn config_dir() -> Option<PathBuf> {
|
|||||||
let mut config = home::home_dir()?;
|
let mut config = home::home_dir()?;
|
||||||
config.push(".config");
|
config.push(".config");
|
||||||
config.is_valid_dir_or_create()?;
|
config.is_valid_dir_or_create()?;
|
||||||
config.push("pse");
|
config.push("lambdashell");
|
||||||
config.is_valid_dir_or_create()
|
config.is_valid_dir_or_create()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
use mlua::Lua as Luau;
|
use std::{cell::RefCell, fs, io::{self}, rc::Rc};
|
||||||
use std::{cell::RefCell, fs, rc::Rc};
|
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
use color_print::ceprintln;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
history::History, rc::{self}, terminal::TermProcessor, vm::LuauVm
|
commands, history::History, ps::{self, Ps}, rc::{self}, vm::{self, LuauVm}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn shell_error<E: fmt::Display>(err: E) {
|
||||||
|
ceprintln!("<bold,r>[!]:</> {err}")
|
||||||
|
}
|
||||||
|
|
||||||
pub trait MapDisplay<T, E: fmt::Display> {
|
pub trait MapDisplay<T, E: 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>;
|
||||||
@ -23,55 +28,67 @@ impl<T, E: fmt::Display> MapDisplay<T, E> for Result<T, E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shell_error<E: fmt::Display>(err: E) {
|
|
||||||
color_print::ceprintln!("<bold,r>[!]:</> {err}")
|
|
||||||
}
|
|
||||||
pub fn shell_error_none<T, E: fmt::Display>(err: E) -> Option<T> {
|
|
||||||
shell_error(err);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct VmConfig {
|
|
||||||
pub sandbox: bool,
|
|
||||||
pub jit: bool,
|
|
||||||
}
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub norc: bool,
|
pub norc: bool
|
||||||
pub vm: VmConfig,
|
|
||||||
}
|
}
|
||||||
pub struct Rt {
|
pub struct LambdaShell {
|
||||||
pub ps: Rc<RefCell<String>>,
|
terminate: bool,
|
||||||
pub input: String,
|
history: History,
|
||||||
pub vm: Luau,
|
config: Config,
|
||||||
|
vm: LuauVm,
|
||||||
|
ps: Rc<RefCell<Ps>>,
|
||||||
}
|
}
|
||||||
pub struct Pse {
|
impl LambdaShell {
|
||||||
pub config: Config,
|
|
||||||
pub history: History,
|
|
||||||
pub rt: Rt
|
|
||||||
}
|
|
||||||
impl Pse {
|
|
||||||
const DEFAULT_PS: &str = concat!("pse-", env!("CARGO_PKG_VERSION"), "$ ");
|
|
||||||
|
|
||||||
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 {
|
||||||
rt: Rt {
|
ps: Rc::clone(&ps),
|
||||||
ps: Rc::new(RefCell::new(Self::DEFAULT_PS.to_owned())),
|
vm: vm::LuauVm::new(ps),
|
||||||
input: String::new(),
|
|
||||||
vm: Luau::new(),
|
|
||||||
},
|
|
||||||
history: History::init(),
|
history: History::init(),
|
||||||
|
terminate: false,
|
||||||
config,
|
config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn wait(&mut self) -> Result<(), io::Error> {
|
||||||
|
io::Write::flush(&mut io::stdout()).map(|()| {
|
||||||
|
let mut input = String::new();
|
||||||
|
io::stdin().read_line(&mut input).map_or_display(|_size| match input.trim() {
|
||||||
|
"exit" => {
|
||||||
|
self.terminate = true;
|
||||||
|
self.history.add("exit");
|
||||||
|
},
|
||||||
|
trim => commands::Command::new(trim.to_owned()).exec(&mut self.history)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn error<E: fmt::Display>(&mut self, err: E) {
|
||||||
|
shell_error(err);
|
||||||
|
self.terminate = true;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn start(&mut self) {
|
pub fn start(&mut self) {
|
||||||
if !self.config.norc {
|
if !self.config.norc {
|
||||||
if let Some(conf_file) = rc::config_file() {
|
if let Some(conf_file) = rc::config_file() {
|
||||||
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));
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
self.term_input_processor().map_or_display(|()| self.history.write_to_file_fallible())
|
self.ps.borrow().display();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if self.terminate { break } else {
|
||||||
|
match self.wait() {
|
||||||
|
Ok(()) => self.ps.borrow().display(),
|
||||||
|
Err(flush_err) => self.error(flush_err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.history.write_to_file_fallible();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn vm_exec(&self, source: String) {
|
||||||
|
self.vm.exec(source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
119
src/terminal.rs
119
src/terminal.rs
@ -1,119 +0,0 @@
|
|||||||
use crossterm::{event::{self, Event, KeyCode, KeyEvent, KeyModifiers}, terminal};
|
|
||||||
use std::io::{self, Write};
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
use crate::{commands::Command, session::{self, Pse}};
|
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
|
||||||
pub enum InputHandleError {
|
|
||||||
#[error("UserExit")]
|
|
||||||
UserExit,
|
|
||||||
#[error("Sigterm")]
|
|
||||||
Sigterm,
|
|
||||||
#[error("Render failure: {0}")]
|
|
||||||
Write(io::Error),
|
|
||||||
#[error("Flush failure: {0}")]
|
|
||||||
Flush(io::Error),
|
|
||||||
#[error("Disabling the terminal's raw mode failed: {0}")]
|
|
||||||
EnableRaw(io::Error),
|
|
||||||
#[error("Enabling the terminal's raw mode failed: {0}")]
|
|
||||||
DisableRaw(io::Error),
|
|
||||||
#[error("key input failure: {0}")]
|
|
||||||
Key(KeyCode),
|
|
||||||
}
|
|
||||||
type InputResult<T> = Result<T, InputHandleError>;
|
|
||||||
|
|
||||||
trait SpecificKeybinds {
|
|
||||||
const TERM_ID_1: &str;
|
|
||||||
fn key_ctrl(&mut self, input_key: KeyEvent, keycode: KeyCode) -> InputResult<()>;
|
|
||||||
fn key_enter(&mut self) -> InputResult<()>;
|
|
||||||
fn key_backspace(&mut self) -> InputResult<()>;
|
|
||||||
}
|
|
||||||
impl SpecificKeybinds for Pse {
|
|
||||||
const TERM_ID_1: &str = "exit";
|
|
||||||
|
|
||||||
fn key_ctrl(&mut self, input_key: KeyEvent, keycode: KeyCode) -> InputResult<()> {
|
|
||||||
if input_key.modifiers.contains(KeyModifiers::CONTROL) {
|
|
||||||
match keycode {
|
|
||||||
KeyCode::Char('c') => Err(InputHandleError::Sigterm),
|
|
||||||
_ => Ok(())
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
self.term_render(Some(keycode.to_string()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn key_enter(&mut self) -> InputResult<()> {
|
|
||||||
if self.rt.input == Self::TERM_ID_1 { return Err(InputHandleError::UserExit) };
|
|
||||||
|
|
||||||
terminal::disable_raw_mode().map_err(InputHandleError::DisableRaw)?;
|
|
||||||
Command::new(&self.rt.input).exec(&mut self.history);
|
|
||||||
self.rt.input.clear();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn key_backspace(&mut self) -> InputResult<()> {
|
|
||||||
match self.rt.input.pop() {
|
|
||||||
Some(_) => self.term_render(None),
|
|
||||||
None => {
|
|
||||||
//the string is empty, do terminal beep
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait TermProcessor {
|
|
||||||
fn term_render(&mut self, def: Option<String>) -> InputResult<()>;
|
|
||||||
fn term_input_handler(&mut self, input_key: KeyEvent) -> Option<()>;
|
|
||||||
fn term_input_mainthread(&mut self) -> io::Result<()>;
|
|
||||||
fn term_input_processor(&mut self) -> io::Result<()>;
|
|
||||||
}
|
|
||||||
impl TermProcessor for Pse {
|
|
||||||
fn term_render(&mut self, def: Option<String>) -> InputResult<()> {
|
|
||||||
match def {
|
|
||||||
Some(def_string) => {
|
|
||||||
self.rt.input.push_str(&def_string);
|
|
||||||
write!(io::stdout(), "{def_string}").map_err(InputHandleError::Write)?;
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
write!(io::stdout(), "{}", self.rt.input).map_err(InputHandleError::Write)?
|
|
||||||
}
|
|
||||||
};
|
|
||||||
io::stdout().flush().map_err(InputHandleError::Flush)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn term_input_handler(&mut self, input_key: KeyEvent) -> Option<()> {
|
|
||||||
let input_handle = match input_key.code {
|
|
||||||
KeyCode::Enter => self.key_enter(),
|
|
||||||
KeyCode::Backspace => self.key_backspace(),
|
|
||||||
KeyCode::Tab => todo!(),
|
|
||||||
KeyCode::Right => todo!(),
|
|
||||||
KeyCode::Left => todo!(),
|
|
||||||
KeyCode::Up => todo!(),
|
|
||||||
KeyCode::Down => todo!(),
|
|
||||||
keycode => self.key_ctrl(input_key, keycode)
|
|
||||||
};
|
|
||||||
input_handle.map_or_else(|inp_err| match inp_err {
|
|
||||||
InputHandleError::UserExit => None,
|
|
||||||
InputHandleError::Sigterm => self.term_render(Some("^C".to_owned())).ok(),
|
|
||||||
input_err => session::shell_error_none(input_err)
|
|
||||||
}, Some)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn term_input_mainthread(&mut self) -> io::Result<()> {
|
|
||||||
crossterm::execute!(io::stdout(), event::EnableBracketedPaste)?;
|
|
||||||
loop {
|
|
||||||
terminal::enable_raw_mode()?;
|
|
||||||
if let Event::Key(event) = event::read()? {
|
|
||||||
if self.term_input_handler(event).is_none() { break Ok(()) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn term_input_processor(&mut self) -> io::Result<()> {
|
|
||||||
self.term_input_mainthread()?;
|
|
||||||
terminal::disable_raw_mode()?;
|
|
||||||
crossterm::execute!(io::stdout(), event::DisableBracketedPaste)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +1,11 @@
|
|||||||
use mlua::{Function, MultiValue, Result as lResult, Table, Value};
|
use mlua::{Function, Lua as Luau, MultiValue, Result as lResult, Table, Value};
|
||||||
use color_print::cformat;
|
use color_print::{cformat, ceprintln};
|
||||||
use shell::ShellGlobal;
|
|
||||||
use terminal::TerminalGlobal;
|
use terminal::TerminalGlobal;
|
||||||
|
use std::{cell::RefCell, rc::Rc};
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
use shell::ShellGlobal;
|
||||||
|
|
||||||
use crate::session::{Pse, MapDisplay};
|
use crate::{ps::Ps, session::MapDisplay};
|
||||||
|
|
||||||
mod shell;
|
mod shell;
|
||||||
mod terminal;
|
mod terminal;
|
||||||
@ -17,27 +18,25 @@ impl<T, E: fmt::Display> LuauRuntimeErr<T> for Result<T, E> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn map_or_luau_rt_err<R, F: FnOnce(T) -> Option<R>>(self, f: F) -> Option<R> {
|
fn map_or_luau_rt_err<R, F: FnOnce(T) -> Option<R>>(self, f: F) -> Option<R> {
|
||||||
self.map_or_else(|luau_rt_err| {
|
self.map_or_else(|luau_rt_err| {
|
||||||
color_print::ceprintln!("<bold>====</>\n<r><bold>[!]:</> {luau_rt_err}</>\n<bold>====</>");
|
ceprintln!("<bold>====</>\n<r><bold>[!]:</> {luau_rt_err}</>\n<bold>====</>");
|
||||||
None
|
None
|
||||||
}, f)
|
}, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait VmGlobals {
|
trait Globals {
|
||||||
const LIB_VERSION: &str;
|
const LIB_VERSION: &str;
|
||||||
const CONV_ERROR: &str;
|
const CONV_ERROR: &str;
|
||||||
const LIB_NAME: &str;
|
fn global_warn(&self, luau_globals: &Table) -> lResult<()>;
|
||||||
fn vm_glob_warn(&self, luau_globals: &Table) -> lResult<()>;
|
fn global_version(&self, luau_globals: &Table) -> lResult<()>;
|
||||||
fn vm_glob_version(&self, luau_globals: &Table) -> lResult<()>;
|
|
||||||
}
|
}
|
||||||
impl VmGlobals for Pse {
|
impl Globals for LuauVm {
|
||||||
const LIB_VERSION: &str = env!("CARGO_PKG_VERSION");
|
const LIB_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
const LIB_NAME: &str = env!("CARGO_PKG_NAME");
|
|
||||||
const CONV_ERROR: &str = "<SHELL CONVERSION ERROR>";
|
const CONV_ERROR: &str = "<SHELL CONVERSION ERROR>";
|
||||||
|
|
||||||
fn vm_glob_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.raw_set("warn", self.rt.vm.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(Self::CONV_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()))
|
||||||
@ -47,31 +46,34 @@ impl VmGlobals for Pse {
|
|||||||
})?)
|
})?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn vm_glob_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.raw_set("_VERSION", format!("{luau_info}, {} {}", Self::LIB_NAME, Self::LIB_VERSION))
|
luau_globals.raw_set("_VERSION", format!("{luau_info}, liblambdashell {}", Self::LIB_VERSION))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait LuauVm {
|
pub struct LuauVm {
|
||||||
fn vm_setglobs(&self) -> lResult<()>;
|
vm: Luau,
|
||||||
fn vm_exec(&self, source: String);
|
ps: Rc<RefCell<Ps>>
|
||||||
}
|
}
|
||||||
impl LuauVm for Pse {
|
impl LuauVm {
|
||||||
fn vm_setglobs(&self) -> lResult<()> {
|
pub(crate) fn new(ps: Rc<RefCell<Ps>>) -> Self {
|
||||||
let luau_globals = self.rt.vm.globals();
|
Self { vm: Luau::new(), ps }
|
||||||
self.vm_glob_shell(&luau_globals)?;
|
}
|
||||||
self.vm_glob_terminal(&luau_globals)?;
|
|
||||||
self.vm_glob_warn(&luau_globals)?;
|
|
||||||
self.vm_glob_version(&luau_globals)?;
|
|
||||||
|
|
||||||
|
fn set_shell_globals(&self) -> lResult<()> {
|
||||||
|
let luau_globals = self.vm.globals();
|
||||||
|
self.global_shell(&luau_globals)?;
|
||||||
|
self.global_terminal(&luau_globals)?;
|
||||||
|
self.global_warn(&luau_globals)?;
|
||||||
|
self.global_version(&luau_globals)?;
|
||||||
luau_globals.raw_set("getfenv", mlua::Nil)?;
|
luau_globals.raw_set("getfenv", mlua::Nil)?;
|
||||||
luau_globals.raw_set("setfenv", mlua::Nil)?;
|
luau_globals.raw_set("setfenv", mlua::Nil)?;
|
||||||
self.rt.vm.enable_jit(self.config.vm.jit);
|
self.vm.sandbox(true)?;
|
||||||
self.rt.vm.sandbox(true)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn vm_exec(&self, source: String) {
|
pub fn exec(&self, source: String) {
|
||||||
self.vm_setglobs().map_or_display_none(|()| self.rt.vm.load(source).exec().map_or_luau_rt_err(Some));
|
self.set_shell_globals().map_or_display_none(|()| self.vm.load(source).exec().map_or_luau_rt_err(Some));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,11 +2,11 @@ use mlua::{Lua as Luau, MetaMethod, Result as lResult, Table, UserData, UserData
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
use whoami::fallible;
|
use whoami::fallible;
|
||||||
|
|
||||||
use crate::session::Pse;
|
use crate::{ps::Ps, vm::LuauVm};
|
||||||
|
|
||||||
|
const DEFAULT_HOSTNAME: &str = "hostname";
|
||||||
|
|
||||||
fn luau_sys_details(luau: &Luau) -> lResult<Table> {
|
fn luau_sys_details(luau: &Luau) -> lResult<Table> {
|
||||||
const DEFAULT_HOSTNAME: &str = "hostname";
|
|
||||||
|
|
||||||
let system = luau.create_table()?;
|
let system = luau.create_table()?;
|
||||||
system.raw_set("DESKTOP_ENV", whoami::desktop_env().to_string())?;
|
system.raw_set("DESKTOP_ENV", whoami::desktop_env().to_string())?;
|
||||||
system.raw_set("DEVICENAME", whoami::devicename().to_string())?;
|
system.raw_set("DEVICENAME", whoami::devicename().to_string())?;
|
||||||
@ -19,18 +19,17 @@ fn luau_sys_details(luau: &Luau) -> lResult<Table> {
|
|||||||
Ok(system)
|
Ok(system)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Shell(Rc<RefCell<String>>);
|
struct Shell(Rc<RefCell<Ps>>);
|
||||||
impl UserData for Shell {
|
impl UserData for Shell {
|
||||||
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
|
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
|
||||||
fields.add_field_method_get("PROMPT", |_, this| Ok(this.0.borrow().to_string()));
|
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));
|
fields.add_field_method_get("SYSTEM", |luau, _| luau_sys_details(luau));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||||
methods.add_meta_method_mut(MetaMethod::NewIndex, |_, this, (t_index, t_value): (String, String)| -> lResult<()> {
|
methods.add_meta_method_mut(MetaMethod::NewIndex, |_, this, (tindex, tvalue): (String, String)| -> lResult<()> {
|
||||||
if t_index == "PROMPT" {
|
if tindex == "PROMPT" {
|
||||||
let mut prompt = this.0.borrow_mut();
|
this.0.borrow_mut().modify(tvalue);
|
||||||
*prompt = t_value;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
});
|
});
|
||||||
@ -38,10 +37,11 @@ impl UserData for Shell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait ShellGlobal {
|
pub trait ShellGlobal {
|
||||||
fn vm_glob_shell(&self, luau_globals: &Table) -> lResult<()>;
|
fn global_shell(&self, luau_globals: &Table) -> lResult<()>;
|
||||||
}
|
}
|
||||||
impl ShellGlobal for Pse {
|
impl ShellGlobal for LuauVm {
|
||||||
fn vm_glob_shell(&self, luau_globals: &Table) -> lResult<()> {
|
fn global_shell(&self, luau_globals: &Table) -> lResult<()> {
|
||||||
luau_globals.raw_set("SHELL", Shell(Rc::clone(&self.rt.ps)))
|
luau_globals.set("SHELL", Shell(Rc::clone(&self.ps)))?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,7 +2,7 @@ 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::session::Pse;
|
use crate::vm::LuauVm;
|
||||||
|
|
||||||
macro_rules! foreground_styles_luau {
|
macro_rules! foreground_styles_luau {
|
||||||
($luau:expr, $style_table:expr, $($color:ident)+) => {
|
($luau:expr, $style_table:expr, $($color:ident)+) => {
|
||||||
@ -84,10 +84,10 @@ impl UserData for Terminal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait TerminalGlobal {
|
pub trait TerminalGlobal {
|
||||||
fn vm_glob_terminal(&self, luau_globals: &Table) -> lResult<()>;
|
fn global_terminal(&self, luau_globals: &Table) -> lResult<()>;
|
||||||
}
|
}
|
||||||
impl TerminalGlobal for Pse {
|
impl TerminalGlobal for LuauVm {
|
||||||
fn vm_glob_terminal(&self, luau_globals: &Table) -> lResult<()> {
|
fn global_terminal(&self, luau_globals: &Table) -> lResult<()> {
|
||||||
luau_globals.raw_set("TERMINAL", Terminal)
|
luau_globals.raw_set("TERMINAL", Terminal)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user