No description
Find a file
2025-07-16 16:48:42 +10:00
src cleaner design 2025-07-16 16:46:31 +10:00
.gitignore initial commit 2025-07-16 16:03:21 +10:00
Cargo.lock initial commit 2025-07-16 16:03:21 +10:00
Cargo.toml initial commit 2025-07-16 16:03:21 +10:00
README.md update name 2025-07-16 16:48:42 +10:00

Rust Tick

A simple terminal-based stopwatch application built with Ratatui, demonstrating modern terminal UI patterns in Rust.

Features

  • ⏱️ Precision Timing: Millisecond precision with real-time updates
  • ⏸️ Start/Pause/Stop: Full control over timing operations
  • 🏃 Lap Times: Record and display multiple lap times
  • 🎨 Rich UI: Clean, intuitive terminal interface with color coding
  • ⌨️ Keyboard Controls: Simple and efficient keyboard shortcuts

Implementation Overview

This stopwatch application showcases several key Ratatui patterns and Rust best practices:

Architecture

StopwatchApp
├── State Management (StopwatchState enum)
├── Time Tracking (std::time::Instant/Duration)
├── Event Handling (crossterm events)
└── UI Rendering (ratatui widgets)

Key Components

  1. State Management

    • StopwatchState enum: Stopped, Running, Paused
    • Instant for precise time tracking
    • Duration for elapsed time calculations
    • Vec<Duration> for lap time storage
  2. Event Loop Pattern

    loop {
        terminal.draw(|frame| render(&mut app, frame))?;
        if handle_events(&mut app)? { break; }
        app.update();
    }
    
  3. UI Layout Structure

    • Title bar with application name
    • Main stopwatch display with state indicator
    • Controls help section
    • Scrollable lap times list
  4. Time Formatting

    • Format: MM:SS.mmm (minutes:seconds.milliseconds)
    • Real-time updates during running state
    • Consistent display across all time values

Controls

Key Action
Space / Enter Start/Pause the stopwatch
R Reset (stop and clear)
L Record lap time
Q / Esc Quit application

Technical Implementation

Time Management

// High-precision timing using std::time
struct StopwatchApp {
    start_time: Option<Instant>,    // When timing started
    pause_time: Option<Duration>,   // Accumulated time when paused
    elapsed: Duration,              // Current elapsed time
    lap_times: Vec<Duration>,       // Recorded lap times
}

State Transitions

Stopped --[Start]--> Running --[Pause]--> Paused
   ^                    |                    |
   |                    |                    |
   +----[Reset]---------+        [Resume]---+

UI Components

  • Layout: Vertical split with constraints for each section
  • Styling: Color-coded state indicators (Red/Green/Yellow)
  • Widgets: Paragraph widgets with Block borders
  • Text: Styled spans for different information types

Event Handling

  • Non-blocking event polling with 16ms timeout (60 FPS)
  • Key press filtering for responsive input
  • State-aware command processing

Building and Running

# Build the application
cargo build

# Run the stopwatch
cargo run

# Quick compile check
cargo check

Dependencies

  • ratatui: Terminal UI framework
  • crossterm: Cross-platform terminal handling
  • std::time: High-precision timing

Ratatui Patterns Demonstrated

  1. Widget Composition: Building complex UIs from simple widgets
  2. State Management: Proper separation of application state and UI
  3. Event Handling: Responsive keyboard input processing
  4. Layout System: Flexible constraint-based layouts
  5. Styling: Color schemes and text styling
  6. Real-time Updates: Smooth animation with proper timing

This implementation serves as an excellent example of:

  • Modern Rust TUI development patterns
  • Clean separation of concerns
  • Efficient real-time updates
  • User-friendly interface design
  • Robust state management

The code is well-structured, properly documented, and follows Rust best practices while showcasing the power and flexibility of the Ratatui framework.