From c0372a0415971a4e2ebdc1ba290a4a168f075794 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 2 Aug 2026 16:19:05 +0800 Subject: feat(shell_ctx): replace manual arg parsing with macro Use the `special_argument!` macro to simplify extracting shell context values from command-line arguments. --- mingling_core/src/comp/shell_ctx.rs | 42 ++++++++----------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) (limited to 'mingling_core/src/comp') diff --git a/mingling_core/src/comp/shell_ctx.rs b/mingling_core/src/comp/shell_ctx.rs index 1fca325..8efbc9d 100644 --- a/mingling_core/src/comp/shell_ctx.rs +++ b/mingling_core/src/comp/shell_ctx.rs @@ -2,7 +2,7 @@ use std::collections::HashSet; -use crate::{Flag, ShellFlag, Suggest}; +use crate::{Flag, ShellFlag, Suggest, special_argument}; /// Context passed from the shell to the completion system, /// providing information about the current command line state @@ -38,43 +38,19 @@ pub struct ShellContext { impl TryFrom> for ShellContext { type Error = String; - fn try_from(args: Vec) -> Result { - use std::collections::HashMap; - - // Parse arguments into a map for easy lookup - let mut arg_map = HashMap::new(); - let mut i = 0; - while i < args.len() { - if args[i].starts_with('-') { - let key = args[i].clone(); - if i + 1 < args.len() && !args[i + 1].starts_with('-') { - arg_map.insert(key, args[i + 1].clone()); - i += 2; - } else { - arg_map.insert(key, String::new()); - i += 1; - } - } else { - i += 1; - } - } - + fn try_from(mut args: Vec) -> Result { // Extract values with defaults - let command_line = arg_map.get("-f").cloned().unwrap_or_default(); - let cursor_position = arg_map - .get("-C") + let command_line = special_argument!(args, "-f").unwrap_or_default(); + let cursor_position = special_argument!(args, "-C") .and_then(|s| s.parse().ok()) .unwrap_or_default(); - let current_word = arg_map.get("-w").cloned().unwrap_or_default(); - let previous_word = arg_map.get("-p").cloned().unwrap_or_default(); - let command_name = arg_map.get("-c").cloned().unwrap_or_default(); - let word_index = arg_map - .get("-i") + let current_word = special_argument!(args, "-w").unwrap_or_default(); + let previous_word = special_argument!(args, "-p").unwrap_or_default(); + let command_name = special_argument!(args, "-c").unwrap_or_default(); + let word_index = special_argument!(args, "-i") .and_then(|s| s.parse().ok()) .unwrap_or_default(); - let shell_flag = arg_map - .get("-F") - .cloned() + let shell_flag = special_argument!(args, "-F") .map_or(ShellFlag::Other("unknown".to_string()), ShellFlag::from); let all_words = command_line -- cgit