dugix 1.0.0

A local-first terminal chat app for deep research and multi-agent work
pub mod approval;
pub mod apps;
pub mod chrome;
pub mod connectors;
pub mod context;
pub mod copy;
pub mod files;
pub mod fleet;
pub mod harness;
pub mod help;
pub mod image_viewer;
pub mod jobs;
pub mod key;
pub mod library;
pub mod list_filter;
pub mod local;
pub mod login;
pub mod model;
pub mod procs;
pub mod research_live;
pub mod session;
pub mod settings;
pub mod skills;
pub mod space;
pub mod subagents;
pub mod swarm;
pub mod usage;

#[cfg(test)]
mod library_tests;
#[cfg(test)]
mod pattern_actions_tests;
#[cfg(test)]
mod pattern_lists_tests;
#[cfg(test)]
mod pattern_runs_tests;
#[cfg(test)]
mod pattern_viewers_tests;
#[cfg(test)]
mod subagents_tests;
#[cfg(test)]
mod tests;

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

// Shared key classification for the session/space/skills popups: these helpers
// decide which action a key maps to; each popup decides what the action does.

/// Actions available while browsing a popup's list; `Create` and `Rename` are
/// gated by `classify_browse_key`'s flags.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum BrowseAction {
    Close,
    Filter(char),
    Backspace,
    MoveUp,
    MoveDown,
    PageUp,
    PageDown,
    Top,
    Bottom,
    Create,
    Rename,
    ConfirmDelete,
}

/// Move a list cursor by a paging `BrowseAction`; `page` is a page jump.
pub(super) fn apply_page(mut move_by: impl FnMut(i32), action: BrowseAction, page: i32) {
    match action {
        BrowseAction::PageUp => move_by(-page),
        BrowseAction::PageDown => move_by(page),
        BrowseAction::Top => move_by(i32::MIN / 2),
        BrowseAction::Bottom => move_by(i32::MAX / 2),
        _ => {}
    }
}

/// Map a keypress to a `BrowseAction`; Enter is left to each popup, where it
/// means open/select the highlighted item.
pub(super) const fn classify_browse_key(
    key: KeyEvent,
    supports_create: bool,
    supports_rename: bool,
) -> Option<BrowseAction> {
    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
    match key.code {
        KeyCode::Esc => Some(BrowseAction::Close),
        KeyCode::Up => Some(BrowseAction::MoveUp),
        KeyCode::Down => Some(BrowseAction::MoveDown),
        KeyCode::PageUp => Some(BrowseAction::PageUp),
        KeyCode::PageDown => Some(BrowseAction::PageDown),
        KeyCode::Home => Some(BrowseAction::Top),
        KeyCode::End => Some(BrowseAction::Bottom),
        KeyCode::Char('n') if ctrl && supports_create => Some(BrowseAction::Create),
        KeyCode::Char('r') if ctrl && supports_rename => Some(BrowseAction::Rename),
        KeyCode::Char('d') if ctrl => Some(BrowseAction::ConfirmDelete),
        KeyCode::Backspace => Some(BrowseAction::Backspace),
        KeyCode::Char(c) if !ctrl => Some(BrowseAction::Filter(c)),
        _ => None,
    }
}

/// Actions available while editing a text field.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum EditAction {
    Cancel,
    Save,
    Backspace,
    Push(char),
}

pub(super) const fn classify_edit_key(key: KeyEvent) -> Option<EditAction> {
    match key.code {
        KeyCode::Esc => Some(EditAction::Cancel),
        KeyCode::Enter => Some(EditAction::Save),
        KeyCode::Backspace => Some(EditAction::Backspace),
        // An unbound Ctrl/Alt chord is not text.
        KeyCode::Char(c) if !is_chord(key) => Some(EditAction::Push(c)),
        _ => None,
    }
}

/// Whether a key carries Ctrl or Alt — a shortcut, never typed text.
pub(super) const fn is_chord(key: KeyEvent) -> bool {
    key.modifiers
        .intersects(KeyModifiers::CONTROL.union(KeyModifiers::ALT))
}

/// Actions available while a delete confirmation is showing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ConfirmDeleteAction {
    Yes,
    No,
}

pub(super) const fn classify_confirm_delete_key(key: KeyEvent) -> Option<ConfirmDeleteAction> {
    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
    match key.code {
        KeyCode::Char('d') if ctrl => Some(ConfirmDeleteAction::Yes),
        KeyCode::Esc => Some(ConfirmDeleteAction::No),
        _ => None,
    }
}