Skip to content

Commit

Permalink
refactor(complete): Split up completion code
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 9, 2024
1 parent 5a839d6 commit a4775ef
Show file tree
Hide file tree
Showing 5 changed files with 766 additions and 744 deletions.
67 changes: 67 additions & 0 deletions clap_complete/src/dynamic/candidate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::ffi::OsStr;
use std::ffi::OsString;

use clap::builder::StyledStr;

/// A completion candidate definition
///
/// This makes it easier to add more fields to completion candidate,
/// rather than using `(OsString, Option<StyledStr>)` or `(String, Option<StyledStr>)` to represent a completion candidate
#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CompletionCandidate {
/// Main completion candidate content
content: OsString,

/// Help message with a completion candidate
help: Option<StyledStr>,

/// Whether the completion candidate is hidden
hidden: bool,
}

impl CompletionCandidate {
/// Create a new completion candidate
pub fn new(content: impl Into<OsString>) -> Self {
let content = content.into();
Self {
content,
..Default::default()
}
}

/// Set the help message of the completion candidate
pub fn help(mut self, help: Option<StyledStr>) -> Self {
self.help = help;
self
}

/// Set the visibility of the completion candidate
pub fn hide(mut self, hidden: bool) -> Self {
self.hidden = hidden;
self
}

/// Get the content of the completion candidate
pub fn get_content(&self) -> &OsStr {
&self.content
}

/// Get the help message of the completion candidate
pub fn get_help(&self) -> Option<&StyledStr> {
self.help.as_ref()
}

/// Get the visibility of the completion candidate
pub fn is_hide_set(&self) -> bool {
self.hidden
}

/// Add a prefix to the content of completion candidate
pub fn add_prefix(mut self, prefix: impl Into<OsString>) -> Self {
let suffix = self.content;
let mut content = prefix.into();
content.push(&suffix);
self.content = content;
self
}
}
Loading

0 comments on commit a4775ef

Please sign in to comment.