diff options
| author | Miquel Sabaté Solà <mssola@mssola.com> | 2026-01-15 15:55:01 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <mssola@mssola.com> | 2026-01-15 16:12:10 +0100 |
| commit | c40851cc007e8d5f0c12a4b77d370f0f6e4263a2 (patch) | |
| tree | fc8f83a67b535f13931822464ea22c2feaac7fa3 /crates/cli/src/run.rs | |
| parent | 28e853e6b085c625af03be5211ded3f307c0fd96 (diff) | |
| download | mihi-c40851cc007e8d5f0c12a4b77d370f0f6e4263a2.tar.gz mihi-c40851cc007e8d5f0c12a4b77d370f0f6e4263a2.zip | |
Add adjectives on the inflection practice
For now we are limiting this only to be on the masculine gender, but
this could be changed in the future.
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
Diffstat (limited to 'crates/cli/src/run.rs')
| -rw-r--r-- | crates/cli/src/run.rs | 191 |
1 files changed, 107 insertions, 84 deletions
diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 06d4551..21a92c2 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -1,8 +1,8 @@ use inquire::{Confirm, Editor, Text}; -use mihi::{configuration, get_inflected_from}; +use mihi::{configuration, get_adjective_table, get_inflected_from}; use mihi::{ - get_noun_table, select_relevant_words, touch_exercise, update_success, Category, Exercise, - ExerciseKind, Word, + get_noun_table, select_relevant_words, touch_exercise, update_success, Category, + DeclensionTable, Exercise, ExerciseKind, Word, }; use std::env; use std::fs; @@ -103,6 +103,107 @@ fn same_answer(given: &String, expected: &String) -> bool { accepted_diff(given, expected) } +fn ask_for_table(word: &Word, table: &DeclensionTable, id: Option<&str>) -> bool { + let added = match id { + Some(s) => format!(" ({}) ", s), + None => " ".to_string(), + }; + let mut initial = format!("== {}{}==\n\n", word.enunciated, added); + let mut expected = format!("== {}{}==\n\n", word.enunciated, added); + + for idx in configuration().case_order.to_usizes() { + match idx { + 0 => { + initial.push_str("Nominative: \n"); + expected.push_str( + format!( + "Nominative: {}\n", + get_inflected_from(word, &table.nominative) + ) + .as_str(), + ); + } + 1 => { + initial.push_str("Vocative: \n"); + expected.push_str( + format!("Vocative: {}\n", get_inflected_from(word, &table.vocative)).as_str(), + ); + } + 2 => { + initial.push_str("Accusative: \n"); + expected.push_str( + format!( + "Accusative: {}\n", + get_inflected_from(word, &table.accusative) + ) + .as_str(), + ); + } + 3 => { + initial.push_str("Genitive: \n"); + expected.push_str( + format!("Genitive: {}\n", get_inflected_from(word, &table.genitive)).as_str(), + ); + } + 4 => { + initial.push_str("Dative: \n"); + expected.push_str( + format!("Dative: {}\n", get_inflected_from(word, &table.dative)).as_str(), + ); + } + 5 => { + initial.push_str("Ablative: \n"); + expected.push_str( + format!("Ablative: {}\n", get_inflected_from(word, &table.ablative)).as_str(), + ); + } + 6 => { + if word.locative { + initial.push_str("Locative: \n"); + expected.push_str( + format!("Locative: {}\n", get_inflected_from(word, &table.locative)) + .as_str(), + ); + } + } + _ => {} + } + } + + // Inflection time! + let Ok(solution) = Editor::new("Open the editor to inflect:") + .with_predefined_text(initial.as_str()) + .with_file_extension(".md") + .prompt() + else { + return false; + }; + + same_answer(&solution, &expected) +} + +fn good_noun_inflection(word: &Word) -> bool { + if let Ok(table) = get_noun_table(word) { + return ask_for_table(word, &table, None); + } + true +} + +fn good_adjective_inflection(word: &Word) -> bool { + if let Ok(tables) = get_adjective_table(word) { + ask_for_table(word, &tables[0], Some("in the masculine")); + } + true +} + +fn good_inflection(word: &Word) -> bool { + match word.category { + Category::Noun => good_noun_inflection(word), + Category::Adjective => good_adjective_inflection(word), + _ => todo!(), + } +} + fn run_inflect_words(words: &Vec<Word>, locale: &Locale) -> bool { for word in words { // If the translation cannot be found, skip this word. @@ -139,87 +240,9 @@ fn run_inflect_words(words: &Vec<Word>, locale: &Locale) -> bool { println!("\x1b[91m❌\x1b[0m\n"); } - let mut initial = format!("== {} ==\n\n", word.enunciated); - let mut expected = format!("== {} ==\n\n", word.enunciated); - - if let Ok(table) = get_noun_table(word) { - for idx in configuration().case_order.to_usizes() { - match idx { - 0 => { - initial.push_str("Nominative: \n"); - expected.push_str( - format!( - "Nominative: {}\n", - get_inflected_from(word, &table.nominative) - ) - .as_str(), - ); - } - 1 => { - initial.push_str("Vocative: \n"); - expected.push_str( - format!("Vocative: {}\n", get_inflected_from(word, &table.vocative)) - .as_str(), - ); - } - 2 => { - initial.push_str("Accusative: \n"); - expected.push_str( - format!( - "Accusative: {}\n", - get_inflected_from(word, &table.accusative) - ) - .as_str(), - ); - } - 3 => { - initial.push_str("Genitive: \n"); - expected.push_str( - format!("Genitive: {}\n", get_inflected_from(word, &table.genitive)) - .as_str(), - ); - } - 4 => { - initial.push_str("Dative: \n"); - expected.push_str( - format!("Dative: {}\n", get_inflected_from(word, &table.dative)) - .as_str(), - ); - } - 5 => { - initial.push_str("Ablative: \n"); - expected.push_str( - format!("Ablative: {}\n", get_inflected_from(word, &table.ablative)) - .as_str(), - ); - } - 6 => { - if word.locative { - initial.push_str("Locative: \n"); - expected.push_str( - format!( - "Locative: {}\n", - get_inflected_from(word, &table.locative) - ) - .as_str(), - ); - } - } - _ => {} - } - } - } - - // Inflection time! - let Ok(solution) = Editor::new("Open the editor to inflect:") - .with_predefined_text(initial.as_str()) - .with_file_extension(".md") - .prompt() - else { - return false; - }; - - if same_answer(&solution, &expected) { + // Now ask for inflecting the given word in various ways depending on + // the word category. + if good_inflection(word) { if word.steps as usize == MAX_STEPS - 1 { let _ = update_success(word, word.succeeded + 1, 0); } else { |
