oxi-cli 0.1.4-alpha

Terminal-based AI coding assistant — multi-provider, streaming-first, extensible
Documentation
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//! Theme system for oxi TUI
//!
//! Provides theme management with dark/light mode support,
//! color schemes, and custom theme loading from ~/.oxi/themes/

use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::RwLock;

/// Color representation
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}

impl Color {
    pub fn new(r: u8, g: u8, b: u8) -> Self {
        Self { r, g, b }
    }

    pub fn from_hex(hex: &str) -> Option<Self> {
        let hex = hex.trim_start_matches('#');
        if hex.len() != 6 {
            return None;
        }
        let r = u8::from_str_radix(&hex[0..2], 16).ok()?;
        let g = u8::from_str_radix(&hex[2..4], 16).ok()?;
        let b = u8::from_str_radix(&hex[4..6], 16).ok()?;
        Some(Self { r, g, b })
    }

    pub fn to_rgb_tuple(&self) -> (u8, u8, u8) {
        (self.r, self.g, self.b)
    }
}

/// Predefined theme colors
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ThemeColorRole {
    Primary,
    Secondary,
    Accent,
    Background,
    Foreground,
    Border,
    Error,
    Warning,
    Success,
    Muted,
    Link,
}

impl ThemeColorRole {
    pub fn name(&self) -> &'static str {
        match self {
            ThemeColorRole::Primary => "primary",
            ThemeColorRole::Secondary => "secondary",
            ThemeColorRole::Accent => "accent",
            ThemeColorRole::Background => "background",
            ThemeColorRole::Foreground => "foreground",
            ThemeColorRole::Border => "border",
            ThemeColorRole::Error => "error",
            ThemeColorRole::Warning => "warning",
            ThemeColorRole::Success => "success",
            ThemeColorRole::Muted => "muted",
            ThemeColorRole::Link => "link",
        }
    }
}

/// A complete theme definition
#[derive(Debug, Clone)]
pub struct Theme {
    /// Theme name
    pub name: String,
    /// Whether this is a dark theme
    pub is_dark: bool,
    /// Color definitions (role -> RGB)
    pub colors: HashMap<String, Color>,
    /// Optional path to the theme file
    pub source_path: Option<PathBuf>,
}

impl Theme {
    /// Create a new theme
    pub fn new(name: &str, is_dark: bool) -> Self {
        Self {
            name: name.to_string(),
            is_dark,
            colors: HashMap::new(),
            source_path: None,
        }
    }

    /// Get a color by role name
    pub fn get_color(&self, role: &str) -> Option<Color> {
        self.colors.get(role).cloned()
    }

    /// Set a color
    pub fn set_color(&mut self, role: &str, color: Color) {
        self.colors.insert(role.to_string(), color);
    }

    /// Build from JSON (for theme file loading)
    pub fn from_json(name: &str, json: &serde_json::Value) -> Option<Self> {
        let is_dark = json
            .get("isDark")
            .or_else(|| json.get("is_dark"))
            .and_then(|v| v.as_bool())
            .unwrap_or(true);

        let mut theme = Self::new(name, is_dark);

        if let Some(colors) = json.get("colors").and_then(|v| v.as_object()) {
            for (key, value) in colors {
                if let Some(color_str) = value.as_str() {
                    if let Some(color) = Color::from_hex(color_str) {
                        theme.set_color(key, color);
                    }
                } else if let Some(rgb) = value.as_array() {
                    if rgb.len() >= 3 {
                        let r = rgb[0].as_u64()? as u8;
                        let g = rgb[1].as_u64()? as u8;
                        let b = rgb[2].as_u64()? as u8;
                        theme.set_color(key, Color::new(r, g, b));
                    }
                }
            }
        }

        Some(theme)
    }

    /// Serialize to JSON
    pub fn to_json(&self) -> serde_json::Value {
        let mut obj = serde_json::Map::new();
        obj.insert("name".to_string(), serde_json::Value::String(self.name.clone()));
        obj.insert(
            "isDark".to_string(),
            serde_json::Value::Bool(self.is_dark),
        );

        let mut colors = serde_json::Map::new();
        for (key, color) in &self.colors {
            colors.insert(
                key.clone(),
                serde_json::Value::String(format!("#{:02x}{:02x}{:02x}", color.r, color.g, color.b)),
            );
        }
        obj.insert("colors".to_string(), serde_json::Value::Object(colors));

        serde_json::Value::Object(obj)
    }
}

/// Built-in dark theme
pub fn dark_theme() -> Theme {
    let mut theme = Theme::new("dark", true);

    // Background colors
    theme.set_color("background", Color::new(30, 30, 30));
    theme.set_color("foreground", Color::new(220, 220, 220));
    theme.set_color("primary", Color::new(100, 180, 255));
    theme.set_color("secondary", Color::new(150, 150, 150));
    theme.set_color("accent", Color::new(80, 200, 120));
    theme.set_color("border", Color::new(60, 60, 60));
    theme.set_color("muted", Color::new(100, 100, 100));
    theme.set_color("error", Color::new(255, 80, 80));
    theme.set_color("warning", Color::new(255, 200, 80));
    theme.set_color("success", Color::new(80, 200, 120));
    theme.set_color("link", Color::new(100, 180, 255));

    theme
}

/// Built-in light theme
pub fn light_theme() -> Theme {
    let mut theme = Theme::new("light", false);

    // Background colors
    theme.set_color("background", Color::new(255, 255, 255));
    theme.set_color("foreground", Color::new(30, 30, 30));
    theme.set_color("primary", Color::new(0, 100, 200));
    theme.set_color("secondary", Color::new(100, 100, 100));
    theme.set_color("accent", Color::new(0, 150, 80));
    theme.set_color("border", Color::new(200, 200, 200));
    theme.set_color("muted", Color::new(150, 150, 150));
    theme.set_color("error", Color::new(200, 40, 40));
    theme.set_color("warning", Color::new(200, 150, 0));
    theme.set_color("success", Color::new(0, 150, 80));
    theme.set_color("link", Color::new(0, 100, 200));

    theme
}

/// Theme manager
pub struct ThemeManager {
    themes: HashMap<String, Theme>,
    current_theme: RwLock<String>,
}

impl ThemeManager {
    /// Create a new theme manager with built-in themes
    pub fn new() -> Self {
        let mut manager = Self {
            themes: HashMap::new(),
            current_theme: RwLock::new("dark".to_string()),
        };

        manager.register_theme(dark_theme());
        manager.register_theme(light_theme());

        manager
    }

    /// Register a theme
    pub fn register_theme(&mut self, theme: Theme) {
        self.themes.insert(theme.name.clone(), theme);
    }

    /// Get a theme by name
    pub fn get_theme(&self, name: &str) -> Option<&Theme> {
        self.themes.get(name)
    }

    /// Get current theme
    pub fn get_current_theme(&self) -> Option<Theme> {
        let name = self.current_theme.read().unwrap().clone();
        self.themes.get(&name).cloned()
    }

    /// Set current theme
    pub fn set_current_theme(&self, name: &str) -> bool {
        if self.themes.contains_key(name) {
            *self.current_theme.write().unwrap() = name.to_string();
            true
        } else {
            false
        }
    }

    /// Get all registered theme names
    pub fn theme_names(&self) -> Vec<String> {
        self.themes.keys().cloned().collect()
    }

    /// Load a theme from a file
    pub fn load_theme_from_file(&mut self, path: &PathBuf) -> Option<Theme> {
        let content = std::fs::read_to_string(path).ok()?;
        let json: serde_json::Value = serde_json::from_str(&content).ok()?;

        let name = json
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or("custom");

        let mut theme = Theme::from_json(name, &json)?;
        theme.source_path = Some(path.clone());

        self.register_theme(theme.clone());
        Some(theme)
    }

    /// Load all themes from ~/.oxi/themes/
    pub fn load_user_themes(&mut self) -> Vec<Theme> {
        let mut loaded = Vec::new();

        if let Some(home) = std::env::var_os("HOME") {
            let themes_dir = PathBuf::from(home).join(".oxi").join("themes");

            if themes_dir.exists() {
                if let Ok(entries) = std::fs::read_dir(themes_dir) {
                    for entry in entries.flatten() {
                        let path = entry.path();
                        if path.extension().map(|e| e == "json").unwrap_or(false) {
                            if let Some(theme) = self.load_theme_from_file(&path) {
                                loaded.push(theme);
                            }
                        }
                    }
                }
            }
        }

        loaded
    }

    /// Toggle between dark and light themes
    pub fn toggle_dark_light(&self) -> bool {
        let current = self.current_theme.read().unwrap().clone();
        let current_theme = self.themes.get(&current)?;

        let next_name = if current_theme.is_dark {
            "light"
        } else {
            "dark"
        };

        self.set_current_theme(next_name)
    }
}

impl Default for ThemeManager {
    fn default() -> Self {
        Self::new()
    }
}

/// ANSI escape code helpers for TUI rendering
impl Theme {
    /// Get ANSI foreground escape code
    pub fn fg_ansi(&self, role: &str) -> String {
        self.get_color(role)
            .map(|c| format!("\x1b[38;2;{};{};{}m", c.r, c.g, c.b))
            .unwrap_or_else(|| "\x1b[39m".to_string())
    }

    /// Get ANSI background escape code
    pub fn bg_ansi(&self, role: &str) -> String {
        self.get_color(role)
            .map(|c| format!("\x1b[48;2;{};{};{}m", c.r, c.g, c.b))
            .unwrap_or_else(|| "\x1b[49m".to_string())
    }

    /// Get color as 256-color ANSI code (approximation)
    pub fn fg_ansi_256(&self, role: &str) -> String {
        self.get_color(role).map(|c| {
            let index = 16 + (c.r / 43) * 36 + (c.g / 43) * 6 + (c.b / 43);
            format!("\x1b[38;5;{}m", index)
        }).unwrap_or_else(|| "\x1b[39m".to_string())
    }

    /// Reset ANSI formatting
    pub fn reset_ansi() -> String {
        "\x1b[0m".to_string()
    }

    /// Bold formatting
    pub fn bold_ansi() -> String {
        "\x1b[1m".to_string()
    }

    /// Italic formatting
    pub fn italic_ansi() -> String {
        "\x1b[3m".to_string()
    }

    /// Underline formatting
    pub fn underline_ansi() -> String {
        "\x1b[4m".to_string()
    }
}

/// Global theme instance (for convenience)
lazy_static::lazy_static! {
    pub static ref GLOBAL_THEME: RwLock<Theme> = RwLock::new(dark_theme());
}

/// Set the global theme
pub fn set_global_theme(theme: Theme) {
    *GLOBAL_THEME.write().unwrap() = theme;
}

/// Get the global theme
pub fn get_global_theme() -> Theme {
    GLOBAL_THEME.read().unwrap().clone()
}

/// Format text with a theme color role
pub fn fmt_with_color(text: &str, role: &str) -> String {
    let theme = get_global_theme();
    format!("{}{}{}", theme.fg_ansi(role), text, Theme::reset_ansi())
}

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

    #[test]
    fn test_color_from_hex() {
        let color = Color::from_hex("#FF5500").unwrap();
        assert_eq!(color.r, 255);
        assert_eq!(color.g, 85);
        assert_eq!(color.b, 0);
    }

    #[test]
    fn test_color_from_hex_with_hash() {
        let color = Color::from_hex("#AABBCC").unwrap();
        assert_eq!(color.r, 170);
        assert_eq!(color.g, 187);
        assert_eq!(color.b, 204);
    }

    #[test]
    fn test_color_invalid_hex() {
        assert!(Color::from_hex("invalid").is_none());
        assert!(Color::from_hex("#ABC").is_none()); // Too short
    }

    #[test]
    fn test_theme_new() {
        let theme = Theme::new("test", true);
        assert_eq!(theme.name, "test");
        assert!(theme.is_dark);
        assert!(theme.colors.is_empty());
    }

    #[test]
    fn test_theme_set_get_color() {
        let mut theme = Theme::new("test", true);
        theme.set_color("primary", Color::new(255, 0, 0));

        let color = theme.get_color("primary").unwrap();
        assert_eq!(color.r, 255);
        assert_eq!(color.g, 0);
        assert_eq!(color.b, 0);
    }

    #[test]
    fn test_dark_theme() {
        let theme = dark_theme();
        assert!(theme.is_dark);
        assert!(theme.get_color("background").is_some());
        assert!(theme.get_color("foreground").is_some());
    }

    #[test]
    fn test_light_theme() {
        let theme = light_theme();
        assert!(!theme.is_dark);
        assert!(theme.get_color("background").is_some());
        assert!(theme.get_color("foreground").is_some());
    }

    #[test]
    fn test_theme_from_json() {
        let json = serde_json::json!({
            "name": "custom",
            "isDark": false,
            "colors": {
                "primary": "#FF0000",
                "background": [10, 20, 30]
            }
        });

        let theme = Theme::from_json("custom", &json).unwrap();
        assert_eq!(theme.name, "custom");
        assert!(!theme.is_dark);
        assert_eq!(theme.get_color("primary").unwrap().r, 255);
        assert_eq!(theme.get_color("background").unwrap().r, 10);
    }

    #[test]
    fn test_theme_to_json() {
        let mut theme = Theme::new("test", true);
        theme.set_color("primary", Color::new(255, 128, 64));

        let json = theme.to_json();
        assert_eq!(json["name"], "test");
        assert!(json["isDark"].as_bool().unwrap());
    }

    #[test]
    fn test_theme_manager() {
        let mut manager = ThemeManager::new();

        // Check built-in themes
        assert!(manager.get_theme("dark").is_some());
        assert!(manager.get_theme("light").is_some());
        assert!(manager.get_theme("nonexistent").is_none());

        // Check current theme
        assert_eq!(manager.get_current_theme().unwrap().name, "dark");

        // Set and get
        manager.set_current_theme("light");
        assert_eq!(manager.get_current_theme().unwrap().name, "light");

        // Toggle
        manager.toggle_dark_light();
        assert_eq!(manager.get_current_theme().unwrap().name, "dark");
    }

    #[test]
    fn test_ansi_codes() {
        let theme = dark_theme();

        let fg = theme.fg_ansi("primary");
        assert!(fg.starts_with("\x1b[38;2;"));

        let bg = theme.bg_ansi("background");
        assert!(bg.starts_with("\x1b[48;2;"));

        let reset = Theme::reset_ansi();
        assert_eq!(reset, "\x1b[0m");
    }

    #[test]
    fn test_global_theme() {
        set_global_theme(light_theme());
        let theme = get_global_theme();
        assert!(!theme.is_dark);
    }

    #[test]
    fn test_fmt_with_color() {
        let formatted = fmt_with_color("test", "primary");
        assert!(formatted.starts_with("\x1b[38;2;"));
        assert!(formatted.contains("test"));
        assert!(formatted.ends_with("\x1b[0m"));
    }
}