dugix 1.0.0

A local-first terminal chat app for deep research and multi-agent work
//! The status bar (model, status line, context meter) and the completed-task
//! toasts that float above the composer.

// Casts here are on terminal-bounded values (dims, byte colors, glyph counts),
// never on unbounded user data.
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss
)]
use ratatui::Frame;
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Clear, Paragraph};

use super::style::glyph;
use super::{humanize, popups};
use crate::app_view::AppView;

#[cfg(test)]
mod auto_mode_tests {
    use super::*;
    use dugix_core::app::{App, WorkMode};
    use dugix_core::db::Db;
    use dugix_core::space::Space;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    #[test]
    fn auto_badge_visibly_reports_unavailable_confinement() {
        let root = std::env::temp_dir().join(format!("dugix-auto-status-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&root).unwrap();
        let mut app = AppView::new(App::new(
            Db::open_in_memory().unwrap(),
            None,
            Space {
                root: root.join("data"),
            },
        ));
        app.workspace = Some(std::sync::Arc::new(
            dugix_core::workspace::Workspace::open(&root, vec![]).unwrap(),
        ));
        app.work_mode = WorkMode::AgentAuto;
        for (available, expected) in [(true, "agent·auto ·"), (false, "agent·auto (no sandbox) ·")]
        {
            app.auto_sandbox_available = available;
            let mut terminal = Terminal::new(TestBackend::new(180, 1)).unwrap();
            terminal
                .draw(|frame| render_status(frame, &app, frame.area()))
                .unwrap();
            let screen: String = terminal
                .backend()
                .buffer()
                .content
                .iter()
                .map(|cell| cell.symbol())
                .collect();
            assert!(screen.contains(expected), "{screen}");
        }
        drop(app);
        std::fs::remove_dir_all(root).unwrap();
    }
}

/// Green → yellow → red gradient for `t` in 0.0..=1.0.
fn gradient(t: f64) -> Color {
    let (r, g) = if t < 0.5 {
        let k = t / 0.5;
        ((40.0 + k * 190.0) as u8, 200u8)
    } else {
        let k = (t - 0.5) / 0.5;
        (230u8, (200.0 - k * 190.0) as u8)
    };
    Color::Rgb(r, g, 40)
}

#[allow(clippy::too_many_lines)] // status badges, layout, and context meter
pub(super) fn render_status(f: &mut Frame, app: &AppView, area: Rect) {
    use dugix_core::db::DEFAULT_SPACE;
    let theme = &app.theme;
    let dim = Style::default().fg(theme.fg_dim);
    let sep = || super::style::sep(theme);

    let model = app
        .current_model
        .as_deref()
        .map_or_else(|| "no model".to_string(), short_model_label);
    let mut tags: Vec<String> = Vec::new();
    if app.active_space.name != DEFAULT_SPACE {
        tags.push(format!("{} {}", glyph::SPACE, app.active_space.name));
    }
    if app.work_mode.is_agent()
        && let Some(workspace) = &app.workspace
    {
        let mut root = workspace.root().display().to_string();
        if let Some(home) = std::env::var_os("HOME")
            && let Ok(relative) = workspace
                .root()
                .strip_prefix(std::path::PathBuf::from(home))
        {
            root = if relative.as_os_str().is_empty() {
                "~".to_string()
            } else {
                format!("~/{}", relative.display())
            };
        }
        let max_root = (area.width as usize / 3).max(8);
        let len = root.chars().count();
        if len > max_root {
            root = format!(
                "…{}",
                root.chars().skip(len - max_root + 1).collect::<String>()
            );
        }
        let mode = if app.work_mode == dugix_core::app::WorkMode::AgentAuto {
            if app.auto_sandbox_available {
                "agent·auto"
            } else {
                "agent·auto (no sandbox)"
            }
        } else {
            "agent"
        };
        tags.push(format!("{} {mode} · {root}", glyph::TOOL));
    }
    if app.incognito {
        tags.push(format!("{} incognito", glyph::INCOGNITO));
    }
    if app.memory_tidy_due() > 0 && !app.incognito {
        tags.push(format!("{} memory tidy", glyph::DIGEST));
    }
    if app.training_enabled() {
        tags.push(format!("{} training", glyph::DOT));
    }
    let badge_max = (area.width as usize * 2 / 5).max(12);
    let tag_w: usize = tags.iter().map(|t| t.chars().count() + 3).sum();
    let model = fit_badge("", &model, badge_max.saturating_sub(tag_w + 2).max(8));
    let mut left = vec![
        Span::styled(
            format!(" {} ", glyph::MODEL),
            Style::default().fg(theme.accent),
        ),
        Span::styled(
            model,
            Style::default()
                .fg(theme.accent)
                .add_modifier(Modifier::BOLD),
        ),
    ];
    for tag in tags {
        left.push(sep());
        left.push(Span::styled(tag, Style::default().fg(theme.accent2)));
    }

    let procs = app.procs.running_count();
    if procs > 0 {
        left.push(sep());
        left.push(Span::styled(format!("{procs} procs"), dim));
    }

    let agents = app.fleet.live();
    if agents > 0 {
        left.push(sep());
        left.push(Span::styled(format!("{agents} fleet"), dim));
        let blocked = app.fleet.blocked();
        if blocked > 0 {
            left.push(Span::styled(
                format!(" {} {blocked} need you", crate::ui::style::glyph::WARN),
                Style::default().fg(theme.warning),
            ));
        }
    }

    let mut right: Vec<Span> = Vec::new();
    if app.settings.show_stats
        && let Some(limit) = app.context_limit()
    {
        right.extend(context_meter(app, limit, 10));
        right.push(Span::raw(" "));
        right.push(Span::styled(context_numbers(app, limit), dim));
    } else if let Some(rate) = app.meter.turn_cache.reuse_rate() {
        let partial = if app.meter.turn_cache.is_partial() {
            "~"
        } else {
            ""
        };
        right.push(Span::styled(
            format!("{partial}{:.0}% reused", rate * 100.0),
            dim,
        ));
    }
    if !right.is_empty() {
        right.insert(0, Span::raw("  "));
        right.push(Span::raw(" "));
    }

    let width_of = |spans: &[Span]| -> u16 {
        u16::try_from(spans.iter().map(Span::width).sum::<usize>()).unwrap_or(u16::MAX)
    };
    let (left_w, right_w) = (width_of(&left), width_of(&right));
    // On a narrow bar the meter yields first, then the status text.
    let right_w = if left_w + right_w + 8 > area.width {
        0
    } else {
        right_w
    };
    let cols = Layout::horizontal([
        Constraint::Length(left_w),
        Constraint::Min(0),
        Constraint::Length(right_w),
    ])
    .split(area);
    f.render_widget(Paragraph::new(Line::from(left)), cols[0]);
    if !app.status.is_empty() {
        let text = popups::chrome::truncate(&app.status, cols[1].width.saturating_sub(3) as usize);
        f.render_widget(
            Paragraph::new(Line::from(vec![sep(), Span::styled(text, dim)])),
            cols[1],
        );
    }
    if right_w > 0 {
        f.render_widget(
            Paragraph::new(Line::from(right)).alignment(ratatui::layout::Alignment::Right),
            cols[2],
        );
    }
}

/// `▌▌▌▌▌▌▌▌` — how full the context window is, each filled cell colored by its
/// position along the ramp. A half block per cell leaves a gap between
/// segments; block elements are drawn by the terminal itself at exactly one
/// cell, unlike geometric shapes (`▰▱`), which fall back to wider fonts and
/// overlap their neighbours.
const METER_CELL: &str = "▌";

fn context_meter(app: &AppView, limit: u64, cells: usize) -> Vec<Span<'static>> {
    let ratio = if limit > 0 {
        (app.context_used() as f64 / limit as f64).clamp(0.0, 1.0)
    } else {
        0.0
    };
    let filled = (ratio * cells as f64).ceil() as usize;
    (0..cells)
        .map(|x| {
            if x < filled {
                let t = x as f64 / (cells - 1).max(1) as f64;
                Span::styled(METER_CELL, Style::default().fg(gradient(t)))
            } else {
                Span::styled(METER_CELL, Style::default().fg(app.theme.border_dim))
            }
        })
        .collect()
}

/// `34% 44k/128k · 99% reused`. The rate is the share of the tokens the provider
/// had already seen that it served from cache, over the whole turn rather than
/// its last request; omitted while nothing was reusable yet. `~` marks a turn
/// some provider did not fully account for.
fn context_numbers(app: &AppView, limit: u64) -> String {
    let used = app.context_used();
    let pct = if limit > 0 {
        used as f64 / limit as f64 * 100.0
    } else {
        0.0
    };
    let mut label = format!("{pct:.0}% {}/{}", humanize(used), humanize(limit));
    if let Some(rate) = app.meter.turn_cache.reuse_rate() {
        let partial = if app.meter.turn_cache.is_partial() {
            "~"
        } else {
            ""
        };
        let _ = std::fmt::Write::write_fmt(
            &mut label,
            format_args!(" · {partial}{:.0}% reused", rate * 100.0),
        );
    }
    label
}

/// `prefix` + `model` within `max` chars; a too-long label shortens the model
/// name but keeps its ` · backend` tag, which is what disambiguates.
pub(super) fn fit_badge(prefix: &str, model: &str, max: usize) -> String {
    let full = format!("{prefix}{model}");
    if crate::ui::style::width(&full) <= max {
        return full;
    }
    let (name, tag) = model
        .rsplit_once(" · ")
        .map_or((model, String::new()), |(n, t)| (n, format!(" · {t}")));
    let room = max.saturating_sub(crate::ui::style::width(prefix) + crate::ui::style::width(&tag));
    if room < 4 {
        return popups::chrome::truncate(&full, max);
    }
    format!("{prefix}{}{tag}", popups::chrome::truncate(name, room))
}

/// The status-bar name for a model id: its last path segment, plus the backend
/// when it isn't `OpenRouter`. `OpenRouter` ids may contain `:` (`…:free`), so
/// only a known backend prefix is split off.
pub(crate) fn short_model_label(id: &str) -> String {
    let (backend, rest) = match id.split_once(':') {
        Some((tag, rest))
            if matches!(
                tag,
                "openai" | "opencode" | "go" | "codex" | "mistral" | "local"
            ) =>
        {
            (Some(tag), rest)
        }
        _ => (None, id),
    };
    // OpenCode's flat-fee bundle nests its own `go:` tag inside the id.
    let (backend, rest) = match rest.strip_prefix("go:") {
        Some(inner) if backend == Some("opencode") => (Some("go"), inner),
        _ => (backend, rest),
    };
    let name = rest.rsplit('/').next().unwrap_or(rest);
    match backend {
        Some(tag) => format!("{name} · {tag}"),
        None => name.to_string(),
    }
}

/// Persistent, direct-click targets for completed chat tasks; only the newest
/// five are painted, so they can't cover a small terminal.
pub(super) fn render_notifications(f: &mut Frame, app: &mut AppView, area: Rect) {
    app.notification_areas.clear();
    let rows = app.notifications.len().min(5) as u16;
    if rows == 0 || area.width == 0 || area.height == 0 {
        return;
    }
    let width = area.width.min(64);
    let start = app.notifications.len().saturating_sub(rows as usize);
    let y = area.y + area.height.saturating_sub(rows);
    for (offset, index) in (start..app.notifications.len()).enumerate() {
        let notification = &app.notifications[index];
        let (glyph, color) = if notification.success {
            (glyph::OK, app.theme.success)
        } else {
            (glyph::FAIL, app.theme.error)
        };
        let bg = Style::default().bg(app.theme.raised);
        let line = popups::chrome::fit_line(
            Line::from(vec![
                Span::styled(glyph::RAIL, Style::default().fg(color)),
                Span::styled(
                    format!("{glyph} "),
                    Style::default().fg(color).add_modifier(Modifier::BOLD),
                ),
                Span::styled(
                    notification.title.clone(),
                    Style::default()
                        .fg(app.theme.fg)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(
                    format!(" · {} ", notification.text),
                    Style::default().fg(app.theme.fg_dim),
                ),
            ]),
            width as usize,
        );
        // Clear of the scrollbar gutter; the rect is also its click target.
        let w = u16::try_from(line.width()).unwrap_or(width).min(width);
        let rect = Rect {
            x: area.x + area.width.saturating_sub(w + 1),
            y: y + offset as u16,
            width: w,
            height: 1,
        };
        f.render_widget(Clear, rect);
        f.render_widget(
            Paragraph::new(line).style(app.theme.background_style().patch(bg)),
            rect,
        );
        app.notification_areas.push((rect, index));
    }
}