Compare commits

...

2 commits

Author SHA1 Message Date
fa47639ef3
feat: better cd 2025-06-10 18:42:05 +08:00
2061bb4f74
chore: refactor 2025-06-10 12:51:06 +08:00
2 changed files with 16 additions and 9 deletions

View file

@ -1,8 +1,17 @@
use std::env;
// Provides Built-In Shell Functions.
pub fn cd(args: &[String]) {
let path = args[0].to_string();
assert!(env::set_current_dir(&path).is_ok());
let path = if args.is_empty() {
std::env::var("HOME").unwrap_or_default()
} else {
shellexpand::tilde(&args[0]).to_string()
};
let status = std::env::set_current_dir(&path);
if status.is_err() {
println!("cd: no such file or directory: {}", &path);
}
}
pub fn help() {

View file

@ -5,8 +5,6 @@ mod inbuilt;
use inbuilt::*;
use mlua::prelude::*;
use std::env;
use whoami::{username, devicename};
// Helper function for resolving aliases.
fn resolve_alias(config: &mlua::Table, cmd: &str) -> Option<String> {
@ -31,8 +29,8 @@ fn resolve_prompt(config: &mlua::Table) -> String {
// Format current working directory.
fn fmt_cwd() -> String {
let home = env::var("HOME").unwrap_or_default();
let cwd = env::current_dir()
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();
@ -48,8 +46,8 @@ fn fmt_cwd() -> String {
// Context for shell expansion.
fn context(s: &str) -> Option<String> {
match s {
"user" => Some(username()),
"host" => Some(devicename()),
"user" => Some(whoami::username()),
"host" => Some(whoami::devicename()),
"cwd" => Some(fmt_cwd()),
_ => None,
}