Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add label to espanso cli on match #1720

Merged
merged 5 commits into from Dec 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 24 additions & 6 deletions espanso/src/cli/match_cli/list.rs
Expand Up @@ -43,13 +43,17 @@ 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 +66,34 @@ pub fn print_matches_as_plain(match_list: &[&Match], only_triggers: bool, preser
println!("{trigger}");
} else {
let description = m.description();

if preserve_newlines {
if let Some(label) = &m.label {
if preserve_newlines {
println!("{trigger} - {description} - {label}");
} else {
println!(
"{} - {} - {}",
trigger,
description.replace('\n', " "),
label
)
}
} else if preserve_newlines {
println!("{trigger} - {description}");
} else {
println!("{} - {}", trigger, description.replace('\n', " "));
println!("{} - {}", trigger, description.replace('\n', " "))
}
}
}
}

Ok(())
}

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

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

let json = serde_json::to_string_pretty(&entries)?;
Expand Down