Expand description
§User Interface Module (ui.rs)
This module implements the terminal-based user interface for the Perspt chat application using the Ratatui TUI framework. It provides a rich, interactive chat experience with real-time markdown rendering, scrollable chat history, and comprehensive error handling.
§Features
- Rich Terminal UI: Modern terminal interface with colors, borders, and layouts
- Real-time Markdown Rendering: Live rendering of LLM responses with markdown formatting
- Advanced Scrollable Chat History: Intelligent scrolling with accurate text wrapping calculations
- Progress Indicators: Visual feedback for LLM response generation
- Error Display: Comprehensive error handling with user-friendly messages
- Help System: Built-in help overlay with keyboard shortcuts
- Responsive Layout: Adaptive layout that works across different terminal sizes
- Unicode Text Wrapping: Accurate character counting for proper terminal text wrapping
§Scroll System Improvements
The scroll system has been enhanced to handle long responses reliably:
- Accurate Text Wrapping: Uses
.chars().count()
for proper Unicode character counting - Consistent Calculations: Unified logic between
max_scroll()
andupdate_scroll_state()
- Content Visibility: Conservative buffer ensures no content is cut off at the bottom
- Separator Line Handling: Properly accounts for separator lines in scroll calculations
- Debug Logging: Comprehensive logging for scroll-related troubleshooting
§Architecture
The UI follows a component-based architecture:
App
- Main application state and controller with enhanced scroll managementChatMessage
- Individual message representation with stylingErrorState
- Error handling and display logic- Event handling system for keyboard inputs and timers
§Usage Example
use perspt::ui::{App, run_app};
use perspt::config::AppConfig;
let config = AppConfig::load().unwrap();
let mut app = App::new(config);
run_app(&mut app).await?;
Structs§
- App
- Main application state and controller.
- Chat
Message - Represents a single message in the chat interface.
- Error
State - Represents an error state with detailed information for user display.
- Event
Stream 🔒 - Enhanced event stream wrapper for non-blocking event handling
Enums§
- AppEvent
- Events that can occur in the application.
- Error
Type - Categories of errors that can occur in the application.
- Message
Type - Represents the type of message in the chat interface.
Constants§
Functions§
- categorize_
error 🔒 - Categorizes error messages into specific error types with helpful details
- draw_
enhanced_ 🔒chat_ area - Enhanced chat area with better scrolling and formatting
- draw_
enhanced_ 🔒header - Enhanced header with better visual hierarchy
- draw_
enhanced_ 🔒help_ overlay - Enhanced help overlay with better formatting
- draw_
enhanced_ 🔒input_ area - Enhanced input area with visible cursor and better feedback
- draw_
enhanced_ 🔒status_ line - Enhanced status line with better error handling
- draw_
enhanced_ 🔒ui - Enhanced UI rendering with cursor support and better performance
- handle_
llm_ 🔒response - Handle LLM responses with immediate UI updates
- handle_
terminal_ 🔒event - Application events for the enhanced UI loop (This duplicate enum has been removed - using the main AppEvent enum above) Handle terminal events with immediate response
- initiate_
llm_ 🔒request_ enhanced - Enhanced LLM request initiation with proper error handling
- markdown_
to_ 🔒lines - Renders the application header with model information and status.
- parse_
inline_ 🔒markdown - Parse inline markdown elements like bold, italic, and
code
- parse_
inline_ 🔒markdown_ to_ spans - Parse inline markdown elements and return styled spans
- run_ui
- Runs the enhanced UI event loop with real-time responsiveness.