refactor: better handling of hint_style_arg

This commit is contained in:
graelo 2022-06-01 08:46:42 +02:00
parent d2eb8f0e16
commit e13c58db30
3 changed files with 23 additions and 19 deletions

View file

@ -58,12 +58,12 @@ pub struct Config {
/// ///
/// Underline or surround the hint for increased visibility. /// Underline or surround the hint for increased visibility.
/// If not provided, only the hint colors will be used. /// If not provided, only the hint colors will be used.
#[clap(short = 's', long, arg_enum, rename_all = "lowercase")] #[clap(short = 's', long = "hint-style", arg_enum, rename_all = "lowercase")]
pub hint_style: Option<HintStyleArg>, pub hint_style_arg: Option<HintStyleArg>,
/// Chars surrounding each hint, used with `Surround` style. /// Chars surrounding each hint, used with `Surround` style.
#[clap(long, default_value = "{}", #[clap(long, default_value = "{}",
parse(try_from_str = parse_chars))] parse(try_from_str = try_parse_chars))]
pub hint_surroundings: (char, char), pub hint_surroundings: (char, char),
} }
@ -78,7 +78,7 @@ pub enum HintStyleArg {
} }
/// Try to parse a `&str` into a tuple of `char`s. /// Try to parse a `&str` into a tuple of `char`s.
fn parse_chars(src: &str) -> Result<(char, char)> { fn try_parse_chars(src: &str) -> Result<(char, char)> {
if src.chars().count() != 2 { if src.chars().count() != 2 {
return Err(Error::ExpectedSurroundingPair); return Err(Error::ExpectedSurroundingPair);
} }
@ -86,3 +86,20 @@ fn parse_chars(src: &str) -> Result<(char, char)> {
let chars: Vec<char> = src.chars().collect(); let chars: Vec<char> = src.chars().collect();
Ok((chars[0], chars[1])) Ok((chars[0], chars[1]))
} }
impl Config {
pub fn hint_style(&self) -> Option<ui::HintStyle> {
match &self.hint_style_arg {
None => None,
Some(style) => match style {
HintStyleArg::Bold => Some(ui::HintStyle::Bold),
HintStyleArg::Italic => Some(ui::HintStyle::Italic),
HintStyleArg::Underline => Some(ui::HintStyle::Underline),
HintStyleArg::Surround => {
let (open, close) = self.hint_surroundings;
Some(ui::HintStyle::Surround(open, close))
}
},
}
}
}

View file

@ -96,7 +96,7 @@ impl ConfigExt {
} }
"@copyrat-hint-style" => { "@copyrat-hint-style" => {
let case_insensitive = true; let case_insensitive = true;
inner.hint_style = Some( inner.hint_style_arg = Some(
basic::HintStyleArg::from_str(value, case_insensitive) basic::HintStyleArg::from_str(value, case_insensitive)
.map_err(Error::ExpectedEnumVariant)?, .map_err(Error::ExpectedEnumVariant)?,
) )

View file

@ -27,19 +27,6 @@ pub fn run(lines: &[&str], opt: &config::basic::Config) -> Option<ui::Selection>
return None; return None;
} }
let hint_style = match &opt.hint_style {
None => None,
Some(style) => match style {
config::basic::HintStyleArg::Bold => Some(ui::HintStyle::Bold),
config::basic::HintStyleArg::Italic => Some(ui::HintStyle::Italic),
config::basic::HintStyleArg::Underline => Some(ui::HintStyle::Underline),
config::basic::HintStyleArg::Surround => {
let (open, close) = opt.hint_surroundings;
Some(ui::HintStyle::Surround(open, close))
}
},
};
let default_output_destination = config::extended::OutputDestination::Tmux; let default_output_destination = config::extended::OutputDestination::Tmux;
let selection: Option<ui::Selection> = { let selection: Option<ui::Selection> = {
@ -49,7 +36,7 @@ pub fn run(lines: &[&str], opt: &config::basic::Config) -> Option<ui::Selection>
default_output_destination, default_output_destination,
&opt.colors, &opt.colors,
&opt.hint_alignment, &opt.hint_alignment,
hint_style, opt.hint_style(),
); );
ui.present() ui.present()