TUI Engine & Management
Relevant source files
The following files were used as context for generating this wiki page:
LogUtil implements a robust Terminal User Interface (TUI) engine based on the ratatui and crossterm libraries. This system manages the application lifecycle, including an interactive file selector, a configuration settings interface, and a multi-tabbed dashboard for real-time log analysis. The engine is designed to handle user input, background data updates from Nginx logs, and stateful rendering of complex UI components.
The management layer is divided into three primary phases: the interactive initialization phase (File & Settings selection), the main application loop (Tab & State management), and the background processing monitoring.
Sources: src/main.rs, src/app.rs:1-25, README.md:40-100
Architecture Overview
The TUI engine follows a centralized management pattern where main.rs initializes the terminal environment and delegates control to either FileSettings or the App struct. The system utilizes an "Alternate Screen" to provide a full-screen experience and raw mode to capture keyboard and mouse events directly.
Component Interaction Flow
The following diagram illustrates the transition from the interactive startup mode to the main analysis dashboard.
Interactive Initialization Mode
When the application starts without a specific file path, it enters an interactive mode managed by the FileSettings struct. This mode provides a dual-panel interface for file system navigation and parameter configuration.
File Selector and Settings Management
The FileSettings struct tracks the state of two distinct panels. The left panel allows directory traversal, while the right panel allows users to toggle analysis features (Security, Performance, Bots) and configure regex patterns.
| Component | Functionality | Source |
|---|---|---|
| File Selector | Directory navigation with icons, size formatting, and modified timestamps. | src/file_settings.rs:163-245 |
| Settings Panel | Configuration of Count, Regex, Date Format, and feature flags. | src/file_settings.rs:303-395 |
| Mouse Support | Enables click selection and scroll navigation in both panels. | src/file_settings.rs:420-560 |
Input Handling in Settings
The system differentiates between "Navigation Mode" (moving focus) and "Input Mode" (typing text into settings).
Sources: src/file_settings.rs:562-730
Main Application Engine (App & Tabs)
Once analysis begins, the App struct takes over management. It initializes a TabManager and a collection of stateful tabs based on the provided AppConfig.
Tab Lifecycle and Rendering
The App::draw function establishes the primary layout, consisting of a Header (Tabs), a Summary bar, and a main content area.
pub(crate) fn draw(&mut self, frame: &mut Frame) {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints([Constraint::Length(3), Constraint::Min(0)].as_ref())
.split(frame.area());
// ... Render Header and Summary ...
let idx = self.tab_manager.current_tab();
if let Some(tab) = self.tabs.get_mut(idx) {
tab.draw(frame, chunks[1]);
}
}
Sources: src/app.rs:134-185
Global State Management
The App struct caches summary statistics (Total Requests, Unique IPs, URLs) to ensure high-performance rendering (refreshing only once per second) even when the underlying GLOBAL_DB is updated rapidly by the background thread.
| Metric | Source Logic | Style |
|---|---|---|
| Requests | db.get_stats().total_records | Yellow / Bold |
| Unique IPs | db.get_stats().unique_ips | Cyan |
| Unique URLs | db.get_stats().unique_urls | Pink |
Sources: src/app.rs:188-212, src/app.rs:151-165
TUI Utility Components
The TuiManager provides reusable UI widgets and helper functions to maintain visual consistency across the application.
Modal and Progress Management
- Modals: Rendered as centered popups using
Clearto overlap existing content. They are used for success notifications (e.g., "Copied to clipboard") or warnings. - Progress Bar: A
Gaugewidget used during initial file parsing to show loading status.
Sources: src/tui_manager.rs:46-105
Visual Styles
The engine defines several constant styles used throughout the interface:
HEADER_STYLE: Light Green, Bold.SELECTED_ITEM_STYLE: White text on Dark Blue background.TEXT_FG_COLOR: Light Gray (Rgb 158, 158, 158).
Sources: src/tui_manager.rs:36-44
Summary & Integration
The TUI Engine & Management system provides a seamless transition from a user-friendly configuration interface to a sophisticated real-time monitoring dashboard. By utilizing a trait-based tab system (Box<dyn Tab>), the engine can dynamically include or exclude heavy features like Security analysis or Heatmaps based on user configuration, ensuring optimal performance on various terminal emulators.
Sources: src/app.rs:43-75, src/main.rs:275-300