From fa8fa41d0760043de90f32e7f03a969c02992e4f Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Sun, 22 Dec 2024 12:04:48 +0900 Subject: [PATCH] Implement unalias --- src/core/builtins.rs | 20 +++-------------- src/core/builtins/alias.rs | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 src/core/builtins/alias.rs diff --git a/src/core/builtins.rs b/src/core/builtins.rs index 8c76dcea..03181272 100644 --- a/src/core/builtins.rs +++ b/src/core/builtins.rs @@ -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; @@ -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); @@ -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); @@ -50,22 +52,6 @@ impl ShellCore { } } -pub fn alias(core: &mut ShellCore, args: &mut Vec) -> 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 = 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) -> i32 { let mut feeder = Feeder::new(&args[1..].join(" ")); diff --git a/src/core/builtins/alias.rs b/src/core/builtins/alias.rs new file mode 100644 index 00000000..bb9ebcf0 --- /dev/null +++ b/src/core/builtins/alias.rs @@ -0,0 +1,45 @@ +//SPDX-FileCopyrightText: 2024 Ryuichi Ueda +//SPDX-License-Identifier: BSD-3-Clause + +use crate::ShellCore; + +pub fn alias(core: &mut ShellCore, args: &mut Vec) -> 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 = 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) -> 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 = args[1].split("=").map(|t| t.to_string()).collect(); + core.db.aliases.insert(kv[0].clone(), kv[1..].join("=")); + }*/ + + 0 +} +