dugix 1.0.0

A local-first terminal chat app for deep research and multi-agent work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//! Semantic terminal colors and background modes.

use dugix_core::theme_colors::{Palette, ThemeColors};
use ratatui::style::{Color, Style};

pub use dugix_core::theme_colors::source_stamp;
#[cfg(test)]
use dugix_core::theme_colors::{Alacritty, ghostty_pairs};

/// Uses the terminal's default background for transparency because cells cannot alpha-blend.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BackgroundMode {
    #[default]
    Opaque,
    Transparent,
}

impl BackgroundMode {
    pub const SETTING_KEY: &str = "ui_background";

    #[must_use]
    pub const fn key(self) -> &'static str {
        match self {
            Self::Opaque => "opaque",
            Self::Transparent => "transparent",
        }
    }

    #[must_use]
    pub const fn label(self) -> &'static str {
        self.key()
    }

    #[must_use]
    pub const fn next(self) -> Self {
        match self {
            Self::Opaque => Self::Transparent,
            Self::Transparent => Self::Opaque,
        }
    }

    #[must_use]
    pub fn parse(value: &str) -> Option<Self> {
        let value = value.trim();
        if value.eq_ignore_ascii_case("opaque")
            || value.eq_ignore_ascii_case("solid")
            || value.eq_ignore_ascii_case("on")
        {
            Some(Self::Opaque)
        } else if value.eq_ignore_ascii_case("transparent")
            || value.eq_ignore_ascii_case("terminal")
            || value.eq_ignore_ascii_case("reset")
            || value.eq_ignore_ascii_case("off")
        {
            Some(Self::Transparent)
        } else {
            None
        }
    }
}

#[derive(Clone, Copy)]
pub struct Theme {
    pub bg: Color,
    pub surface: Color,
    pub fg: Color,
    pub fg_dim: Color,
    pub accent: Color,
    pub accent2: Color,
    pub border: Color,
    pub border_dim: Color,
    pub success: Color,
    pub warning: Color,
    pub error: Color,
    pub diff_add_bg: Color,
    pub diff_del_bg: Color,
    pub user_msg: Color,
    pub assistant_msg: Color,
    pub tool_msg: Color,
    pub research_msg: Color,
    pub raised: Color,
    raised_opaque: Color,
    pub control: Color,
    pub selection: Color,
    pub round_caps: bool,
}

impl Default for Theme {
    fn default() -> Self {
        Self {
            bg: Color::Black,
            surface: Color::Black,
            fg: Color::White,
            fg_dim: Color::DarkGray,
            accent: Color::Cyan,
            accent2: Color::Magenta,
            border: Color::Cyan,
            border_dim: Color::DarkGray,
            success: Color::Green,
            warning: Color::Yellow,
            error: Color::Red,
            diff_add_bg: Color::Indexed(22),
            diff_del_bg: Color::Indexed(52),
            user_msg: Color::Cyan,
            assistant_msg: Color::White,
            tool_msg: Color::Yellow,
            research_msg: Color::Magenta,
            raised: Color::Indexed(236),
            raised_opaque: Color::Indexed(236),
            control: Color::Indexed(238),
            selection: Color::Indexed(239),
            round_caps: false,
        }
    }
}

impl Theme {
    fn from_palette(palette: &Palette) -> Self {
        let mut theme = Self::default();
        for (role, field) in [
            ("bg", &mut theme.bg),
            ("fg", &mut theme.fg),
            ("fg_dim", &mut theme.fg_dim),
            ("accent", &mut theme.accent),
            ("accent2", &mut theme.accent2),
            ("border", &mut theme.border),
            ("border_dim", &mut theme.border_dim),
            ("success", &mut theme.success),
            ("warning", &mut theme.warning),
            ("error", &mut theme.error),
            ("diff_add_bg", &mut theme.diff_add_bg),
            ("diff_del_bg", &mut theme.diff_del_bg),
            ("user_msg", &mut theme.user_msg),
            ("assistant_msg", &mut theme.assistant_msg),
            ("tool_msg", &mut theme.tool_msg),
            ("research_msg", &mut theme.research_msg),
            ("raised", &mut theme.raised),
            ("control", &mut theme.control),
            ("selection", &mut theme.selection),
        ] {
            if let Some([r, g, b]) = palette.rgb(role) {
                *field = Color::Rgb(r, g, b);
            }
        }
        theme.surface = theme.bg;
        theme.raised_opaque = theme.raised;
        theme
    }

    #[cfg(test)]
    fn from_alacritty(a: &Alacritty) -> Self {
        Self::from_palette(&Palette::from_alacritty(a))
    }

    #[cfg(test)]
    fn from_ghostty(colors: &std::collections::HashMap<String, String>) -> Option<Self> {
        Palette::from_ghostty(colors).map(|palette| Self::from_palette(&palette))
    }

    /// Keeps studio colors opaque because terminal transparency is not a deck color.
    #[must_use]
    pub fn colors(&self) -> ThemeColors {
        [
            ("bg", self.bg),
            ("fg", self.fg),
            ("fg_dim", self.fg_dim),
            ("accent", self.accent),
            ("accent2", self.accent2),
            ("border", self.border),
            ("border_dim", self.border_dim),
            ("success", self.success),
            ("warning", self.warning),
            ("error", self.error),
            ("raised", self.raised_opaque),
            ("control", self.control),
            ("selection", self.selection),
        ]
        .map(|(role, color)| (role.into(), color_hex(color)))
        .into()
    }

    pub fn set_background_mode(&mut self, mode: BackgroundMode) {
        (self.surface, self.raised) = match mode {
            BackgroundMode::Opaque => (self.bg, self.raised_opaque),
            BackgroundMode::Transparent => (Color::Reset, Color::Reset),
        };
    }

    #[must_use]
    pub fn background_style(&self) -> Style {
        Style::default().bg(self.surface)
    }

    /// Chooses readable text for a filled control in light or dark themes.
    #[must_use]
    pub fn on(&self, fill: Color) -> Color {
        let fill = luminance(fill);
        let contrast = |color: Color| {
            let other = luminance(color);
            (fill.max(other) + 0.05) / (fill.min(other) + 0.05)
        };
        if contrast(self.bg) >= contrast(self.fg) {
            self.bg
        } else {
            self.fg
        }
    }
}

fn luminance(color: Color) -> f64 {
    let hex = color_hex(color);
    let channel = |range: std::ops::Range<usize>| {
        let value = f64::from(u8::from_str_radix(&hex[range], 16).unwrap_or(0)) / 255.0;
        if value <= 0.039_28 {
            value / 12.92
        } else {
            ((value + 0.055) / 1.055).powf(2.4)
        }
    };
    0.2126 * channel(1..3) + 0.7152 * channel(3..5) + 0.0722 * channel(5..7)
}

fn color_hex(color: Color) -> String {
    use dugix_core::theme_colors::indexed_hex;
    match color {
        Color::Rgb(r, g, b) => format!("#{r:02x}{g:02x}{b:02x}"),
        Color::Indexed(index) => indexed_hex(index),
        Color::Black | Color::Reset => indexed_hex(0),
        Color::Red => indexed_hex(1),
        Color::Green => indexed_hex(2),
        Color::Yellow => indexed_hex(3),
        Color::Blue => indexed_hex(4),
        Color::Magenta => indexed_hex(5),
        Color::Cyan => indexed_hex(6),
        Color::Gray => indexed_hex(7),
        Color::White => indexed_hex(15),
        Color::DarkGray => indexed_hex(8),
        Color::LightRed => indexed_hex(9),
        Color::LightGreen => indexed_hex(10),
        Color::LightYellow => indexed_hex(11),
        Color::LightBlue => indexed_hex(12),
        Color::LightMagenta => indexed_hex(13),
        Color::LightCyan => indexed_hex(14),
    }
}

pub fn load() -> Theme {
    let mut theme = Theme::from_palette(&dugix_core::theme_colors::load());
    let var = |name| std::env::var(name).ok();
    theme.round_caps = round_caps(
        var("DUGIX_ROUND_CAPS").as_deref(),
        var("TERM_PROGRAM").as_deref(),
        var("TERM").as_deref(),
        std::env::home_dir().is_some_and(|home| home.join(".config/omarchy").is_dir()),
    );
    theme
}

// Ghostty, WezTerm and Kitty bundle Powerline glyphs; Omarchy provides a Nerd Font.
#[must_use]
pub fn round_caps(
    forced: Option<&str>,
    term_program: Option<&str>,
    term: Option<&str>,
    omarchy: bool,
) -> bool {
    match forced.map(str::trim) {
        Some("1" | "true" | "on" | "yes") => return true,
        Some("0" | "false" | "off" | "no") => return false,
        _ => {}
    }
    let program = term_program.unwrap_or_default().to_ascii_lowercase();
    let term = term.unwrap_or_default().to_ascii_lowercase();
    omarchy
        || matches!(program.as_str(), "ghostty" | "wezterm" | "kitty")
        || term.contains("kitty")
        || term.contains("ghostty")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn text_on_fills_picks_the_more_readable_theme_color() {
        let dark = Theme::default();
        assert_eq!(dark.on(Color::Cyan), Color::Black);
        assert_eq!(dark.on(Color::Indexed(236)), Color::White);
        let light = Theme {
            bg: Color::Rgb(250, 250, 250),
            fg: Color::Rgb(30, 30, 30),
            ..Theme::default()
        };
        assert_eq!(light.on(Color::Rgb(20, 60, 140)), Color::Rgb(250, 250, 250));
        assert_eq!(light.on(Color::Rgb(250, 220, 90)), Color::Rgb(30, 30, 30));
    }

    #[test]
    fn round_caps_follow_the_override_then_known_terminals() {
        assert!(round_caps(Some("1"), None, None, false));
        assert!(!round_caps(Some("0"), Some("ghostty"), None, true));
        assert!(round_caps(None, Some("ghostty"), None, false));
        assert!(round_caps(None, Some("WezTerm"), None, false));
        assert!(round_caps(None, None, Some("xterm-kitty"), false));
        assert!(round_caps(None, Some("Apple_Terminal"), None, true));
        assert!(!round_caps(
            None,
            Some("Apple_Terminal"),
            Some("xterm-256color"),
            false
        ));
        assert!(!Theme::default().round_caps);
    }

    #[test]
    fn diff_backgrounds_follow_rgb_palettes_with_ansi_fallbacks() {
        let theme = Theme::default();
        assert_eq!(theme.diff_add_bg, Color::Indexed(22));
        assert_eq!(theme.diff_del_bg, Color::Indexed(52));
        let colors = ghostty_pairs(
            "background = #101820\nforeground = #eeeeee\npalette = 2=#20d040\npalette = 1=#e02030",
        )
        .into_iter()
        .collect();
        let theme = Theme::from_ghostty(&colors).unwrap();
        assert_eq!(theme.diff_add_bg, Color::Rgb(19, 57, 38));
        assert_eq!(theme.diff_del_bg, Color::Rgb(53, 25, 35));
    }

    #[test]
    fn parses_omarchy_alacritty_toml() {
        let toml = r##"
[colors.primary]
background = "#0B0C16"
foreground = "#ddf7ff"

[colors.normal]
black = "#0B0C16"
red = "#50f872"
green = "#4fe88f"
yellow = "#50f7d4"
blue = "#829dd4"
magenta = "#86a7df"
cyan = "#7cf8f7"
white = "#85E1FB"

[colors.bright]
black = "#6a6e95"
red = "#85ff9d"
green = "#9cf7c2"
yellow = "#a4ffec"
blue = "#c4d2ed"
magenta = "#cddbf4"
cyan = "#d1fffe"
white = "#ddf7ff"
"##;
        let a: Alacritty = toml::from_str(toml).unwrap();
        let theme = Theme::from_alacritty(&a);
        assert_eq!(theme.accent, Color::Rgb(0x7c, 0xf8, 0xf7));
        assert_eq!(theme.fg, Color::Rgb(0xdd, 0xf7, 0xff));
    }

    #[test]
    fn background_mode_parses_and_cycles() {
        assert_eq!(BackgroundMode::parse("solid"), Some(BackgroundMode::Opaque));
        assert_eq!(
            BackgroundMode::parse("terminal"),
            Some(BackgroundMode::Transparent)
        );
        assert_eq!(BackgroundMode::Opaque.next(), BackgroundMode::Transparent);
        assert_eq!(BackgroundMode::Transparent.next(), BackgroundMode::Opaque);
    }

    #[test]
    fn background_mode_updates_surface_color() {
        let mut theme = Theme::default();
        let raised = theme.raised;
        theme.set_background_mode(BackgroundMode::Transparent);
        assert_eq!(theme.surface, Color::Reset);
        assert_eq!(
            theme.raised,
            Color::Reset,
            "no opaque blocks when see-through"
        );
        theme.set_background_mode(BackgroundMode::Opaque);
        assert_eq!(theme.surface, theme.bg);
        assert_eq!(theme.raised, raised);
    }

    #[test]
    fn missing_file_falls_back_to_default() {
        // No omarchy config in the test sandbox (or whatever's there parses) —
        // either way `load()` must not panic and must return *some* theme.
        let _ = load();
    }

    #[test]
    fn parses_a_ghostty_theme_with_inline_overrides_and_derived_shades() {
        let theme_file = "background = #171c22\nforeground = #d9d6cf\n\
            selection-background = #35464d\npalette = 6=#7fa6a2\npalette = 8=#73808c";
        let mut colors: std::collections::HashMap<String, String> =
            ghostty_pairs(theme_file).into_iter().collect();
        // A config line overrides the theme file.
        colors.extend(ghostty_pairs("palette = 5=#a092b4"));
        let t = Theme::from_ghostty(&colors).unwrap();
        assert_eq!(t.bg, Color::Rgb(0x17, 0x1c, 0x22));
        assert_eq!(t.accent, Color::Rgb(0x7f, 0xa6, 0xa2));
        assert_eq!(t.accent2, Color::Rgb(0xa0, 0x92, 0xb4));
        assert_eq!(t.selection, Color::Rgb(0x35, 0x46, 0x4d));
        // Raised sits just above the background, toward the foreground.
        let (Color::Rgb(r, ..), Color::Rgb(br, ..)) = (t.raised, t.bg) else {
            panic!("raised should be RGB");
        };
        assert!(r > br && r < 0x40, "{r:#x}");
    }

    #[test]
    fn ghostty_theme_needs_background_and_foreground() {
        let colors = ghostty_pairs("palette = 6=#7fa6a2").into_iter().collect();
        assert!(Theme::from_ghostty(&colors).is_none());
    }

    #[test]
    fn control_surface_has_ansi_fallback_and_rgb_contrast_in_both_theme_sources() {
        let fallback = Theme::default();
        assert_eq!(fallback.control, Color::Indexed(238));
        assert_ne!(fallback.control, fallback.bg);
        let a: Alacritty = toml::from_str(
            r##"
[colors.primary]
background = "#000000"
foreground = "#ffffff"
[colors.normal]
black = "#000000"
red = "#ff0000"
green = "#00ff00"
yellow = "#ffff00"
magenta = "#ff00ff"
cyan = "#00ffff"
[colors.bright]
black = "#808080"
red = "#ff0000"
green = "#00ff00"
yellow = "#ffff00"
magenta = "#ff00ff"
cyan = "#00ffff"
"##,
        )
        .unwrap();
        let colors = ghostty_pairs("background = #ffffff\nforeground = #000000")
            .into_iter()
            .collect();
        for (mut theme, control) in [
            (Theme::from_alacritty(&a), Color::Rgb(41, 41, 41)),
            (
                Theme::from_ghostty(&colors).unwrap(),
                Color::Rgb(214, 214, 214),
            ),
        ] {
            assert_eq!(theme.control, control);
            theme.set_background_mode(BackgroundMode::Transparent);
            assert_eq!(theme.control, control);
            theme.set_background_mode(BackgroundMode::Opaque);
            assert_eq!(theme.control, control);
        }
    }

    #[test]
    fn malformed_toml_falls_back_to_default() {
        let bad = "not = [valid";
        let parsed: Option<Alacritty> = toml::from_str(bad).ok();
        assert!(parsed.is_none());
    }
}