diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-08-02 16:19:05 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-08-02 16:19:05 +0800 |
| commit | c0372a0415971a4e2ebdc1ba290a4a168f075794 (patch) | |
| tree | 08130055d2079ad2d072ff9c5d17823d4f8de9a8 /mingling_core/src/comp | |
| parent | baa4a8ec475ba8e7887fc8fa17582830411f1355 (diff) | |
feat(shell_ctx): replace manual arg parsing with macro
Use the `special_argument!` macro to simplify extracting
shell context values from command-line arguments.
Diffstat (limited to 'mingling_core/src/comp')
| -rw-r--r-- | mingling_core/src/comp/shell_ctx.rs | 42 |
1 files changed, 9 insertions, 33 deletions
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<Vec<String>> for ShellContext { type Error = String; - fn try_from(args: Vec<String>) -> Result<Self, Self::Error> { - 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<String>) -> Result<Self, Self::Error> { // 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 |
