Skip to content

Commit

Permalink
Update trane to version 0.22.0 (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmr authored Aug 11, 2024
1 parent 9987a78 commit 834e27b
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 46 deletions.
99 changes: 57 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "trane-cli"
version = "0.21.0"
version = "0.22.0"
build = "build.rs"
default-run = "trane"

Expand All @@ -19,11 +19,12 @@ built = { version = "0.7.4", features = ["chrono", "dependency-tree", "git2", "s
chrono = "0.4.38"
clap = { version = "4.5.9", features = ["derive"] }
indoc = "2.0.5"
rand = "0.8.5"
rustyline = "14.0.0"
rustyline-derive = "0.10.0"
serde_json = "1.0.120"
termimad = "0.29.4"
trane = "0.21.3"
trane = "0.22.0"
ustr = { version = "1.0.0", features = ["serde"] }
# Commented out for use in local development.
# trane = { path = "../trane" }
Expand Down
48 changes: 46 additions & 2 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//! Contains the logic to print Trane assets to the terminal.
use anyhow::{Context, Result};
use rand::prelude::SliceRandom;
use std::fs::read_to_string;
use termimad::print_inline;
use trane::data::{BasicAsset, ExerciseAsset, ExerciseManifest};
use trane::data::{
course_generator::literacy::LiteracyLesson, BasicAsset, ExerciseAsset, ExerciseManifest,
};

/// Prints the markdown file at the given path to the terminal.
pub fn print_markdown(path: &str) -> Result<()> {
Expand All @@ -14,6 +17,39 @@ pub fn print_markdown(path: &str) -> Result<()> {
Ok(())
}

/// Randomly samples five values from the given list of strings.
fn sample(values: &[String]) -> Vec<String> {
let mut sampled = values.to_vec();
let mut rng = rand::thread_rng();
sampled.shuffle(&mut rng);
sampled.truncate(5);
sampled
}

/// Prints a literacy asset to the terminal.
pub fn print_literacy(lesson_type: &LiteracyLesson, examples: &[String], exceptions: &[String]) {
let sampled_examples = sample(examples);
let sampled_exceptions = sample(exceptions);
match lesson_type {
LiteracyLesson::Reading => println!("Lesson type: Reading"),
LiteracyLesson::Dictation => println!("Lesson type: Dictation"),
}
if !sampled_examples.is_empty() {
println!("Examples:");
for example in sampled_examples {
print_inline(&example);
println!();
}
}
if !sampled_exceptions.is_empty() {
println!("Exceptions:");
for exception in sampled_exceptions {
print_inline(&exception);
println!();
}
}
}

/// Trait to display an asset to the terminal.
pub trait DisplayAsset {
/// Prints the asset to the terminal.
Expand Down Expand Up @@ -49,6 +85,14 @@ impl DisplayExercise for ExerciseAsset {
match self {
ExerciseAsset::BasicAsset(asset) => asset.display_asset(),
ExerciseAsset::FlashcardAsset { front_path, .. } => print_markdown(front_path),
ExerciseAsset::LiteracyAsset {
lesson_type,
examples,
exceptions,
} => {
print_literacy(lesson_type, examples, exceptions);
Ok(())
}
ExerciseAsset::SoundSliceAsset {
link, description, ..
} => {
Expand Down Expand Up @@ -108,7 +152,7 @@ impl DisplayAnswer for ExerciseAsset {
Ok(())
}
}
ExerciseAsset::SoundSliceAsset { .. } => Ok(()),
ExerciseAsset::SoundSliceAsset { .. } | ExerciseAsset::LiteracyAsset { .. } => Ok(()),
}
}
}
Expand Down

0 comments on commit 834e27b

Please sign in to comment.