Change formatting to be closer to what jisho presents.
This commit is contained in:
parent
9b2e51fee3
commit
556c32b1ec
1 changed files with 29 additions and 14 deletions
43
src/main.rs
43
src/main.rs
|
@ -150,6 +150,7 @@ fn main() -> Result<(), ureq::Error> {
|
||||||
|
|
||||||
fn print_item(query: &str, value: &Value, output: &mut String) -> Option<usize> {
|
fn print_item(query: &str, value: &Value, output: &mut String) -> Option<usize> {
|
||||||
let japanese = value_to_arr(value.get("japanese")?).get(0)?.to_owned();
|
let japanese = value_to_arr(value.get("japanese")?).get(0)?.to_owned();
|
||||||
|
let mut num_of_lines = 0;
|
||||||
|
|
||||||
let reading = japanese
|
let reading = japanese
|
||||||
.get("reading")
|
.get("reading")
|
||||||
|
@ -163,24 +164,31 @@ fn print_item(query: &str, value: &Value, output: &mut String) -> Option<usize>
|
||||||
|
|
||||||
// Print senses
|
// Print senses
|
||||||
let senses = value_to_arr(value.get("senses")?);
|
let senses = value_to_arr(value.get("senses")?);
|
||||||
|
let mut prev_parts_of_speech = String::new();
|
||||||
|
|
||||||
for (i, sense) in senses.iter().enumerate() {
|
for (i, sense) in senses.iter().enumerate() {
|
||||||
let sense_str = format_sense(&sense, i);
|
let (sense_str, bump) = format_sense(&sense, i, &mut prev_parts_of_speech);
|
||||||
if sense_str.is_empty() {
|
if sense_str.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
// This bump is to keep count of lines that may or may not be printed (like noun, adverb)
|
||||||
|
if bump {
|
||||||
|
num_of_lines += 1;
|
||||||
|
}
|
||||||
|
|
||||||
aux = format!(" {}\n", sense_str);
|
aux = format!(" {}\n", sense_str);
|
||||||
*output += &aux;
|
*output += &aux;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(senses.iter().count() + 1)
|
num_of_lines += senses.iter().count() + 1;
|
||||||
|
Some(num_of_lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_sense(value: &Value, index: usize) -> String {
|
fn format_sense(value: &Value, index: usize, prev_parts_of_speech: &mut String) -> (String, bool) {
|
||||||
let english_definitons = value.get("english_definitions");
|
let english_definitons = value.get("english_definitions");
|
||||||
let parts_of_speech = value.get("parts_of_speech");
|
let parts_of_speech = value.get("parts_of_speech");
|
||||||
if english_definitons.is_none() {
|
if english_definitons.is_none() {
|
||||||
return "".to_owned();
|
return ("".to_owned(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
let english_definiton = value_to_arr(english_definitons.unwrap());
|
let english_definiton = value_to_arr(english_definitons.unwrap());
|
||||||
|
@ -193,7 +201,6 @@ fn format_sense(value: &Value, index: usize) -> String {
|
||||||
let s = value_to_str(i);
|
let s = value_to_str(i);
|
||||||
match s {
|
match s {
|
||||||
"Suru verb - irregular" => "Irregular verb",
|
"Suru verb - irregular" => "Irregular verb",
|
||||||
"Ichidan verb" => "iru/eru verb",
|
|
||||||
_ => {
|
_ => {
|
||||||
if s.contains("Godan verb") {
|
if s.contains("Godan verb") {
|
||||||
"Godan verb"
|
"Godan verb"
|
||||||
|
@ -206,28 +213,36 @@ fn format_sense(value: &Value, index: usize) -> String {
|
||||||
.collect::<Vec<&str>>()
|
.collect::<Vec<&str>>()
|
||||||
.join(", ");
|
.join(", ");
|
||||||
|
|
||||||
if parts.is_empty() {
|
// Do not repeat a meaning's part of speech if it is the same as the previous meaning
|
||||||
String::new()
|
if !parts.is_empty() && parts != *prev_parts_of_speech {
|
||||||
|
*prev_parts_of_speech = parts.clone();
|
||||||
|
format!("[{}]\n ", parts.bright_blue())
|
||||||
} else {
|
} else {
|
||||||
format!("[{}]", parts.bright_blue())
|
String::new()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
String::new()
|
String::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let bump = if parts_of_speech.is_empty() {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
};
|
||||||
|
|
||||||
let tags = format_sense_tags(value);
|
let tags = format_sense_tags(value);
|
||||||
|
|
||||||
format!(
|
(format!(
|
||||||
"{}. {} {} {}",
|
"{}{}. {} {}",
|
||||||
index + 1,
|
parts_of_speech,
|
||||||
|
(index + 1).to_string().bright_black(),
|
||||||
english_definiton
|
english_definiton
|
||||||
.iter()
|
.iter()
|
||||||
.map(|i| value_to_str(i))
|
.map(|i| value_to_str(i))
|
||||||
.collect::<Vec<&str>>()
|
.collect::<Vec<&str>>()
|
||||||
.join(", "),
|
.join(", "),
|
||||||
tags,
|
tags,
|
||||||
parts_of_speech
|
), bump)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Format tags from a whole meaning
|
/// Format tags from a whole meaning
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue