Skip to content

Commit

Permalink
refactor: compgen description remove wrapped parentheses (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Jul 20, 2023
1 parent 5bae09e commit d1d299d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
12 changes: 9 additions & 3 deletions src/compgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,10 @@ fn parse_candidate_value(input: &str) -> CandidateValue {
}
if parts_len > 0 {
if let Some(stripped_value) = parts.first().and_then(|v| v.strip_suffix('\0')) {
value = stripped_value.to_string();
value = stripped_value.trim().to_string();
nospace = true;
} else {
value = parts[0].to_string();
value = parts[0].trim().to_string();
}
}
(value, description, nospace, comp_kind)
Expand All @@ -860,7 +860,13 @@ fn convert_arg_value(value: &str) -> Option<String> {

fn truncate_description(description: &str) -> String {
let max_width = 80;
let description = description.trim().replace('\t', "");
let mut description = description.trim().replace('\t', "");
if description.starts_with('(') && description.ends_with(')') {
description = description
.trim_start_matches('(')
.trim_end_matches(')')
.to_string();
}
if description.len() < max_width {
description
} else {
Expand Down
6 changes: 4 additions & 2 deletions tests/compgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,10 @@ fn desc() {
let script = r###"
# @option --oa[`_choice_fn`]
_choice_fn() {
echo -e "abc\t(desc1)"
echo -e "def\t(desc2)"
echo -e "abc\t desc"
echo -e "def\t (desc) "
echo -e " ijk\t value (desc)"
echo -e " xyz \t [desc]"
}
"###;

Expand Down
42 changes: 28 additions & 14 deletions tests/snapshots/integration__compgen__desc.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,45 @@ source: tests/compgen.rs
expression: data
---
************ COMPGEN Bash `prog --oa ` ************
abc ((desc1))
def ((desc2))
abc (desc)
def (desc)
ijk (value (desc))
xyz ([desc])

************ COMPGEN Elvish `prog --oa ` ************
abc 1 abc (desc1) green
def 1 def (desc2) green
abc 1 abc desc green
def 1 def desc green
ijk 1 ijk value (desc) green
xyz 1 xyz [desc] green

************ COMPGEN Fish `prog --oa ` ************
abc (desc1)
def (desc2)
abc desc
def desc
ijk value (desc)
xyz [desc]

************ COMPGEN Nushell `prog --oa ` ************
abc (desc1)
def (desc2)
abc desc
def desc
ijk value (desc)
xyz [desc]

************ COMPGEN Powershell `prog --oa ` ************
abc 1 abc (desc1) 32
def 1 def (desc2) 32
abc 1 abc desc 32
def 1 def desc 32
ijk 1 ijk value (desc) 32
xyz 1 xyz [desc] 32

************ COMPGEN Xonsh `prog --oa ` ************
abc 1 abc (desc1)
def 1 def (desc2)
abc 1 abc desc
def 1 def desc
ijk 1 ijk value (desc)
xyz 1 xyz [desc]

************ COMPGEN Zsh `prog --oa ` ************
abc abc:(desc1) abc 32
def def:(desc2) def 32
abc abc:desc abc 32
def def:desc def 32
ijk ijk:value (desc) ijk 32
xyz xyz:[desc] xyz 32


12 changes: 6 additions & 6 deletions tests/snapshots/integration__compgen__desc2.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ expression: data
abc

************ COMPGEN Elvish `prog --oa a` ************
abc 1 abc (desc1) green
abc 1 abc desc1 green

************ COMPGEN Fish `prog --oa a` ************
abc (desc1)
abc desc1

************ COMPGEN Nushell `prog --oa a` ************
abc (desc1)
abc desc1

************ COMPGEN Powershell `prog --oa a` ************
abc 1 abc (desc1) 32
abc 1 abc desc1 32

************ COMPGEN Xonsh `prog --oa a` ************
abc 1 abc (desc1)
abc 1 abc desc1

************ COMPGEN Zsh `prog --oa a` ************
abc abc:(desc1) abc 32
abc abc:desc1 abc 32


0 comments on commit d1d299d

Please sign in to comment.