diff options
Diffstat (limited to 'mingling_core')
| -rw-r--r-- | mingling_core/src/comp.rs | 11 | ||||
| -rw-r--r-- | mingling_core/src/comp/comp_ctx.rs | 19 |
2 files changed, 30 insertions, 0 deletions
diff --git a/mingling_core/src/comp.rs b/mingling_core/src/comp.rs index 1769a5c..bf7ab13 100644 --- a/mingling_core/src/comp.rs +++ b/mingling_core/src/comp.rs @@ -1,3 +1,4 @@ +mod comp_ctx; mod flags; mod shell_ctx; mod suggest; @@ -5,6 +6,16 @@ mod suggest; use std::collections::BTreeSet; use std::fmt::Display; +/// Constant defining the name of the completion subcommand. +/// +/// When a user invokes this subcommand (e.g., `your_program __comp`), the +/// program enters completion mode and generates shell completions based on +/// the current shell context. +/// +/// This value is used internally by the completion system to intercept the +/// command-line input and redirect to the completion handler. +pub const COMPLETION_SUBCOMMAND: &str = "__comp"; + #[doc(hidden)] pub use flags::*; #[doc(hidden)] diff --git a/mingling_core/src/comp/comp_ctx.rs b/mingling_core/src/comp/comp_ctx.rs new file mode 100644 index 0000000..02e79c5 --- /dev/null +++ b/mingling_core/src/comp/comp_ctx.rs @@ -0,0 +1,19 @@ +use crate::{COMPLETION_SUBCOMMAND, Program, ProgramCollect}; + +impl<C> Program<C> +where + C: ProgramCollect<Enum = C>, +{ + /// Checks whether the program is currently in a completion mode. + /// + /// This is determined by checking if the special completion subcommand + /// (defined by [`COMPLETION_SUBCOMMAND`]) appears among the parsed arguments. + /// When `true`, the program should generate shell completions instead of + /// running its normal execution path. + pub fn is_completing(&self) -> bool { + // Check if the first argument (args[1]) is the completion subcommand + self.args + .get(1) + .is_some_and(|arg| arg == COMPLETION_SUBCOMMAND) + } +} |
