diff --git a/src/tui/error_buffer.rs b/src/tui/error_buffer.rs new file mode 100644 index 0000000..94aabc7 --- /dev/null +++ b/src/tui/error_buffer.rs @@ -0,0 +1,61 @@ +use std::collections::VecDeque; +use std::sync::{Arc, Mutex}; +use std::time::Instant; + +use once_cell::sync::Lazy; +use tracing::Level; + +const MAX_ERROR_EVENTS: usize = 50; + +#[derive(Debug, Clone)] +pub struct ErrorEvent { + pub level: Level, + pub message: String, + pub pushed_at: Instant, +} + +pub static ERROR_EVENTS: Lazy>>> = + Lazy::new(|| Arc::new(Mutex::new(VecDeque::new()))); + +pub fn push_error(event: ErrorEvent) { + match ERROR_EVENTS.lock() { + Ok(mut events) => { + events.push_back(event); + while events.len() > MAX_ERROR_EVENTS { + events.pop_front(); + } + } + Err(e) => { + tracing::error!("Error buffer lock poisoned: {}", e); + } + } +} + +pub fn has_errors() -> bool { + match ERROR_EVENTS.lock() { + Ok(events) => !events.is_empty(), + Err(_) => false, + } +} + +pub fn pop_error() -> Option { + match ERROR_EVENTS.lock() { + Ok(mut events) => events.pop_front(), + Err(_) => None, + } +} + +pub fn peek_error() -> Option { + match ERROR_EVENTS.lock() { + Ok(events) => events.front().cloned(), + Err(_) => None, + } +} + +/// Returns all queued error events, newest first (for top-to-bottom stacking). +pub fn peek_all_errors() -> Vec { + match ERROR_EVENTS.lock() { + Ok(events) => events.iter().rev().cloned().collect(), + Err(_) => Vec::new(), + } +} diff --git a/src/tui/log_buffer.rs b/src/tui/log_buffer.rs new file mode 100644 index 0000000..8fd108c --- /dev/null +++ b/src/tui/log_buffer.rs @@ -0,0 +1,38 @@ +use std::collections::VecDeque; +use std::sync::{Arc, Mutex}; + +use once_cell::sync::Lazy; +use tracing::Level; + +const MAX_LOG_ENTRIES: usize = 200; + +#[derive(Debug, Clone)] +pub struct LogEntry { + pub level: Level, + pub message: String, +} + +pub static LOG_BUFFER: Lazy>>> = + Lazy::new(|| Arc::new(Mutex::new(VecDeque::new()))); + +pub fn push_log(entry: LogEntry) { + match LOG_BUFFER.lock() { + Ok(mut buf) => { + buf.push_back(entry); + while buf.len() > MAX_LOG_ENTRIES { + buf.pop_front(); + } + } + Err(e) => { + tracing::error!("Log buffer lock poisoned: {}", e); + } + } +} + +/// Returns all log entries newest-first. +pub fn get_logs() -> Vec { + match LOG_BUFFER.lock() { + Ok(buf) => buf.iter().rev().cloned().collect(), + Err(_) => Vec::new(), + } +} diff --git a/src/tui/progress.rs b/src/tui/progress.rs new file mode 100644 index 0000000..ca39195 --- /dev/null +++ b/src/tui/progress.rs @@ -0,0 +1,71 @@ +use once_cell::sync::Lazy; +use std::sync::{Arc, Mutex}; + +#[derive(Debug, Default, Clone)] +pub struct ProgressState { + /// Current main action text, e.g. "Downloading 1.20.1..." + pub current_action: Option, + /// (bytes_downloaded, total_bytes). total = 0 means unknown size. + pub progress: Option<(u64, u64)>, + /// Detail text shown below main action, e.g. "libraries/net/minecraft/client.jar" + pub sub_action: Option, +} + +pub static PROGRESS: Lazy>> = + Lazy::new(|| Arc::new(Mutex::new(ProgressState::default()))); + +pub fn set_action(text: impl Into) { + let text = text.into(); + match PROGRESS.lock() { + Ok(mut state) => { + state.current_action = Some(text.clone()); + } + Err(e) => { + tracing::error!("Progress lock poisoned: {}", e); + } + } + crate::tui::log_buffer::push_log(crate::tui::log_buffer::LogEntry { + level: tracing::Level::INFO, + message: text, + }); +} + +pub fn set_progress(current: u64, total: u64) { + match PROGRESS.lock() { + Ok(mut state) => { + state.progress = Some((current, total)); + } + Err(e) => { + tracing::error!("Progress lock poisoned: {}", e); + } + } +} + +pub fn set_sub_action(text: impl Into) { + let text = text.into(); + match PROGRESS.lock() { + Ok(mut state) => { + state.sub_action = Some(text.clone()); + } + Err(e) => { + tracing::error!("Progress lock poisoned: {}", e); + } + } + crate::tui::log_buffer::push_log(crate::tui::log_buffer::LogEntry { + level: tracing::Level::INFO, + message: format!(" {}", text), + }); +} + +pub fn clear() { + match PROGRESS.lock() { + Ok(mut state) => { + state.current_action = None; + state.progress = None; + state.sub_action = None; + } + Err(e) => { + tracing::error!("Progress lock poisoned: {}", e); + } + } +} diff --git a/src/tui/widgets/status.rs b/src/tui/widgets/status.rs index 2f58440..7a38e9a 100644 --- a/src/tui/widgets/status.rs +++ b/src/tui/widgets/status.rs @@ -1,14 +1,13 @@ use ratatui::{ - layout::Rect, - style::{Color, Style}, + layout::{Constraint, Direction, Layout, Rect}, + style::{Color, Modifier, Style}, text::{Line, Span}, - widgets::{Block, BorderType, Borders, Paragraph}, + widgets::{Block, BorderType, Borders, Gauge, Paragraph}, Frame, }; -use tracing::Level; use crate::tui::layout::FocusedArea; -use crate::tui::logging::STATUS_EVENTS; +use crate::tui::progress::PROGRESS; use super::styled_title; @@ -25,27 +24,78 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea) { .border_type(BorderType::Rounded) .border_style(Style::default().fg(border_color)); - let latest = STATUS_EVENTS - .lock() - .ok() - .and_then(|events| events.back().cloned()); + let inner = block.inner(area); + frame.render_widget(block, area); - let content = match latest { - Some(event) => { - let (label, label_color) = match event.level { - Level::ERROR => (" ERROR ", Color::Red), - Level::WARN => (" WARN ", Color::Yellow), - _ => (" INFO ", Color::White), - }; - - Paragraph::new(Line::from(vec![ - Span::styled(label, Style::default().fg(Color::White).bg(label_color)), - Span::raw(" "), - Span::raw(event.message), - ])) + let state = match PROGRESS.lock() { + Ok(s) => s.clone(), + Err(_) => { + let idle = Paragraph::new(Span::styled("Ready", Style::default().fg(Color::DarkGray))); + frame.render_widget(idle, inner); + return; } - None => Paragraph::new(Span::styled("Ready", Style::default().fg(Color::DarkGray))), }; - frame.render_widget(content.block(block), area); + if state.current_action.is_none() { + let idle = Paragraph::new(Span::styled("Ready", Style::default().fg(Color::DarkGray))); + frame.render_widget(idle, inner); + return; + } + + let action_text = state.current_action.as_deref().unwrap_or(""); + let sub_text = state.sub_action.as_deref().unwrap_or(""); + + let has_progress = state.progress.is_some(); + let constraints = if has_progress { + vec![ + Constraint::Length(1), + Constraint::Length(1), + Constraint::Length(1), + ] + } else { + vec![Constraint::Length(1), Constraint::Length(1)] + }; + + let chunks = Layout::default() + .direction(Direction::Vertical) + .constraints(constraints) + .split(inner); + + let mut chunk_idx = 0; + + if has_progress { + if let Some((current, total)) = state.progress { + let ratio = if total > 0 { + (current as f64 / total as f64).min(1.0) + } else { + 0.0 + }; + let pct = (ratio * 100.0) as u16; + let gauge = Gauge::default() + .gauge_style( + Style::default() + .fg(Color::Green) + .bg(Color::DarkGray) + .add_modifier(Modifier::BOLD), + ) + .percent(pct); + frame.render_widget(gauge, chunks[chunk_idx]); + chunk_idx += 1; + } + } + + let action_line = Paragraph::new(Line::from(vec![Span::styled( + action_text, + Style::default().fg(Color::White), + )])); + frame.render_widget(action_line, chunks[chunk_idx]); + chunk_idx += 1; + + if !sub_text.is_empty() && chunk_idx < chunks.len() { + let sub_line = Paragraph::new(Line::from(vec![Span::styled( + sub_text, + Style::default().fg(Color::DarkGray), + )])); + frame.render_widget(sub_line, chunks[chunk_idx]); + } }