Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat: brief output for `just getmetadata` (#2197)

Andrew Dunbarcd9251480893
diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rs
index f1af636d5..c040d740f 100644
--- a/harper-cli/src/main.rs
+++ b/harper-cli/src/main.rs
@@ -92,7 +92,12 @@ enum Args {
annotation_type: AnnotationType,
},
/// Get the metadata associated with one or more words.
- Metadata { words: Vec<String> },
+ Metadata {
+ words: Vec<String>,
+ /// Only show the part-of-speech flags and emojis, not the full JSON
+ #[arg(short, long)]
+ brief: bool,
+ },
/// Get all the forms of a word using the affixes.
Forms { line: String },
/// Emit a decompressed, line-separated list of the words in Harper's dictionary.
@@ -372,14 +377,41 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
- Args::Metadata { words } => {
- let mut results = BTreeMap::new();
+ Args::Metadata { words, brief } => {
+ type PosPredicate = fn(&DictWordMetadata) -> bool;
+
+ const POS: &[(&str, PosPredicate)] = &[
+ ("NπŸ“¦", |m| m.is_noun() && !m.is_proper_noun()),
+ ("OπŸ“›", DictWordMetadata::is_proper_noun),
+ ("VπŸƒ", DictWordMetadata::is_verb),
+ ("J🌈", DictWordMetadata::is_adjective),
+ ("R🀷", DictWordMetadata::is_adverb),
+ ("CπŸ”—", DictWordMetadata::is_conjunction),
+ ("DπŸ‘‰", DictWordMetadata::is_determiner),
+ ("PπŸ“₯", |m| m.preposition),
+ ("IπŸ‘€", DictWordMetadata::is_pronoun),
+ ];
+
for word in words {
- let metadata = dictionary.get_word_metadata_str(&word);
- results.insert(word, metadata);
+ let meta = dictionary.get_word_metadata_str(&word);
+ let (flags, emojis) = meta.as_ref().map_or_else(
+ || (String::new(), String::new()),
+ |md| {
+ POS.iter()
+ .filter(|&(_, pred)| pred(md))
+ .map(|(syms, _)| {
+ let mut ch = syms.chars();
+ (ch.next().unwrap(), ch.next().unwrap())
+ })
+ .unzip()
+ },
+ );
+
+ let json = brief.then(String::new).unwrap_or_else(|| {
+ format!("\n{}", serde_json::to_string_pretty(&meta).unwrap())
+ });
+ println!("{}: {} {}{}", word, flags, emojis, json);
}
- let json = serde_json::to_string_pretty(&results).unwrap();
- println!("{json}");
Ok(())
}
Args::SummarizeLintRecord { file } => {
diff --git a/justfile b/justfile
index ff555167d..6b6c23ba4 100644
--- a/justfile
+++ b/justfile
@@ -378,6 +378,8 @@ userdictoverlap:
# Get the metadata associated with one or more words in Harper's dictionary as JSON.
getmetadata *words:
cargo run --bin harper-cli -- metadata {{words}}
+getmetadata-brief *words:
+ cargo run --bin harper-cli -- metadata --brief {{words}}
# Get all the forms of a word using the affixes.
getforms word:
cargo run --bin harper-cli -- forms {{word}}