Compare commits

...

3 commits

Author SHA1 Message Date
afe54d381b
add example prompt configuration 2025-06-30 22:42:01 +08:00
297aa689b3
refactor 2025-06-30 22:41:18 +08:00
d631bba13b
rename project 2025-06-30 22:28:27 +08:00
7 changed files with 69 additions and 67 deletions

20
Cargo.lock generated
View file

@ -45,6 +45,16 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crsh"
version = "0.1.0"
dependencies = [
"mlua",
"shellexpand",
"shlex",
"whoami",
]
[[package]]
name = "dirs"
version = "6.0.0"
@ -245,16 +255,6 @@ dependencies = [
"thiserror",
]
[[package]]
name = "rush"
version = "0.1.0"
dependencies = [
"mlua",
"shellexpand",
"shlex",
"whoami",
]
[[package]]
name = "rustc-hash"
version = "2.1.1"

View file

@ -1,5 +1,5 @@
[package]
name = "rush"
name = "crsh"
version = "0.1.0"
edition = "2024"

View file

@ -1,3 +1,3 @@
# RuSH
# Crab Shell
Learning rust by making a shell.

View file

@ -1,6 +1,8 @@
-- This table will hold the configuration.
local config = {}
config.prompt = "[${user}@${host}:${dir}$"
config.shellAliases = {
ls = "ls --color=auto",
}

View file

@ -15,7 +15,7 @@ pub fn cd(args: &[String]) {
}
pub fn help() {
println!("RuSH v0.1.0");
println!("crsh v0.1.0");
println!("---------------------");
println!("Available Commands:");
println!(" cd - Change directory");

52
src/lib.rs Normal file
View 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,
}
}

View file

@ -1,64 +1,12 @@
mod core;
use core::*;
mod inbuilt;
use inbuilt::*;
use crsh::*;
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 'crsh'")
.exec()
.expect("Failed to load configuration.");