Module ui

Source
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() and update_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 management
  • ChatMessage - Individual message representation with styling
  • ErrorState - 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.
ChatMessage
Represents a single message in the chat interface.
ErrorState
Represents an error state with detailed information for user display.
EventStream 🔒
Enhanced event stream wrapper for non-blocking event handling

Enums§

AppEvent
Events that can occur in the application.
ErrorType
Categories of errors that can occur in the application.
MessageType
Represents the type of message in the chat interface.

Constants§

MAX_STREAMING_BUFFER_SIZE 🔒
SMALL_BUFFER_THRESHOLD 🔒
UI_UPDATE_INTERVAL 🔒

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.