Skip to content

Commit 7043a1e

Browse files
feat: improve installation process
Remove the need for an installation script, typy can now just be installed using cargo (as is idiomatic for Rust projects). Typy will now download the requested language package at run time, if it is not already present.
1 parent 6f4928d commit 7043a1e

File tree

8 files changed

+1134
-97
lines changed

8 files changed

+1134
-97
lines changed

Cargo.lock

Lines changed: 1088 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ lazy_static = "1.5.0"
1616
anyhow = "1.0.95"
1717
chrono = { version = "0.4", features = ["serde"] }
1818
comfy-table = "7.1.4"
19+
reqwest = { version = "0.12.12", features = ["blocking"] }

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ word and asks you to type it as fast as possible. The game tracks your typing sp
2727
different game modes, such as uppercase and punctuation, to help you improve your typing skills in different areas.
2828

2929
## Installation
30-
To install Typy on Linux, you can use the following command:
30+
To install Typy, you can use the [Cargo] package manager:
31+
32+
[Cargo]: https://doc.rust-lang.org/cargo/
3133

3234
```bash
33-
curl -sSL https://raw.githubusercontent.com/Pazl27/typy-cli/master/scripts/install.sh | bash
35+
cargo install --git "https://github.com/Pazl27/typy-cli.git" --tag "v0.8.0"
3436
```
3537

36-
This command downloads and runs the installation script from the Typy GitHub repository. The script will handle the installation process for you, ensuring that Typy is set up correctly on your system.
37-
3838
If you prefer to get the newest version and compile it yourself, follow these steps:
3939

4040
1. Clone the Typy repository:
@@ -144,5 +144,5 @@ If you want to provide a new language to the Typy repository, feel free to creat
144144
145145
## Uninstall
146146
```bash
147-
curl -sSL https://raw.githubusercontent.com/Pazl27/typy-cli/master/scripts/uninstall.sh | bash
147+
cargo uninstall typy-cli
148148
```

scripts/install.sh

Lines changed: 0 additions & 41 deletions
This file was deleted.

scripts/uninstall.sh

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/terminal/game.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ pub fn run(mode: Mode, theme: ThemeColors) -> Result<()> {
6767
let mut stdout = stdout();
6868

6969
let language = language::Language::new();
70-
let file_name = format!(".local/share/typy/{}.txt", language.lang);
71-
let mut game =
72-
Game::new(word_provider::get_words(&file_name).context("Failed to get words from file")?);
70+
let mut game = Game::new(
71+
word_provider::get_words(&language.lang).context("Failed to get words from file")?,
72+
);
7373

7474
mode.transform(&mut game.list);
7575

src/word_provider/finder.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
1-
use anyhow::Result;
1+
use anyhow::{bail, Context, Result};
22
use dirs::home_dir;
3+
use reqwest::blocking::get;
34
use std::{
4-
fs::File,
5+
fs::{create_dir_all, write, File},
56
io::{BufRead, BufReader},
67
path::PathBuf,
8+
sync::LazyLock,
79
};
810

911
use rand::seq::IndexedRandom;
1012

11-
pub fn find(lenght: i32, res: &str) -> Result<Vec<String>, std::io::Error> {
12-
let path = if res.contains("resources") {
13-
PathBuf::from(res)
13+
static WORDS_DIR: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
14+
if cfg!(test) {
15+
Some(PathBuf::from("./resources/"))
1416
} else {
15-
let mut home_path = home_dir().ok_or(std::io::Error::new(
16-
std::io::ErrorKind::NotFound,
17-
"Home directory not found",
18-
))?;
19-
home_path.push(res);
20-
home_path
17+
home_dir().map(|p| p.join(".local/share/typy"))
18+
}
19+
});
20+
const WORDS_URL: &str =
21+
"https://raw.githubusercontent.com/Pazl27/typy-cli/refs/heads/master/resources/";
22+
23+
pub fn find(language: &str, lenght: i32) -> Result<Vec<String>> {
24+
let Some(words_file) = WORDS_DIR
25+
.as_ref()
26+
.map(|p| p.join(format!("{language}.txt")))
27+
else {
28+
bail!("Unable to find home directory");
2129
};
2230

23-
let words = read_file(path.to_str().unwrap())?;
31+
// Download words file if not already present
32+
if !words_file.exists() {
33+
create_dir_all(words_file.parent().unwrap())?;
34+
let language_url = format!("{WORDS_URL}{language}.txt");
35+
let resp = get(&language_url)
36+
.context("Failed to download words file from ".to_owned() + &language_url)?;
37+
write(
38+
&words_file,
39+
resp.text()
40+
.context("Failed to extract text from words file download")?,
41+
)
42+
.with_context(|| format!("Failed to save words file to {words_file:#?}"))?;
43+
}
44+
45+
let words = read_file(words_file.to_str().unwrap())?;
2446
let mut word = random_word(&words);
2547

2648
let mut fitted_words = Vec::new();

src/word_provider/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use finder::find;
55

66
const LENGTH: i32 = 70;
77

8-
pub fn get_words(res: &str) -> Result<Vec<Vec<String>>> {
8+
pub fn get_words(language: &str) -> Result<Vec<Vec<String>>> {
99
let mut words = Vec::new();
1010
for _ in 0..3 {
11-
words.push(find(LENGTH, res)?);
11+
words.push(find(language, LENGTH)?);
1212
}
1313
Ok(words)
1414
}
@@ -19,7 +19,7 @@ mod word_provider_tests {
1919

2020
#[test]
2121
fn test_get_words() {
22-
let words = get_words("resources/english.txt");
22+
let words = get_words("english");
2323

2424
for word in &words.unwrap() {
2525
let mut length = 0;

0 commit comments

Comments
 (0)