| 1 | use std::ffi::OsStr; |
| 2 | use std::ffi::OsString; |
| 3 | |
| 4 | use clap::builder::StyledStr; |
| 5 | |
| 6 | /// A shell-agnostic completion candidate |
| 7 | #[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 8 | pub struct CompletionCandidate { |
| 9 | value: OsString, |
| 10 | help: Option<StyledStr>, |
| 11 | id: Option<String>, |
| 12 | tag: Option<StyledStr>, |
| 13 | display_order: Option<usize>, |
| 14 | hidden: bool, |
| 15 | } |
| 16 | |
| 17 | impl CompletionCandidate { |
| 18 | /// Create a new completion candidate |
| 19 | pub fn new(value: impl Into<OsString>) -> Self { |
| 20 | let value = value.into(); |
| 21 | Self { |
| 22 | value, |
| 23 | ..Default::default() |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | /// Set the help message of the completion candidate |
| 28 | pub fn help(mut self, help: Option<StyledStr>) -> Self { |
| 29 | self.help = help; |
| 30 | self |
| 31 | } |
| 32 | |
| 33 | /// Only first for a given Id is shown |
| 34 | /// |
| 35 | /// To reduce the risk of conflicts, this should likely contain a namespace. |
| 36 | pub fn id(mut self, id: Option<String>) -> Self { |
| 37 | self.id = id; |
| 38 | self |
| 39 | } |
| 40 | |
| 41 | /// Group candidates by tag |
| 42 | /// |
| 43 | /// Future: these may become user-visible |
| 44 | pub fn tag(mut self, tag: Option<StyledStr>) -> Self { |
| 45 | self.tag = tag; |
| 46 | self |
| 47 | } |
| 48 | |
| 49 | /// Sort weight within a [`CompletionCandidate::tag`] |
| 50 | pub fn display_order(mut self, order: Option<usize>) -> Self { |
| 51 | self.display_order = order; |
| 52 | self |
| 53 | } |
| 54 | |
| 55 | /// Set the visibility of the completion candidate |
| 56 | /// |
| 57 | /// Only shown when there is no visible candidate for completing the current argument. |
| 58 | pub fn hide(mut self, hidden: bool) -> Self { |
| 59 | self.hidden = hidden; |
| 60 | self |
| 61 | } |
| 62 | |
| 63 | /// Add a prefix to the value of completion candidate |
| 64 | /// |
| 65 | /// This is generally used for post-process by [`complete`][crate::engine::complete()] for |
| 66 | /// things like prepending flags, merging delimiter-separated values, etc. |
| 67 | pub fn add_prefix(mut self, prefix: impl Into<OsString>) -> Self { |
| 68 | let suffix = self.value; |
| 69 | let mut value = prefix.into(); |
| 70 | value.push(&suffix); |
| 71 | self.value = value; |
| 72 | self |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// Reflection API |
| 77 | impl CompletionCandidate { |
| 78 | /// Get the literal value being proposed for completion |
| 79 | pub fn get_value(&self) -> &OsStr { |
| 80 | &self.value |
| 81 | } |
| 82 | |
| 83 | /// Get the help message of the completion candidate |
| 84 | pub fn get_help(&self) -> Option<&StyledStr> { |
| 85 | self.help.as_ref() |
| 86 | } |
| 87 | |
| 88 | /// Get the id used for de-duplicating |
| 89 | pub fn get_id(&self) -> Option<&String> { |
| 90 | self.id.as_ref() |
| 91 | } |
| 92 | |
| 93 | /// Get the grouping tag |
| 94 | pub fn get_tag(&self) -> Option<&StyledStr> { |
| 95 | self.tag.as_ref() |
| 96 | } |
| 97 | |
| 98 | /// Get the grouping tag |
| 99 | pub fn get_display_order(&self) -> Option<usize> { |
| 100 | self.display_order |
| 101 | } |
| 102 | |
| 103 | /// Get the visibility of the completion candidate |
| 104 | pub fn is_hide_set(&self) -> bool { |
| 105 | self.hidden |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | impl<S: Into<OsString>> From<S> for CompletionCandidate { |
| 110 | fn from(s: S) -> Self { |
| 111 | CompletionCandidate::new(s.into()) |
| 112 | } |
| 113 | } |