Skip to content

Commit bf3f561

Browse files
committed
Fixed problem with dictionary loading
1 parent 9cd36ba commit bf3f561

File tree

5 files changed

+158
-20
lines changed

5 files changed

+158
-20
lines changed

Cargo.lock

Lines changed: 135 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tukai"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2021"
55
authors = ["hlsxx"]
66
description = "The app provides an interactive typing experience with switchable templates, designed to help users improve their typing speed and accuracy."
@@ -15,6 +15,7 @@ futures = "0.3.31"
1515
maplit = "1.0.2"
1616
rand = "0.8.5"
1717
ratatui = "0.29.0"
18+
rust-embed = "8.5.0"
1819
serde = { version = "1.0.217", features = ["derive"] }
1920
tokio = { version = "1.43.0", features = ["full"] }
2021

File renamed without changes.
File renamed without changes.

src/config.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use ratatui::style::Style;
2+
use rust_embed::RustEmbed;
23
use std::cell::{Ref, RefCell, RefMut};
34

45
use serde::{Deserialize, Serialize};
56

67
use std::{collections::HashMap, fmt::Display, hash::Hash};
7-
use std::{
8-
fs::{self, File},
9-
io::{BufRead, BufReader},
10-
path::{Path, PathBuf},
11-
};
8+
use std::path::{Path, PathBuf};
129

1310
use maplit::hashmap;
1411
use ratatui::style::Color;
@@ -257,9 +254,13 @@ impl TukaiLayout {
257254
}
258255
}
259256

257+
#[derive(RustEmbed)]
258+
#[folder = "dictionary/"]
259+
struct LanguageDictionary;
260+
260261
pub struct Language {
261262
// Language files paths from the `words` folder
262-
language_files: Vec<PathBuf>,
263+
language_files: Vec<String>,
263264

264265
// Current used language index
265266
current_index: usize,
@@ -318,14 +319,18 @@ impl Language {
318319
/// Returns the paths of all available language files in the `words` folder.
319320
///
320321
/// So i.e. available languages
321-
pub fn load_language_files(&self) -> Result<Vec<PathBuf>, Box<dyn std::error::Error>> {
322-
let entries = fs::read_dir("words")?;
322+
pub fn load_language_files(&self) -> Result<Vec<String>, Box<dyn std::error::Error>> {
323+
let languages = LanguageDictionary::iter()
324+
.map(|file| file.to_string())
325+
.collect::<Vec<String>>();
323326

324-
let languages = entries
325-
.filter_map(|entry| entry.ok())
326-
.filter(|entry| entry.path().is_file())
327-
.map(|entry| entry.path())
328-
.collect::<Vec<PathBuf>>();
327+
// Maybe in feature manual load custom languages
328+
//
329+
// let languages = entries
330+
// .filter_map(|entry| entry.ok())
331+
// .filter(|entry| entry.path().is_file())
332+
// .map(|entry| entry.path())
333+
// .collect::<Vec<PathBuf>>();
329334

330335
Ok(languages)
331336
}
@@ -339,18 +344,16 @@ impl Language {
339344
.get(self.current_index)
340345
.ok_or("Not found a language dictionary file")?;
341346

342-
let file = File::open(&language_file_path)?;
343-
let buffer = BufReader::new(file);
347+
let file = LanguageDictionary::get(language_file_path).unwrap();
344348

345-
let words = buffer
349+
let words = std::str::from_utf8(&file.data)?
346350
.lines()
347-
.filter_map(|line| line.ok())
348351
.flat_map(|line| {
349352
line
350353
.split_whitespace()
351354
.map(String::from)
352355
.collect::<Vec<String>>()
353-
}) // Split into words
356+
})
354357
.collect::<Vec<String>>();
355358

356359
Ok(words)

0 commit comments

Comments
 (0)