diff --git a/README.md b/README.md index 6a6ba0e..96fd142 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# Rust SHell eXtended +# Rust SHell eXtensible Learning rust by making a shell. diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..55d2794 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,52 @@ +pub mod core; +pub mod inbuilt; + +pub use core::*; +pub use inbuilt::*; + +// Helper function for resolving aliases. +pub fn resolve_alias(config: &mlua::Table, cmd: &str) -> Option { + config + .get::("shellAliases") + .ok() + .and_then(|aliases| aliases.get(cmd).ok()) +} + +// Return prompt from configuration file with expansion performed. +// Otherwise return default prompt. +pub fn resolve_prompt(config: &mlua::Table) -> String { + let default_prompt = "[${user}@${host}:${dir}]$".to_string(); + + let prompt = config + .get::("prompt") + .unwrap_or(default_prompt) + + " "; + + shellexpand::env_with_context_no_errors(&prompt, context).to_string() +} + +// Format current working directory. +fn fmt_cwd() -> String { + let home = std::env::var("HOME").unwrap_or_default(); + let cwd = std::env::current_dir() + .ok() + .and_then(|p| p.to_str().map(String::from)) + .unwrap_or_default(); + + // Replace $HOME with ~. + if cwd.starts_with(&home) { + cwd.replacen(&home, "~", 1) + } else { + cwd + } +} + +// Context for shell expansion. +fn context(s: &str) -> Option { + match s { + "user" => Some(whoami::username()), + "host" => Some(whoami::devicename()), + "dir" => Some(fmt_cwd()), + _ => None, + } +} diff --git a/src/main.rs b/src/main.rs index a115939..4c39cad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,58 +1,6 @@ -mod core; -use core::*; - -mod inbuilt; -use inbuilt::*; - +use rshx::*; use mlua::prelude::*; -// Helper function for resolving aliases. -fn resolve_alias(config: &mlua::Table, cmd: &str) -> Option { - config - .get::("shellAliases") - .ok() - .and_then(|aliases| aliases.get(cmd).ok()) -} - -// Return prompt from configuration file with expansion performed. -// Otherwise return default prompt. -fn resolve_prompt(config: &mlua::Table) -> String { - let default_prompt = "[${user}@${host}:${cwd}]$".to_string(); - - let prompt = config - .get::("prompt") - .unwrap_or(default_prompt) - + " "; - - shellexpand::env_with_context_no_errors(&prompt, context).to_string() -} - -// Format current working directory. -fn fmt_cwd() -> String { - let home = std::env::var("HOME").unwrap_or_default(); - let cwd = std::env::current_dir() - .ok() - .and_then(|p| p.to_str().map(String::from)) - .unwrap_or_default(); - - // Replace $HOME with ~. - if cwd.starts_with(&home) { - cwd.replacen(&home, "~", 1) - } else { - cwd - } -} - -// Context for shell expansion. -fn context(s: &str) -> Option { - match s { - "user" => Some(whoami::username()), - "host" => Some(whoami::devicename()), - "cwd" => Some(fmt_cwd()), - _ => None, - } -} - fn main() { // Create a new Lua state and load the safe subset of the standard libraries. let lua = Lua::new();