use crate::progress::AnalysisProgress;
use crate::spinner::{AnalysisPhase, phase_message};
use rinkaku_tui::TuiSession;
use rinkaku_tui::splash::SplashState;
use std::sync::Mutex;
struct SplashProgressState {
session: TuiSession,
phase_label: String,
tip: Option<String>,
buffered_notes: Vec<String>,
}
pub(crate) struct SplashProgress {
inner: Mutex<SplashProgressState>,
}
impl SplashProgress {
pub(crate) fn new(session: TuiSession, tip: Option<String>) -> Self {
Self {
inner: Mutex::new(SplashProgressState {
session,
phase_label: phase_message(AnalysisPhase::Starting).to_string(),
tip,
buffered_notes: Vec::new(),
}),
}
}
pub(crate) fn into_session_and_notes(self) -> (TuiSession, Vec<String>) {
let state = self
.inner
.into_inner()
.expect("splash progress mutex must not be poisoned");
(state.session, state.buffered_notes)
}
}
impl AnalysisProgress for SplashProgress {
fn set_phase(&self, phase: AnalysisPhase) {
let label = phase_message(phase).to_string();
let mut guard = self
.inner
.lock()
.expect("splash progress mutex must not be poisoned");
guard.phase_label.clone_from(&label);
let mut state = SplashState::label_only(label);
if let Some(tip) = guard.tip.clone() {
state = state.with_tip(tip);
}
let _ = guard.session.draw_splash(&state);
}
fn report_file_progress(&self, done: usize, total: usize) {
let mut guard = self
.inner
.lock()
.expect("splash progress mutex must not be poisoned");
let label = guard.phase_label.clone();
let mut state = SplashState::with_progress(label, done, total);
if let Some(tip) = guard.tip.clone() {
state = state.with_tip(tip);
}
let _ = guard.session.draw_splash(&state);
}
fn note(&self, message: String) {
let mut guard = self
.inner
.lock()
.expect("splash progress mutex must not be poisoned");
guard.buffered_notes.push(message);
}
}