diff --git a/Cargo.lock b/Cargo.lock index 4e400c8..80b3fb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -767,7 +767,7 @@ dependencies = [ [[package]] name = "typy-cli" -version = "0.1.0" +version = "0.7.0" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 3b15b86..bd1b397 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "typy-cli" -version = "0.1.0" +version = "0.7.0" edition = "2021" [dependencies] diff --git a/README.md b/README.md index fc48cbe..94017c6 100644 --- a/README.md +++ b/README.md @@ -53,10 +53,10 @@ If you prefer to get the newest version and compile it yourself, follow these st sudo mv target/release/typy /usr/local/bin/ ``` -4. Ensure the `words.txt` file is in the correct location: +4. Ensure the `english.txt` file is in the correct location: ```bash mkdir -p ~/.local/share/typy - cp resources/words.txt ~/.local/share/typy/ + cp resources/english.txt ~/.local/share/typy/ ``` ## Flags @@ -105,6 +105,9 @@ style = "SteadyBar" # possible options are: DefaultUserShape, BlinkingBlock, Ste default_mode = "normal" # possible modes are "normal"|"uppercase"|"punctuation", combinations of modes is also possible e.g: "uppercase, punctuation" uppercase_chance = "3" # possible are values between 0 and 1, if value is too high it gets clamped to 1, if too low it gets clamped to 0 punctuation_chance = "0.5" # possible are values between 0 and 1, if value is too high it gets clamped to 1, if too low it gets clamped to 0 + +[language] +lang = "english" # select your desired language ``` To apply the configuration, you can either edit the `config.toml` file directly or use the `typy -c` command to to open the file in your preferred editor: @@ -127,6 +130,18 @@ This will display the stats of the last 10 games and looks something like this: ![Stats](./docs/assets/snapshot_2025-02-24_00-28-16.png) To close this view press `Ctrl + c` or `esc`. +## Language +The language files are located at `~/.local/share/typy/`. The default language is `english`. You can change the language by editing the `config.toml` file or by using the +`typy -c` command. If you want to add a new language you can create a new file in the `~/.local/share/typy/` directory and add the words in the following format: +```txt +word1 +word2 +... +``` +The language file should be named after the language you want to add. For example, if you want to add a German language file, you would create a file named `german.txt` and add the German words to it. +If you want to use the new language you need to change the `lang` field in the `config.toml` file to the name of the language file without the `.txt` extension. +If you want to provide a new language to the Typy repository, feel free to create a pull request. Atm I only have the `english.txt` file in the repository. + ## Uninstall ```bash curl -sSL https://raw.githubusercontent.com/Pazl27/typy-cli/master/scripts/uninstall.sh | bash diff --git a/resources/words.txt b/resources/english.txt similarity index 100% rename from resources/words.txt rename to resources/english.txt diff --git a/scripts/install.sh b/scripts/install.sh index b6b9507..8d44767 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -3,7 +3,7 @@ # Define paths BIN_NAME="typy" LOCAL_DIR="$HOME/.local/share" -CONFIG_PATH="$LOCAL_DIR/$BIN_NAME/words.txt" +CONFIG_PATH="$LOCAL_DIR/$BIN_NAME/english.txt" INSTALL_DIR="$HOME/your-repo" GIT_TAG="v0.7.0" @@ -36,6 +36,6 @@ move_binary # Move any required files to the ~/.local folder (e.g., configuration files) echo "Setting up configuration files..." -curl -L https://github.com/Pazl27/typy-cli/releases/download/$GIT_TAG/words.txt -o "$CONFIG_PATH" +curl -L https://github.com/Pazl27/typy-cli/releases/download/$GIT_TAG/english.txt -o "$CONFIG_PATH" echo "Installation complete! You can now run the CLI tool by typing '$BIN_NAME' in your terminal." diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh index d51e512..f8189b3 100755 --- a/scripts/uninstall.sh +++ b/scripts/uninstall.sh @@ -4,7 +4,7 @@ BIN_NAME="typy" LOCAL_DIR="$HOME/.local/share" BIN_PATH="/usr/bin/$BIN_NAME" -CONFIG_PATH="$LOCAL_DIR/$BIN_NAME/words.txt" +CONFIG_PATH="$LOCAL_DIR/$BIN_NAME/english.txt" CONFIG_DIR="$LOCAL_DIR/$BIN_NAME" # Function to remove the binary with appropriate privileges diff --git a/src/config/config_tables/language.rs b/src/config/config_tables/language.rs new file mode 100644 index 0000000..8c6fdd1 --- /dev/null +++ b/src/config/config_tables/language.rs @@ -0,0 +1,27 @@ +use crate::config::toml_parser::get_config; + +pub struct Language { + pub lang: String, +} + +impl Language { + pub fn new() -> Self { + let theme_colors: Language = match get_config().lock().unwrap().get_language() { + Some(language) => { + let lang = language.lang.unwrap_or("english".to_string()); + + Language { lang } + } + None => Language::default(), + }; + theme_colors + } +} + +impl Default for Language { + fn default() -> Self { + Language { + lang: "english".to_string(), + } + } +} diff --git a/src/config/config_tables/mod.rs b/src/config/config_tables/mod.rs index 71e4848..ca249f6 100644 --- a/src/config/config_tables/mod.rs +++ b/src/config/config_tables/mod.rs @@ -2,3 +2,4 @@ pub mod cursor_style; pub mod graph_colors; pub mod mode_settings; pub mod theme; +pub mod language; diff --git a/src/config/toml_parser.rs b/src/config/toml_parser.rs index 65677c0..4194482 100644 --- a/src/config/toml_parser.rs +++ b/src/config/toml_parser.rs @@ -37,12 +37,19 @@ pub struct ModesTable { pub punctuation_chance: Option, } +#[derive(Serialize, Deserialize)] +#[derive(Clone)] +pub struct LanguageTable { + pub lang: Option +} + #[derive(Serialize, Deserialize)] pub struct ConfigToml { theme: Option, graph: Option, cursor: Option, modes: Option, + language: Option, } impl ConfigToml { @@ -83,6 +90,10 @@ impl ConfigToml { pub fn get_modes(&self) -> Option { self.modes.clone() } + + pub fn get_language(&self) -> Option { + self.language.clone() + } } impl Default for ConfigToml { @@ -92,6 +103,7 @@ impl Default for ConfigToml { graph: None, cursor: None, modes: None, + language: None, } } } diff --git a/src/terminal/game.rs b/src/terminal/game.rs index 61c0741..30a6261 100644 --- a/src/terminal/game.rs +++ b/src/terminal/game.rs @@ -1,3 +1,4 @@ +use super::keyboard::{handle_input, InputAction}; use anyhow::{Context, Result}; use crossterm::cursor::{self, SetCursorStyle}; use crossterm::event::poll; @@ -8,7 +9,6 @@ use crossterm::{ terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType}, ExecutableCommand, }; -use super::keyboard::{handle_input, InputAction}; use std::io::stdout; use std::io::Write; use std::sync::atomic::{AtomicBool, Ordering}; @@ -17,6 +17,7 @@ use std::thread; use std::time::{Duration, Instant}; use crate::config::cursor_style::CursorKind; +use crate::config::language; use crate::config::theme::ThemeColors; use crate::mode::Mode; use crate::scores::finish_overview; @@ -65,10 +66,10 @@ impl Game { pub fn run(mode: Mode, theme: ThemeColors) -> Result<()> { let mut stdout = stdout(); - let mut game = Game::new( - word_provider::get_words(".local/share/typy/words.txt") - .context("Failed to get words from file")?, - ); + let language = language::Language::new(); + let file_name = format!(".local/share/typy/{}.txt", language.lang); + let mut game = + Game::new(word_provider::get_words(&file_name).context("Failed to get words from file")?); mode.transform(&mut game.list); @@ -159,7 +160,6 @@ pub fn run(mode: Mode, theme: ThemeColors) -> Result<()> { } if !game.quit { - stdout.execute(cursor::Hide)?; let score = Score::new( stats.wpm() as u32, @@ -243,4 +243,3 @@ fn start_timer( Ok(()) } - diff --git a/src/word_provider/finder.rs b/src/word_provider/finder.rs index bed5611..192dd0e 100644 --- a/src/word_provider/finder.rs +++ b/src/word_provider/finder.rs @@ -68,7 +68,7 @@ mod finder_tests { #[test] fn test_read_file() { - let words = read_file("./resources/words.txt").unwrap(); + let words = read_file("./resources/english.txt").unwrap(); assert_eq!(words.len(), 7776); } diff --git a/src/word_provider/mod.rs b/src/word_provider/mod.rs index a72927f..1c3fe63 100644 --- a/src/word_provider/mod.rs +++ b/src/word_provider/mod.rs @@ -20,7 +20,7 @@ mod word_provider_tests { #[test] fn test_get_words() { - let words = get_words("resources/words.txt"); + let words = get_words("resources/english.txt"); for word in &words.unwrap() { let mut length = 0;