Coding TypeGym: A Rust Terminal Typing Trainer | Part 4: Text Sources

Published on

In Part 4 we give TypeGym something interesting to type.

Up until now the app was functional, but the practice text was basically hard-coded. This episode is all about building a small text pipeline so each session can pull content from different places (without touching the UI or typing logic).


What We Covered


Design Insights

Treat “text” as an input system

Typing logic and UI shouldn’t care where the text came from.

By making “text generation/loading” an isolated module (and routing it through Config), we can keep the rest of the app stable while we iterate on content:

All without ripping up the main loop.

“Nonsense text” is surprisingly useful

Random words are a great default because:

And for a typing trainer series, it’s perfect: we can keep building features without needing to constantly curate practice texts.


Implementation Highlights

1) TextSource enum

We add a small enum that represents how we fetch practice text:

#[derive(Debug)]
pub enum TextSource {
    Static,
    GenerateNonsense,
    GenerateWeightedNonsense,
    File(String),
    // TODO: MarkovChain(String),
}

This is the key abstraction for the episode: everything else becomes “just implementation details”.

2) Central get_text(config) dispatche

get_text() now does the obvious thing: match on config.text_source and return the requested content.

pub fn get_text(config: &Config) -> Result<String> {
    match config.text_source {
        TextSource::Static => Ok(TEXT.to_string()),
        TextSource::GenerateNonsense => generate_nonsense(),
        TextSource::GenerateWeightedNonsense => generate_weighted_nonsense(),
        TextSource::File(ref path) => read_lines_from_file(path),
    }
}

3) Random nonsense from data/words.txt

We read a list of words, pick a bunch at random, and format them into lines:

This gives a predictable layout while still being random every session.

4) Weighted nonsense from data/words_weighted.txt

Same idea, but with a “word + weight” file:

The result feels more “natural” because common words appear more often. Although it is still a complete nonsense.

5) File-based text: pick a random window of lines

For file mode we keep it intentionally simple:

It’s not paragraph-aware yet, but it’s a great stepping stone.


What’s Next?

Now that TypeGym can pull practice text from multiple sources, the next improvements become much easier:


Project Code

You will find the complete source code here: typegym