commit b87bb4d85fe68461b6b992aadb596fa91ef52130 Author: rhpidfyre Date: Sat Dec 7 17:33:39 2024 -0500 init diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c286db1 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,517 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "bstr" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a68f1f47cdf0ec8ee4b941b2eee2a80cb796db73118c0dd09ac63fbe405be22" +dependencies = [ + "memchr", + "serde", +] + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "cc" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "color-print" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3aa954171903797d5623e047d9ab69d91b493657917bdfb8c2c80ecaf9cdb6f4" +dependencies = [ + "color-print-proc-macro", +] + +[[package]] +name = "color-print-proc-macro" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "692186b5ebe54007e45a59aea47ece9eb4108e141326c304cdc91699a7118a22" +dependencies = [ + "nom", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "const_format" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50c655d81ff1114fb0dcdea9225ea9f0cc712a6f8d189378e82bdf62a473a64b" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eff1a44b93f47b1bac19a27932f5c591e43d1ba357ee4f61526c8a25603f0eb1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + +[[package]] +name = "home" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "js-sys" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.167" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" + +[[package]] +name = "liblambdashell" +version = "0.1.0" +dependencies = [ + "color-print", + "const_format", + "home", + "mlua", + "uzers", + "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]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "luau0-src" +version = "0.11.2+luau653" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02313a53daf1fae25e82f7e7ca56180b72d1f08c514426672877cd957298201c" +dependencies = [ + "cc", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "mlua" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ea43c3ffac2d0798bd7128815212dd78c98316b299b7a902dabef13dc7b6b8d" +dependencies = [ + "bstr", + "either", + "libloading", + "mlua-sys", + "num-traits", + "parking_lot", + "rustc-hash", +] + +[[package]] +name = "mlua-sys" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63a11d485edf0f3f04a508615d36c7d50d299cf61a7ee6d3e2530651e0a31771" +dependencies = [ + "cc", + "cfg-if", + "luau0-src", + "pkg-config", +] + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + +[[package]] +name = "pkg-config" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +dependencies = [ + "bitflags", +] + +[[package]] +name = "rustc-hash" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "serde" +version = "1.0.215" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.215" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "syn" +version = "2.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + +[[package]] +name = "uzers" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4df81ff504e7d82ad53e95ed1ad5b72103c11253f39238bcc0235b90768a97dd" +dependencies = [ + "libc", + "log", +] + +[[package]] +name = "wasite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" + +[[package]] +name = "wasm-bindgen" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" + +[[package]] +name = "web-sys" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "whoami" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "372d5b87f58ec45c384ba03563b03544dc5fadc3983e434b286913f5b4a9bb6d" +dependencies = [ + "redox_syscall", + "wasite", + "web-sys", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..697535b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "liblambdashell" +version = "0.1.0" +edition = "2021" + +[dependencies] +color-print = "0.3.7" +const_format = "0.2.33" +home = "0.5.9" +mlua = { version = "0.10.0", features = ["luau-jit"] } +uzers = "0.12.1" +whoami = "1.5.2" diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..19e26e3 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,162 @@ +use uzers::User; +use std::{ + io, + process, + str::SplitWhitespace, + path::{Path, PathBuf}, +}; + +enum ValidStatus { + NoRootFolder, + TryExists(io::Error) +} + +trait PathBufIsValid { + fn is_valid(&self) -> Result; + fn is_valid_or_home(&self) -> Option; +} + +trait ChangeDirectory { + fn change_directory(&self, args: SplitWhitespace) -> Option; + fn set_current_dir(&self, new_path: &Path) -> Option; + fn specific_user_dir(&self, user: String) -> Option; + fn cd_args(&self, vec_args: Vec) -> Option; + fn previous_dir(&self) -> Option; + fn home_dir(&self) -> Option; +} + +impl PathBufIsValid for PathBuf { + fn is_valid(&self) -> Result { + match self.try_exists() { + Ok(root_folder_exist) => match root_folder_exist { + true => Ok(self.to_path_buf()), + false => Err(ValidStatus::NoRootFolder) + }, + Err(trye_error) => Err(ValidStatus::TryExists(trye_error)) + } + } + + fn is_valid_or_home(&self) -> Option { + match self.is_valid() { + Ok(valid) => Some(valid), + Err(valid_status) => { + match valid_status { + ValidStatus::NoRootFolder => println!("cd: /root: No such file or directory"), + ValidStatus::TryExists(error) => println!("cd: {error}"), + }; + None + }, + } + } +} + +impl ChangeDirectory for Command { + fn set_current_dir(&self, new_path: &Path) -> Option { + match std::env::set_current_dir(new_path) { + Ok(()) => Some(new_path.to_path_buf()), + Err(set_cd_err) => { + println!("{set_cd_err}"); + None + }, + } + } + + fn home_dir(&self) -> Option { + match home::home_dir() { + Some(home_path_buf) => self.set_current_dir(&home_path_buf), + None => self.set_current_dir(Path::new("/")), + } + } + + fn previous_dir(&self) -> Option { + unimplemented!() + } + + fn specific_user_dir(&self, requested_user: String) -> Option { + match requested_user.as_str() { + "root" => PathBuf::from("/root").is_valid_or_home(), + _ => { + for user in unsafe { uzers::all_users().collect::>() } { + let user_name = user.name(); + if *requested_user == *user_name { + let mut user_dir = PathBuf::from("/home"); + user_dir.push(user_name); + return user_dir.is_valid_or_home(); + } + } + None + } + } + } + + fn cd_args(&self, vec_args: Vec) -> Option { + let string_path = vec_args.concat(); + let new_path = Path::new(string_path.as_str()); + match new_path.is_dir() { + true => self.set_current_dir(new_path), + false => { + match new_path.file_name() { + Some(file_name) => println!("cd: {:?} is not a directory.", file_name), + None => println!("cd: Failed to resolve the file name of a file that is not a directory."), + } + None + } + } + } + + fn change_directory(&self, args: SplitWhitespace) -> Option { + let vec_args: Vec = args.map(|arg| arg.to_string()).collect(); + match vec_args.first() { + Some(arg) => match arg.as_str() { + "/" => self.set_current_dir(Path::new("/")), + "-" => self.previous_dir(), + _ => { + let mut arg_chars = arg.chars(); + match arg_chars.next() { + Some(char) => match char == '~' { + true => self.specific_user_dir(arg_chars.collect::()), + false => self.cd_args(vec_args), + }, + None => self.home_dir(), + } + } + }, + None => self.home_dir(), + } + } +} + +pub type ProcessExitStatus = Option; +pub struct Command(String); +impl Command { + pub fn new(input: String) -> Self { + Self(input) + } + + pub fn spawn(&self, command_process: io::Result) -> ProcessExitStatus { + match command_process { + Ok(mut child) => Some(match child.wait() { + Ok(exit_status) => exit_status, + Err(exit_status_err) => { + println!("{exit_status_err}"); + return None; + } + }), + Err(e) => { + println!("{e}"); + return None; + } + } + } + + pub fn exec(&self) -> ProcessExitStatus { + let mut args = self.0.split_whitespace(); + args.next().and_then(|command| match command { + "cd" => { + Self::change_directory(self, args); + None + } + command => self.spawn(process::Command::new(command).args(args).spawn()), + }) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..eb276bc --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,6 @@ +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); + +pub mod shell; +mod commands; +mod ps; +mod rc; \ No newline at end of file diff --git a/src/ps.rs b/src/ps.rs new file mode 100644 index 0000000..8acf18a --- /dev/null +++ b/src/ps.rs @@ -0,0 +1,26 @@ +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 { + match std::env::current_dir() { + Ok(pathbuf) => match pathbuf.file_name() { + Some(name) => { + let name_os_string = name.to_os_string(); + match name_os_string == whoami::username_os() && name_os_string != "root" { + true => "~".to_string(), + false => name.to_string_lossy().to_string(), + } + } + None => "?".to_string(), + }, + Err(_) => "?".to_string(), + } +} + +pub fn display(ps1: &String) { + // let exit_status = shell_storage.command_exit_status.map(|s| format!(" [{s}] ")).unwrap_or(" ".to_string()); + let working_dir_name = cformat!(" {} ", working_dir_name()); + cprint!("{}{}λ ", ps1, working_dir_name); +} diff --git a/src/rc.rs b/src/rc.rs new file mode 100644 index 0000000..627cb37 --- /dev/null +++ b/src/rc.rs @@ -0,0 +1,44 @@ +use std::{io, path::PathBuf}; + +pub enum RcError { + FolderMissing, + FolderTryExists(io::Error) +} + +trait is_valid { + fn is_valid(&self) -> Option; +} + +impl is_valid for PathBuf { + fn is_valid(&self) -> Option { + let try_exists = match self.try_exists() { + Ok(config_exist) => match config_exist { + true => Ok(self.to_path_buf()), + false => Err(RcError::FolderMissing), + }, + Err(trye_error) => Err(RcError::FolderTryExists(trye_error)), + }; + match try_exists { + Ok(file) => Some(file.to_path_buf()), + Err(rc_error) => match rc_error { + RcError::FolderMissing => todo!(), + RcError::FolderTryExists(error) => { + println!("{error}"); + None + }, + }, + } + } +} + +fn dot_config_folder() -> Option { + let mut config = home::home_dir()?; + config.push(".config"); + config.is_valid() +} + +fn rc_folder() -> Option { + let mut dot_config = dot_config_folder()?; + dot_config.push("lambdashell"); + dot_config.is_valid() +} diff --git a/src/shell.rs b/src/shell.rs new file mode 100644 index 0000000..04c7969 --- /dev/null +++ b/src/shell.rs @@ -0,0 +1,75 @@ +use crate::{ps, commands}; +use std::io; + +pub struct Config { + pub norc: bool +} + +pub struct LambdaShell { + terminating: bool, + storage: Storage, + config: Config, +} + +struct Storage { + pub command_exit_status: commands::ProcessExitStatus, + pub ps1: String, +} + +impl LambdaShell { + pub fn create(config: Config) -> Self { + Self { + storage: Storage { + command_exit_status: None, + ps1: ps::DEFAULT_PS.to_string(), + }, + terminating: false, + config, + } + } + + fn input(&mut self) { + let mut input = String::new(); + match io::stdin().read_line(&mut input) { + Ok(_size) => { + let trimmed_input = input.trim(); + match trimmed_input { + //special casey + "exit" => self.terminating = true, + _ => self.storage.command_exit_status = commands::Command::new(trimmed_input.to_string()).exec() + }; + } + Err(read_error) => println!("{read_error}"), + }; + } + + pub fn wait(&mut self) -> Result<(), io::Error> { + match io::Write::flush(&mut io::stdout()) { + Ok(()) => { + self.input(); + Ok(()) + } + Err(flush_error) => { + println!("{flush_error}"); + Err(flush_error) + } + } + } + + pub fn start(&mut self) { + ps::display(&self.storage.ps1); + + loop { + match self.terminating { + true => break, + false => match self.wait() { + Ok(()) => ps::display(&self.storage.ps1), + Err(flush_error) => { + println!("{flush_error}"); + break; + } + }, + } + } + } +}