magi-code 0.96.2

Repository-aware CLI coding agent for terminal work
Documentation
//! Background warnings written to stderr while Mission Control owns the
//! terminal corrupt the frame, so they are held until the screen is released.

use std::sync::Mutex;

const MAX_HELD_TERMINAL_WARNINGS: usize = 32;

/// `Some` while Mission Control owns the terminal; holds deferred warnings.
static HELD_TERMINAL_WARNINGS: Mutex<Option<Vec<String>>> = Mutex::new(None);

pub(crate) fn emit_terminal_warning(message: String) {
    let mut held = HELD_TERMINAL_WARNINGS
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    match held.as_mut() {
        Some(warnings) => {
            if warnings.len() < MAX_HELD_TERMINAL_WARNINGS && !warnings.contains(&message) {
                warnings.push(message);
            }
        }
        None => eprintln!("{message}"),
    }
}

pub(crate) fn hold_terminal_warnings() {
    let mut held = HELD_TERMINAL_WARNINGS
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());
    held.get_or_insert_with(Vec::new);
}

pub(crate) fn release_terminal_warnings() {
    let warnings = HELD_TERMINAL_WARNINGS
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
        .take()
        .unwrap_or_default();
    for message in warnings {
        eprintln!("{message}");
    }
}