summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-03-19 14:05:06 +0800
committer魏曹先生 <1992414357@qq.com>2026-03-19 14:05:06 +0800
commit8adfd97a8a0805121557029a84a13cab8fe2af34 (patch)
tree37289cccaad71d6540e92b537b9ad097d179274f
parent8aab231188fa50a5180f1ff883ab44131173fdba (diff)
Add global flags and language constants for completion
-rw-r--r--src/bin/jvn_comp.rs46
-rw-r--r--src/systems/comp/context.rs2
2 files changed, 37 insertions, 11 deletions
diff --git a/src/bin/jvn_comp.rs b/src/bin/jvn_comp.rs
index dc723dc..463fdd6 100644
--- a/src/bin/jvn_comp.rs
+++ b/src/bin/jvn_comp.rs
@@ -14,6 +14,25 @@ use just_enough_vcs_cli::systems::{
use log::debug;
use log::{LevelFilter, error, trace};
+const GLOBAL_FLAGS: &[&'static str] = &[
+ "--confirm",
+ "-C",
+ "--help",
+ "-h",
+ "--lang",
+ "--no-error-logs",
+ "--no-progress",
+ "--quiet",
+ "-q",
+ "--renderer",
+ "--verbose",
+ "-V",
+ "--version",
+ "-v",
+];
+
+const LANGUAGES: [&'static str; 2] = ["en", "zh-CN"];
+
fn main() {
// If not in release mode, initialize env_logger to capture logs
#[cfg(debug_assertions)]
@@ -46,28 +65,35 @@ fn main() {
#[cfg(debug_assertions)]
trace_ctx(&ctx);
- // Perform pre-completion for common flags;
- // if completion fails, start matching command nodes for completion
- let result = pre_comp(&ctx);
+ trace!("Try using specific completion");
+ let result = comp(&ctx);
if let Some(suggestions) = result {
handle_comp_result(&Some(suggestions));
} else {
- trace!("Using specific completion");
- let result = comp(ctx);
+ trace!("Using default completion");
+ let result = default_comp(&ctx);
handle_comp_result(&result);
}
}
-fn pre_comp(ctx: &CompletionContext) -> Option<Vec<String>> {
+fn default_comp(ctx: &CompletionContext) -> Option<Vec<String>> {
+ if ctx.current_word.starts_with('-') {
+ return Some(GLOBAL_FLAGS.iter().map(|s| s.to_string()).collect());
+ }
+
// Match and comp Override Renderers
if ctx.previous_word == "--renderer" {
return Some(jv_override_renderers());
}
+ if ctx.previous_word == "--lang" {
+ return Some(LANGUAGES.iter().map(|s| s.to_string()).collect());
+ }
+
None
}
-fn comp(ctx: CompletionContext) -> Option<Vec<String>> {
+fn comp(ctx: &CompletionContext) -> Option<Vec<String>> {
let args: Vec<String> = ctx.all_words.iter().skip(1).cloned().collect();
let nodes = jv_cmd_comp_nodes();
let command = format!("{} ", args.join(" "));
@@ -88,7 +114,7 @@ fn comp(ctx: CompletionContext) -> Option<Vec<String>> {
let match_node: Option<String> = match matching_nodes.len() {
0 => {
- if let Some(result) = try_comp_cmd_nodes(&ctx) {
+ if let Some(result) = try_comp_cmd_nodes(ctx) {
return Some(result);
}
// No matching node found
@@ -111,14 +137,14 @@ fn comp(ctx: CompletionContext) -> Option<Vec<String>> {
#[cfg(debug_assertions)]
match &match_node {
Some(node) => trace!("Matched `{}`", node),
- None => trace!("No competions matched."),
+ None => trace!("No completions matched."),
}
let Some(match_node) = match_node else {
return None;
};
- match_comp(match_node, ctx)
+ match_comp(match_node, ctx.clone())
}
fn try_comp_cmd_nodes(ctx: &CompletionContext) -> Option<Vec<String>> {
diff --git a/src/systems/comp/context.rs b/src/systems/comp/context.rs
index 36d1840..caa92e2 100644
--- a/src/systems/comp/context.rs
+++ b/src/systems/comp/context.rs
@@ -1,6 +1,6 @@
use clap::{Parser, command};
-#[derive(Parser, Debug)]
+#[derive(Parser, Debug, Clone)]
#[command(author, version, about, long_about = None)]
pub struct CompletionContext {
/// The full command line