gladius

Crates.iogladius
lib.rsgladius
version1.0.0
created_at2025-09-18 21:25:25.932284+00
updated_at2025-11-03 22:43:08.595236+00
descriptionA library for writing typing-trainers
homepage
repositoryhttps://github.com/mahlquistj/octotype
max_upload_size
id1845418
size205,196
Mads Ahlquist Jensen (mahlquistj)

documentation

README

Gladius - High-Performance Typing Trainer Library

Docs Badge

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.

๐Ÿš€ Quick Start

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));

๐Ÿ’ก Key Features

๐Ÿƒโ€โ™‚๏ธ High Performance

  • Fast character processing - Amortized O(1) keystroke handling
  • O(1) word lookups - Efficient character-to-word mapping
  • Optimized statistics - Welford's algorithm for numerical stability
  • Memory efficient - Minimal allocations during typing

๐Ÿ“Š Comprehensive Statistics

  • Words per minute (raw, corrected, actual)
  • Input per minute (raw, actual)
  • Accuracy percentages (raw, actual)
  • Consistency analysis with standard deviation
  • Detailed error tracking by character and word
  • Real-time measurements at configurable intervals

๐ŸŽฏ Flexible Rendering

  • Character-level rendering with typing state information
  • Line-based rendering with intelligent word wrapping
  • Cursor position tracking across line boundaries
  • Unicode support for international characters and emojis
  • Generic renderer interface for any UI framework

โš™๏ธ Configurable Behavior

  • Measurement intervals for statistics collection
  • Line wrapping options (word boundaries vs. character wrapping)
  • Newline handling (respect or ignore paragraph breaks)
  • Performance tuning for different use cases

๐Ÿ”— Relationship to OctoType

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:

  • Text processing and character management
  • Real-time typing statistics calculation
  • Input validation and error tracking
  • Rendering pipeline for display
  • Performance metrics and analysis

This separation allows:

  • Reusability: Other applications can use Gladius as a typing engine
  • Testing: Core typing logic can be thoroughly tested independently
  • Maintainability: Clear separation of concerns between UI and logic
  • Performance: Optimized core without UI overhead

๐Ÿ“ฆ Installation

Add Gladius to your Cargo.toml:

[dependencies]
gladius = "0.3.2"

๐Ÿ“š Documentation

Complete API documentation is available at docs.rs/gladius.

๐Ÿงช Examples

Basic Typing Session

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"),
}

Custom Configuration

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);

Character-level Rendering

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)
});

โšก Performance Characteristics

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

๐Ÿ›ก๏ธ Thread Safety

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.

๐Ÿ”ง Minimum Supported Rust Version (MSRV)

Gladius supports Rust 1.88.0 and later.

๐Ÿค Contributing

If you have an idea, bug-report or alike, feel free to open an issue or PR - It's more than welcome!

๐Ÿ“„ License

Licensed under the MIT License. See LICENSE for details.

Why "Gladius"?

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.

Commit count: 138

cargo fmt