-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(complete): Split up completion code
- Loading branch information
Showing
5 changed files
with
766 additions
and
744 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.