| Crates.io | gladius |
| lib.rs | gladius |
| version | 1.0.0 |
| created_at | 2025-09-18 21:25:25.932284+00 |
| updated_at | 2025-11-03 22:43:08.595236+00 |
| description | A library for writing typing-trainers |
| homepage | |
| repository | https://github.com/mahlquistj/octotype |
| max_upload_size | |
| id | 1845418 |
| size | 205,196 |
Gladius is a comprehensive Rust library for building typing trainer applications. It provides real-time typing analysis, flexible rendering systems, and detailed performance statistics with a focus on accuracy, performance, and ease of use.
use gladius::TypingSession;
// Create a typing session
let mut session = TypingSession::new("Hello, world!").unwrap();
// Process user input
while let Some((char, result)) = session.input(Some('H')) {
println!("Typed '{}': {:?}", char, result);
break; // Just for demo
}
// Get progress and statistics
println!("Progress: {:.1}%", session.completion_percentage());
println!("WPM: {:.1}", session.statistics().measurements.last()
.map(|m| m.wpm.raw).unwrap_or(0.0));
Gladius serves as the core engine for OctoType, a TUI typing trainer. While OctoType provides the user interface, configuration system, and TUI experience, Gladius handles all the fundamental typing logic:
This separation allows:
Add Gladius to your Cargo.toml:
[dependencies]
gladius = "0.3.2"
Complete API documentation is available at docs.rs/gladius.
use gladius::{TypingSession, CharacterResult};
let mut session = TypingSession::new("The quick brown fox").unwrap();
// Process typing input
match session.input(Some('T')) {
Some((ch, CharacterResult::Correct)) => println!("Correct: {}", ch),
Some((ch, CharacterResult::Wrong)) => println!("Wrong: {}", ch),
Some((ch, CharacterResult::Corrected)) => println!("Corrected: {}", ch),
Some((ch, CharacterResult::Deleted(state))) => println!("Deleted: {} (was {:?})", ch, state),
None => println!("No input processed"),
}
use gladius::{TypingSession, config::Configuration};
let config = Configuration {
measurement_interval_seconds: 0.5, // More frequent measurements
};
let session = TypingSession::new("Hello, world!")
.unwrap()
.with_configuration(config);
use gladius::TypingSession;
let session = TypingSession::new("hello").unwrap();
let rendered: Vec<String> = session.render(|ctx| {
let cursor = if ctx.has_cursor { " |" } else { "" };
let state = match ctx.character.state {
gladius::State::Correct => "โ",
gladius::State::Wrong => "โ",
gladius::State::None => "ยท",
_ => "?",
};
format!("{}{}{}", ctx.character.char, state, cursor)
});
| Operation | Time Complexity | Notes |
|---|---|---|
| Character input | O(1) amortized, O(w) worst case | Usually constant, worst case when recalculating word state |
| Character lookup | O(1) | Direct vector indexing |
| Word lookup | O(1) | Pre-computed mapping |
| Statistics update | O(1) typical, O(m) when measuring | Most updates are constant, measurements scan history |
| Rendering | O(n) | Linear in text length |
Gladius types are not thread-safe by design for maximum performance. Each typing session should be used on a single thread. Multiple sessions can run concurrently on different threads.
Gladius supports Rust 1.88.0 and later.
If you have an idea, bug-report or alike, feel free to open an issue or PR - It's more than welcome!
Licensed under the MIT License. See LICENSE for details.
Gladius is the Latin word for a small sword, but in biology, it's the name for the internal, feather-shaped shell of a squid.
Since gladius is the core library of OctoType, this name felt very fitting.