diff --git a/Cargo.lock b/Cargo.lock index b30e689..29eca51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,9 +32,9 @@ checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "cc" -version = "1.2.8" +version = "1.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad0cf6e91fde44c773c6ee7ec6bba798504641a8bc2eb7e37a04ffbf4dfaa55a" +checksum = "c8293772165d9345bdaaa39b45b2109591e63fe5e6fbc23c6ff930a048aa310b" dependencies = [ "shlex", ] @@ -45,6 +45,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "color-print" version = "0.3.7" @@ -112,6 +118,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "ctrlc" +version = "3.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90eeab0aa92f3f9b4e87f258c72b139c207d251f9cbc1080a0086b86a8870dd3" +dependencies = [ + "nix", + "windows-sys 0.59.0", +] + [[package]] name = "either" version = "1.13.0" @@ -128,6 +144,31 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", + "slab", +] + [[package]] name = "home" version = "0.5.11" @@ -139,9 +180,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.76" +version = "0.3.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" dependencies = [ "once_cell", "wasm-bindgen", @@ -175,6 +216,7 @@ dependencies = [ "color-print", "const_format", "crossterm", + "ctrlc", "home", "mlua", "thiserror", @@ -210,9 +252,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.22" +version = "0.4.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" [[package]] name = "lua-src" @@ -274,6 +316,7 @@ checksum = "9ea43c3ffac2d0798bd7128815212dd78c98316b299b7a902dabef13dc7b6b8d" dependencies = [ "bstr", "either", + "futures-util", "libloading", "mlua-sys", "num-traits", @@ -295,6 +338,18 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -343,6 +398,18 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "pkg-config" version = "0.3.31" @@ -457,6 +524,15 @@ dependencies = [ "libc", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "smallvec" version = "1.13.2" @@ -530,9 +606,9 @@ checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" [[package]] name = "wasm-bindgen" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" dependencies = [ "cfg-if", "once_cell", @@ -541,9 +617,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" dependencies = [ "bumpalo", "log", @@ -555,9 +631,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -565,9 +641,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", @@ -578,15 +654,18 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.99" +version = "0.2.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] [[package]] name = "web-sys" -version = "0.3.76" +version = "0.3.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc" +checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index b7ad3d7..613322e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,9 @@ edition = "2021" color-print = "0.3.7" const_format = { version = "0.2.34", features = ["rust_1_64"] } crossterm = "0.28.1" +ctrlc = "3.4.5" home = "0.5.9" -mlua = { version = "0.10.0", features = ["luau-jit", "vendored"] } +mlua = { version = "0.10.0", features = ["luau-jit", "vendored", "async", "send"] } thiserror = "2.0.9" uzers = "0.12.1" -whoami = "1.5.2" \ No newline at end of file +whoami = "1.5.2" diff --git a/src/session.rs b/src/session.rs index 706922e..27b762d 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,10 +1,8 @@ -use std::{cell::RefCell, fs, io::{self}, rc::Rc}; +use std::{fs, io::{self}, sync::{Arc, Mutex}, thread}; use core::fmt; use color_print::ceprintln; -use crate::{ - commands, history::History, ps::{self, Ps}, rc::{self}, vm::{self, LuauVm} -}; +use crate::{commands, history::History, ps::{self, Ps}, rc::{self}, vm::{self, LuauVm}}; #[inline] pub fn shell_error(err: E) { @@ -28,6 +26,42 @@ impl MapDisplay for Result { } } +trait Errors { + fn error(&mut self, err: E); +} +impl Errors for LambdaShell { + fn error(&mut self, err: E) { + shell_error(err); + self.terminate = true; + } +} + +trait Signals { + fn sigterm_event(&mut self); + fn input(&mut self); +} +impl Signals for LambdaShell { + fn sigterm_event(&mut self) { + // task::spawn(async move { + // signal::ctrl_c().await.expect("Failed to listen for a sigterm signal."); + // // self.history.write_to_file_fallible(); + // std::process::exit(0x0100); + // }); + } + fn input(&mut self) { + thread::spawn(|| { + loop { + if self.terminate { break } else { + match self.wait() { + Ok(()) => self.ps.borrow().display(), + Err(flush_err) => self.error(flush_err), + } + } + } + }); + } +} + #[derive(Debug, Clone)] pub struct Config { pub norc: bool @@ -37,13 +71,13 @@ pub struct LambdaShell { history: History, config: Config, vm: LuauVm, - ps: Rc>, + ps: Arc>, } impl LambdaShell { pub fn create(config: Config) -> Self { - let ps = Rc::new(RefCell::new(Ps::set(ps::DEFAULT_PS.to_owned()))); + let ps = Arc::new(Mutex::new(Ps::set(ps::DEFAULT_PS.to_owned()))); Self { - ps: Rc::clone(&ps), + ps: Arc::clone(&ps), vm: vm::LuauVm::new(ps), history: History::init(), terminate: false, @@ -51,7 +85,7 @@ impl LambdaShell { } } - pub fn wait(&mut self) -> Result<(), io::Error> { + 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() { @@ -64,31 +98,14 @@ impl LambdaShell { }) } - pub fn error(&mut self, err: E) { - shell_error(err); - self.terminate = true; - } - - pub fn start(&mut self) { + pub async fn start(&mut self) { if !self.config.norc { 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.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(); + self.sigterm_event(); + self.input(); } - - pub fn vm_exec(&self, source: String) { - self.vm.exec(source); - } -} +} \ No newline at end of file