mirror of
https://github.com/sajenim/rush.git
synced 2025-06-28 01:44:21 +08:00
Compare commits
2 commits
c7521732f0
...
16cd7fb8eb
Author | SHA1 | Date | |
---|---|---|---|
16cd7fb8eb | |||
2b0053f1d7 |
7 changed files with 58 additions and 58 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -246,7 +246,7 @@ dependencies = [
|
|||
]
|
||||
|
||||
[[package]]
|
||||
name = "rush"
|
||||
name = "rshx"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"mlua",
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
[package]
|
||||
name = "rush"
|
||||
name = "rshx"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
# RuSH
|
||||
# Rust SHell eXtensible
|
||||
|
||||
Learning rust by making a shell.
|
||||
|
|
|
@ -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");
|
||||
|
|
52
src/lib.rs
Normal file
52
src/lib.rs
Normal file
|
@ -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<String> {
|
||||
config
|
||||
.get::<mlua::Table>("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::<String>("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<String> {
|
||||
match s {
|
||||
"user" => Some(whoami::username()),
|
||||
"host" => Some(whoami::devicename()),
|
||||
"dir" => Some(fmt_cwd()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
56
src/main.rs
56
src/main.rs
|
@ -1,64 +1,12 @@
|
|||
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<String> {
|
||||
config
|
||||
.get::<mlua::Table>("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::<String>("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<String> {
|
||||
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();
|
||||
|
||||
// Retreive Lua source code from configuration file.
|
||||
lua.load("config = require 'rush'")
|
||||
lua.load("config = require 'rshx'")
|
||||
.exec()
|
||||
.expect("Failed to load configuration.");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue