Skip to content

Commit

Permalink
fix: list built-in languages
Browse files Browse the repository at this point in the history
Fixes #54.
Arguably regresses #55,
since there's no longer a warning when the language dir doesn't exist.
  • Loading branch information
max-niederman committed Oct 10, 2023
1 parent 8a605fb commit 5767982
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
36 changes: 20 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ impl Opt {
std::io::stdin()
.lock()
.lines()
.filter_map(Result::ok)
.map_while(Result::ok)
.collect()
} else {
let file = fs::File::open(path).expect("Error reading language file.");
io::BufReader::new(file)
.lines()
.filter_map(Result::ok)
.map_while(Result::ok)
.collect()
};

Expand Down Expand Up @@ -134,13 +134,22 @@ impl Opt {
}

/// Installed languages under config directory
fn languages(&self) -> io::Result<Vec<OsString>> {
Ok(self
fn languages(&self) -> io::Result<impl Iterator<Item = OsString>> {
let builtin = Resources::iter().filter_map(|name| {
name.strip_prefix("language/")
.map(ToOwned::to_owned)
.map(OsString::from)
});

let configured = self
.language_dir()
.read_dir()?
.filter_map(Result::ok)
.map(|e| e.file_name())
.collect())
.read_dir()
.into_iter()
.flatten()
.map_while(Result::ok)
.map(|e| e.file_name());

Ok(builtin.chain(configured))
}

/// Config directory
Expand Down Expand Up @@ -195,15 +204,10 @@ fn main() -> crossterm::Result<()> {
}

if opt.list_languages {
match opt.languages() {
Ok(languages) => languages,
Err(_) => {
println!("Warning: Couldn't get installed languages under config directory. Make sure the config directory exists.");
vec![]
}
}
.iter()
opt.languages()
.unwrap()
.for_each(|name| println!("{}", name.to_str().expect("Ill-formatted language name.")));

return Ok(());
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl From<&Test> for Results {
Self {
timing: calc_timing(&events),
accuracy: calc_accuracy(&events),
missed_words: calc_missed_words(&test),
missed_words: calc_missed_words(test),
}
}
}
Expand Down

0 comments on commit 5767982

Please sign in to comment.