Skip to content

Commit

Permalink
Implement unalias
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Dec 22, 2024
1 parent e83d24f commit fa8fa41
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
20 changes: 3 additions & 17 deletions src/core/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//SPDX-FileCopyrightText: 2023 @caro@mi.shellgei.org
//SPDX-License-Identifier: BSD-3-Clause

mod alias;
mod cd;
pub mod completion;
mod history;
Expand All @@ -21,7 +22,7 @@ use crate::utils::exit;
impl ShellCore {
pub fn set_builtins(&mut self) {
self.builtins.insert(":".to_string(), true_);
self.builtins.insert("alias".to_string(), alias);
self.builtins.insert("alias".to_string(), alias::alias);
self.builtins.insert("bg".to_string(), job_commands::bg);
self.builtins.insert("break".to_string(), loop_control::break_);
self.builtins.insert("cd".to_string(), cd::cd);
Expand All @@ -42,6 +43,7 @@ impl ShellCore {
self.builtins.insert("set".to_string(), option::set);
self.builtins.insert("shift".to_string(), option::shift);
self.builtins.insert("shopt".to_string(), option::shopt);
self.builtins.insert("unalias".to_string(), alias::unalias);
self.builtins.insert("unset".to_string(), unset::unset);
self.builtins.insert("source".to_string(), source::source);
self.builtins.insert(".".to_string(), source::source);
Expand All @@ -50,22 +52,6 @@ impl ShellCore {
}
}

pub fn alias(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.len() == 1 {
for (k, v) in &core.db.aliases {
println!("alias {}='{}'", k, v);
}
return 0;
}

if args.len() == 2 && args[1].find("=") != None {
let kv: Vec<String> = args[1].split("=").map(|t| t.to_string()).collect();
core.db.aliases.insert(kv[0].clone(), kv[1..].join("="));
}

0
}

pub fn eval(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
let mut feeder = Feeder::new(&args[1..].join(" "));

Expand Down
45 changes: 45 additions & 0 deletions src/core/builtins/alias.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//SPDX-FileCopyrightText: 2024 Ryuichi Ueda <ryuichiueda@gmail.com>
//SPDX-License-Identifier: BSD-3-Clause

use crate::ShellCore;

pub fn alias(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.len() == 1 {
for (k, v) in &core.db.aliases {
println!("alias {}='{}'", k, v);
}
return 0;
}

if args.len() == 2 && args[1].find("=") != None {
let kv: Vec<String> = args[1].split("=").map(|t| t.to_string()).collect();
core.db.aliases.insert(kv[0].clone(), kv[1..].join("="));
}

0
}

pub fn unalias(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.len() <= 1 {
println!("unalias: usage: unalias [-a] name [name ...]");
}

if args.contains(&"-a".to_string()) {
core.db.aliases.clear();
return 0;
}

for alias in &mut args[1..] {
dbg!("{:?}", &alias);
core.db.aliases.remove_entry(alias);
}

/*
if args.len() == 2 && args[1].find("=") != None {
let kv: Vec<String> = args[1].split("=").map(|t| t.to_string()).collect();
core.db.aliases.insert(kv[0].clone(), kv[1..].join("="));
}*/

0
}

0 comments on commit fa8fa41

Please sign in to comment.