From 2b0053f1d7c7e82059345f42e6a1763dd5b0e01d Mon Sep 17 00:00:00 2001 From: jasmine Date: Fri, 13 Jun 2025 10:33:06 +0800 Subject: [PATCH 1/2] rename project as rush is taken by multiple projects --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 +- rush.lua => rshx.lua | 0 src/inbuilt.rs | 2 +- src/main.rs | 2 +- 6 files changed, 5 insertions(+), 5 deletions(-) rename rush.lua => rshx.lua (100%) diff --git a/Cargo.lock b/Cargo.lock index b50755f..86e187b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,7 +246,7 @@ dependencies = [ ] [[package]] -name = "rush" +name = "rshx" version = "0.1.0" dependencies = [ "mlua", diff --git a/Cargo.toml b/Cargo.toml index 59630fc..d467fc5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "rush" +name = "rshx" version = "0.1.0" edition = "2024" diff --git a/README.md b/README.md index ddcc4d5..96fd142 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# RuSH +# Rust SHell eXtensible Learning rust by making a shell. diff --git a/rush.lua b/rshx.lua similarity index 100% rename from rush.lua rename to rshx.lua diff --git a/src/inbuilt.rs b/src/inbuilt.rs index 87126dd..9092bdf 100644 --- a/src/inbuilt.rs +++ b/src/inbuilt.rs @@ -15,7 +15,7 @@ pub fn cd(args: &[String]) { } pub fn help() { - println!("RuSH v0.1.0"); + println!("rshx v0.1.0"); println!("---------------------"); println!("Available Commands:"); println!(" cd - Change directory"); diff --git a/src/main.rs b/src/main.rs index 7b1c593..a115939 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,7 @@ fn main() { let lua = Lua::new(); // Retreive Lua source code from configuration file. - lua.load("config = require 'rush'") + lua.load("config = require 'rshx'") .exec() .expect("Failed to load configuration."); From 16cd7fb8eb354375ddcc8aeafb413418c96390ed Mon Sep 17 00:00:00 2001 From: jasmine Date: Fri, 13 Jun 2025 15:29:46 +0800 Subject: [PATCH 2/2] chore: refactor --- src/lib.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 54 +---------------------------------------------------- 2 files changed, 53 insertions(+), 53 deletions(-) create mode 100644 src/lib.rs 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();