Skip to content

Commit

Permalink
refactor: compgen color
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jul 20, 2023
1 parent 9414c76 commit a3eb1d5
Show file tree
Hide file tree
Showing 28 changed files with 680 additions and 432 deletions.
388 changes: 258 additions & 130 deletions src/compgen.rs

Large diffs are not rendered by default.

36 changes: 22 additions & 14 deletions src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use crate::{
command::Command,
compgen::CompKind,
compgen::CompColor,
param::{FlagOptionParam, PositionalParam},
utils::run_param_fns,
ArgcValue, Shell,
Expand Down Expand Up @@ -251,7 +251,11 @@ impl<'a, 'b> Matcher<'a, 'b> {
.iter()
.any(|v| redirect_symbols.contains(&v.as_str()))
{
return vec![("__argc_value=path".into(), String::new(), CompKind::Value)];
return vec![(
"__argc_value=path".into(),
String::new(),
CompColor::of_value(),
)];
}
let level = self.cmds.len() - 1;
let mut last_cmd = self.cmds[level].1;
Expand All @@ -272,9 +276,9 @@ impl<'a, 'b> Matcher<'a, 'b> {
{
let describe = param.describe_head();
let kind = if param.is_flag() {
CompKind::Flag
CompColor::of_flag()
} else {
CompKind::Option
CompColor::of_option()
};
output.push((value.clone(), describe.into(), kind));
}
Expand All @@ -293,7 +297,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
})
.collect();
if output.len() == 1 {
output.insert(0, (value.to_string(), String::new(), CompKind::Flag));
output.insert(0, (value.to_string(), String::new(), CompColor::of_flag()));
}
output
}
Expand Down Expand Up @@ -329,7 +333,11 @@ impl<'a, 'b> Matcher<'a, 'b> {
&& !self.arg_comp.is_flag_or_option()
&& last_cmd.positional_params.is_empty()
{
output.push(("__argc_value=path".into(), String::new(), CompKind::Value));
output.push((
"__argc_value=path".into(),
String::new(),
CompColor::of_value(),
));
}
output
}
Expand Down Expand Up @@ -760,9 +768,9 @@ impl<'a, 'b> Matcher<'a, 'b> {
if !exist || param.multiple {
let describe = param.describe_head();
let kind = if param.is_flag() {
CompKind::Flag
CompColor::of_flag()
} else {
CompKind::Option
CompColor::of_option()
};
for v in param.list_names() {
output.push((v, describe.to_string(), kind))
Expand All @@ -773,7 +781,7 @@ impl<'a, 'b> Matcher<'a, 'b> {
}
}

pub(crate) type CompItem = (String, String, CompKind);
pub(crate) type CompItem = (String, String, CompColor);

fn add_positional_arg<'a>(
positional_args: &mut Vec<&'a str>,
Expand Down Expand Up @@ -886,7 +894,7 @@ fn comp_subcomands(cmd: &Command) -> Vec<CompItem> {
for subcmd in cmd.subcommands.iter() {
let describe = subcmd.describe_head();
for v in subcmd.list_names() {
output.push((v, describe.to_string(), CompKind::Command))
output.push((v, describe.to_string(), CompColor::of_command()))
}
}
output
Expand Down Expand Up @@ -935,25 +943,25 @@ fn comp_param(
match choices {
Either::Left(choices) => choices
.iter()
.map(|v| (v.to_string(), String::new(), CompKind::Value))
.map(|v| (v.to_string(), String::new(), CompColor::of_value()))
.collect(),
Either::Right(choices_fn) => vec![(
format!("__argc_fn={}", choices_fn),
String::new(),
CompKind::Value,
CompColor::of_value(),
)],
}
} else {
let value = format!("__argc_value={}", value_name);
vec![(value, describe.into(), CompKind::Value)]
vec![(value, describe.into(), CompColor::of_value())]
};
if let Some(ch) = multi_char {
output.insert(
0,
(
format!("__argc_multi={}", ch),
String::new(),
CompKind::Value,
CompColor::of_value(),
),
);
}
Expand Down
40 changes: 27 additions & 13 deletions tests/compgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ _choice_fn() {
echo -e "c1\t"
echo -e "d1\0"
echo -e "e1"
echo -e "f1\t/kind:valueOther\tdesc f1"
echo -e "f1\t/color:yellow\tdesc f1"
}
"###;

Expand Down Expand Up @@ -768,18 +768,32 @@ fn color() {
let script = r###"
# @arg val[`_fn_color`]
_fn_color() {
echo -e "flag\0\t/kind:flag"
echo -e "option\0\t/kind:option"
echo -e "command\0\t/kind:command"
echo -e "dir\0\t/kind:dir"
echo -e "file\0\t/kind:file"
echo -e "fileExe\0\t/kind:fileExe"
echo -e "symlink\0\t/kind:symlink"
echo -e "valueOther\0\t/kind:valueOther"
echo -e "valueAnother\0\t/kind:valueAnother"
echo -e "valueEmphasis\0\t/kind:valueEmphasis"
echo -e "valueSubtle\0\t/kind:valueSubtle"
echo -e "value\0\t/kind:value"
echo -e "kindFlag\0\t/color:cyan"
echo -e "kindOption\0\t/color:cyan,bold"
echo -e "kindCommand\0\t/color:magenta"
echo -e "kindDir\0\t/color:blue,bold"
echo -e "kindFile\0\t/color:default"
echo -e "kindFileExe\0\t/color:green,bold"
echo -e "kindSymlink\0\t/color:cyan,bold"
echo -e "kindValue\0\t/color:green"
echo -e "colorBlack\0\t/color:black"
echo -e "colorBlackBold\0\t/color:black,bold"
echo -e "colorRed\0\t/color:red"
echo -e "colorRedBold\0\t/color:red,bold"
echo -e "colorGreen\0\t/color:green"
echo -e "colorGreenBold\0\t/color:green,bold"
echo -e "colorYellow\0\t/color:yellow"
echo -e "colorYellowBold\0\t/color:yellow,bold"
echo -e "colorBlue\0\t/color:blue"
echo -e "colorBlueBold\0\t/color:blue,bold"
echo -e "colorMagenta\0\t/color:magenta"
echo -e "colorMagentaBold\0\t/color:magenta,bold"
echo -e "colorCyan\0\t/color:cyan"
echo -e "colorCyanBold\0\t/color:cyan,bold"
echo -e "colorWhite\0\t/color:white"
echo -e "colorWhiteBold\0\t/color:white,bold"
echo -e "colorDefault\0\t/color:default"
echo -e "colorDefaultBold\0\t/color:default,bold"
}
"###;
snapshot_compgen_shells!(script, vec!["prog", ""]);
Expand Down
30 changes: 15 additions & 15 deletions tests/snapshots/integration__compgen__arg_terminated.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ source: tests/compgen.rs
expression: data
---
************ COMPGEN `sudo cmd ` ************
__argc_filter= /kind:value
cmd /kind:value
ok /kind:value
__argc_filter= /color:green
cmd /color:green
ok /color:green

************ COMPGEN `sudo cmd -` ************
__argc_filter= /kind:value
cmd - /kind:value
ok /kind:value
__argc_filter= /color:green
cmd - /color:green
ok /color:green

************ COMPGEN `sudo cmd --` ************
__argc_filter= /kind:value
cmd -- /kind:value
ok /kind:value
__argc_filter= /color:green
cmd -- /color:green
ok /color:green

************ COMPGEN `sudo cmd --foo` ************
__argc_filter= /kind:value
cmd --foo /kind:value
ok /kind:value
__argc_filter= /color:green
cmd --foo /color:green
ok /color:green

************ COMPGEN `sudo cmd foo` ************
__argc_filter= /kind:value
cmd foo /kind:value
ok /kind:value
__argc_filter= /color:green
cmd foo /color:green
ok /color:green


36 changes: 18 additions & 18 deletions tests/snapshots/integration__compgen__assing_option_value.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ source: tests/compgen.rs
expression: data
---
************ COMPGEN `prog --oa=abc` ************
__argc_filter= /kind:value
ARGC_COMPGEN=1 /kind:value
ARGC_FILTER=abc /kind:value
ARGC_LAST_ARG=--oa=abc /kind:value
ARGC_VARS=YXJnY19vYT1hYmM7YXJnY19fYXJncz0oIHByb2cgJy0tb2E9YWJjJyApO2FyZ2NfX2luZGV4PTA7YXJnY19fcG9zaXRpb25hbHM9KCAgKTs= /kind:value
argc__args=([0]="prog" [1]="--oa=abc") /kind:value
argc__index=0 /kind:value
argc__positionals=() /kind:value
argc_oa=abc /kind:value
__argc_filter= /color:green
ARGC_COMPGEN=1 /color:green
ARGC_FILTER=abc /color:green
ARGC_LAST_ARG=--oa=abc /color:green
ARGC_VARS=YXJnY19vYT1hYmM7YXJnY19fYXJncz0oIHByb2cgJy0tb2E9YWJjJyApO2FyZ2NfX2luZGV4PTA7YXJnY19fcG9zaXRpb25hbHM9KCAgKTs= /color:green
argc__args=([0]="prog" [1]="--oa=abc") /color:green
argc__index=0 /color:green
argc__positionals=() /color:green
argc_oa=abc /color:green

************ COMPGEN `prog oa=abc` ************
__argc_filter= /kind:value
ARGC_COMPGEN=1 /kind:value
ARGC_FILTER=oa=abc /kind:value
ARGC_LAST_ARG=oa=abc /kind:value
ARGC_VARS=YXJnY192YWw9J29hPWFiYyc7YXJnY19fYXJncz0oIHByb2cgJ29hPWFiYycgKTthcmdjX19pbmRleD0wO2FyZ2NfX3Bvc2l0aW9uYWxzPSggJ29hPWFiYycgKTs= /kind:value
argc__args=([0]="prog" [1]="oa=abc") /kind:value
argc__index=0 /kind:value
argc__positionals=([0]="oa=abc") /kind:value
argc_val=oa=abc /kind:value
__argc_filter= /color:green
ARGC_COMPGEN=1 /color:green
ARGC_FILTER=oa=abc /color:green
ARGC_LAST_ARG=oa=abc /color:green
ARGC_VARS=YXJnY192YWw9J29hPWFiYyc7YXJnY19fYXJncz0oIHByb2cgJ29hPWFiYycgKTthcmdjX19pbmRleD0wO2FyZ2NfX3Bvc2l0aW9uYWxzPSggJ29hPWFiYycgKTs= /color:green
argc__args=([0]="prog" [1]="oa=abc") /color:green
argc__index=0 /color:green
argc__positionals=([0]="oa=abc") /color:green
argc_val=oa=abc /color:green


Binary file modified tests/snapshots/integration__compgen__choice.snap
Binary file not shown.
28 changes: 14 additions & 14 deletions tests/snapshots/integration__compgen__choice_check_vars.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ source: tests/compgen.rs
expression: data
---
************ COMPGEN `prog argc` ************
argc__args=([0]="prog" [1]="argc") /kind:value
argc__index=0 /kind:value
argc__positionals=([0]="argc") /kind:value
argc_foo=argc /kind:value
argc__args=([0]="prog" [1]="argc") /color:green
argc__index=0 /color:green
argc__positionals=([0]="argc") /color:green
argc_foo=argc /color:green

************ COMPGEN `prog argc ` ************
argc__args=([0]="prog" [1]="argc" [2]="") /kind:value
argc__index=0 /kind:value
argc__positionals=([0]="argc" [1]="") /kind:value
argc_bar= /kind:value
argc_foo=argc /kind:value
argc__args=([0]="prog" [1]="argc" [2]="") /color:green
argc__index=0 /color:green
argc__positionals=([0]="argc" [1]="") /color:green
argc_bar= /color:green
argc_foo=argc /color:green

************ COMPGEN `prog argc argc` ************
argc__args=([0]="prog" [1]="argc" [2]="argc") /kind:value
argc__index=0 /kind:value
argc__positionals=([0]="argc" [1]="argc") /kind:value
argc_bar=argc /kind:value
argc_foo=argc /kind:value
argc__args=([0]="prog" [1]="argc" [2]="argc") /color:green
argc__index=0 /color:green
argc__positionals=([0]="argc" [1]="argc") /color:green
argc_bar=argc /color:green
argc_foo=argc /color:green


12 changes: 6 additions & 6 deletions tests/snapshots/integration__compgen__choice_multi.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ source: tests/compgen.rs
expression: data
---
************ COMPGEN `prog --oa ` ************
abc /kind:value
def /kind:value
ghi /kind:value
abc /color:green
def /color:green
ghi /color:green

************ COMPGEN `prog --oa=` ************
abc /kind:value
def /kind:value
ghi /kind:value
abc /color:green
def /color:green
ghi /color:green


2 changes: 1 addition & 1 deletion tests/snapshots/integration__compgen__choice_slash.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ source: tests/compgen.rs
expression: data
---
************ COMPGEN `prog cmd a\b ` ************
a\b /kind:value
a\b /color:green


Loading

0 comments on commit a3eb1d5

Please sign in to comment.