mirror of
https://github.com/Dadams2/rick.git
synced 2026-06-27 09:49:03 +00:00
No description
- Rust 100%
| src | ||
| .gitignore | ||
| Cargo.lock | ||
| Cargo.toml | ||
| README.md | ||
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
-
State Management
StopwatchStateenum:Stopped,Running,PausedInstantfor precise time trackingDurationfor elapsed time calculationsVec<Duration>for lap time storage
-
Event Loop Pattern
loop { terminal.draw(|frame| render(&mut app, frame))?; if handle_events(&mut app)? { break; } app.update(); } -
UI Layout Structure
- Title bar with application name
- Main stopwatch display with state indicator
- Controls help section
- Scrollable lap times list
-
Time Formatting
- Format:
MM:SS.mmm(minutes:seconds.milliseconds) - Real-time updates during running state
- Consistent display across all time values
- Format:
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
- Widget Composition: Building complex UIs from simple widgets
- State Management: Proper separation of application state and UI
- Event Handling: Responsive keyboard input processing
- Layout System: Flexible constraint-based layouts
- Styling: Color schemes and text styling
- 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.