Skip to content

Commit

Permalink
refactor(match_cli): update print_matches functions to include label …
Browse files Browse the repository at this point in the history
…and handle errors
  • Loading branch information
kud committed Oct 5, 2023
1 parent 97f97a1 commit 8f13818
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions espanso/src/cli/match_cli/list.rs
Expand Up @@ -43,13 +43,13 @@ pub fn list_main(
if cli_args.is_present("json") {
print_matches_as_json(&match_set.matches)?;
} else {
print_matches_as_plain(&match_set.matches, only_triggers, preserve_newlines)
print_matches_as_plain(&match_set.matches, only_triggers, preserve_newlines)?;
}

Ok(())
}

pub fn print_matches_as_plain(match_list: &[&Match], only_triggers: bool, preserve_newlines: bool) {
pub fn print_matches_as_plain(match_list: &[&Match], only_triggers: bool, preserve_newlines: bool) -> Result<()> {
for m in match_list {
let triggers = match &m.cause {
MatchCause::None => vec!["(none)".to_string()],
Expand All @@ -62,21 +62,25 @@ pub fn print_matches_as_plain(match_list: &[&Match], only_triggers: bool, preser
println!("{}", trigger);
} else {
let description = m.description();
let label = &m.label;

if preserve_newlines {
println!("{} - {}", trigger, description)
println!("{} - {} - {}", trigger, description, label)
} else {
println!("{} - {}", trigger, description.replace('\n', " "))
println!("{} - {} - {}", trigger, description.replace('\n', " "), label)
}
}
}
}

Ok(())
}

#[derive(Debug, Serialize)]
struct JsonMatchEntry {
triggers: Vec<String>,
replace: String,
label: String,
}

pub fn print_matches_as_json(match_list: &[&Match]) -> Result<()> {
Expand All @@ -91,6 +95,7 @@ pub fn print_matches_as_json(match_list: &[&Match]) -> Result<()> {
entries.push(JsonMatchEntry {
triggers,
replace: m.description().to_string(),
label: m.label.to_string(),
})
}

Expand Down

0 comments on commit 8f13818

Please sign in to comment.