Skip to content

Commit

Permalink
refactor: better handling of hint_style_arg
Browse files Browse the repository at this point in the history
  • Loading branch information
graelo committed Jun 1, 2022
1 parent d2eb8f0 commit e13c58d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
25 changes: 21 additions & 4 deletions src/config/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ pub struct Config {
///
/// Underline or surround the hint for increased visibility.
/// If not provided, only the hint colors will be used.
#[clap(short = 's', long, arg_enum, rename_all = "lowercase")]
pub hint_style: Option<HintStyleArg>,
#[clap(short = 's', long = "hint-style", arg_enum, rename_all = "lowercase")]
pub hint_style_arg: Option<HintStyleArg>,

/// Chars surrounding each hint, used with `Surround` style.
#[clap(long, default_value = "{}",
parse(try_from_str = parse_chars))]
parse(try_from_str = try_parse_chars))]
pub hint_surroundings: (char, char),
}

Expand All @@ -78,11 +78,28 @@ pub enum HintStyleArg {
}

/// 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 {
return Err(Error::ExpectedSurroundingPair);
}

let chars: Vec<char> = src.chars().collect();
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))
}
},
}
}
}
2 changes: 1 addition & 1 deletion src/config/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl ConfigExt {
}
"@copyrat-hint-style" => {
let case_insensitive = true;
inner.hint_style = Some(
inner.hint_style_arg = Some(
basic::HintStyleArg::from_str(value, case_insensitive)
.map_err(Error::ExpectedEnumVariant)?,
)
Expand Down
15 changes: 1 addition & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ pub fn run(lines: &[&str], opt: &config::basic::Config) -> Option<ui::Selection>
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 selection: Option<ui::Selection> = {
Expand All @@ -49,7 +36,7 @@ pub fn run(lines: &[&str], opt: &config::basic::Config) -> Option<ui::Selection>
default_output_destination,
&opt.colors,
&opt.hint_alignment,
hint_style,
opt.hint_style(),
);

ui.present()
Expand Down

0 comments on commit e13c58d

Please sign in to comment.