jolt-theme 1.2.0

Theme system for terminal TUI applications with iTerm2 import support
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
use std::fmt;
use std::path::{Path, PathBuf};

const ITERM2_REPO_URL: &str =
    "https://raw.githubusercontent.com/mbadolato/iTerm2-Color-Schemes/master/schemes";
const ITERM2_API_URL: &str =
    "https://api.github.com/repos/mbadolato/iTerm2-Color-Schemes/contents/schemes";

pub const ITERM2_GALLERY_URL: &str = "https://iterm2colorschemes.com/";

static VARIANT_PAIRS: &[(&str, &str)] = &[
    ("3024 Night", "3024 Day"),
    ("Aizen Dark", "Aizen Light"),
    ("Atom One Dark", "Atom One Light"),
    ("Belafonte Night", "Belafonte Day"),
    ("Bluloco Dark", "Bluloco Light"),
    ("Builtin Dark", "Builtin Light"),
    ("Builtin Tango Dark", "Builtin Tango Light"),
    ("Farmhouse Dark", "Farmhouse Light"),
    ("Flexoki Dark", "Flexoki Light"),
    ("GitHub Dark", "GitHub"),
    ("GitHub Dark Colorblind", "GitHub Light Colorblind"),
    ("GitHub Dark Default", "GitHub Light Default"),
    ("GitHub Dark High Contrast", "GitHub Light High Contrast"),
    ("GitLab Dark", "GitLab Light"),
    ("Gruvbox Dark", "Gruvbox Light"),
    ("Gruvbox Dark Hard", "Gruvbox Light Hard"),
    ("Gruvbox Material Dark", "Gruvbox Material Light"),
    ("Iceberg Dark", "Iceberg Light"),
    ("Melange Dark", "Melange Light"),
    ("Neobones Dark", "Neobones Light"),
    ("Nvim Dark", "Nvim Light"),
    ("One Double Dark", "One Double Light"),
    ("One Half Dark", "One Half Light"),
    ("Pencil Dark", "Pencil Light"),
    ("Raycast Dark", "Raycast Light"),
    ("Selenized Dark", "Selenized Light"),
    ("Seoulbones Dark", "Seoulbones Light"),
    ("Tinacious Design Dark", "Tinacious Design Light"),
    ("Violet Dark", "Violet Light"),
    ("Xcode Dark", "Xcode Light"),
    ("Xcode Dark hc", "Xcode Light hc"),
    ("Zenbones Dark", "Zenbones Light"),
    ("Zenwritten Dark", "Zenwritten Light"),
    ("iTerm2 Dark Background", "iTerm2 Light Background"),
    ("iTerm2 Solarized Dark", "iTerm2 Solarized Light"),
    ("iTerm2 Tango Dark", "iTerm2 Tango Light"),
    ("Adwaita Dark", "Adwaita"),
    ("Night Owl", "Light Owl"),
    ("Nord", "Nord Light"),
    ("Onenord", "Onenord Light"),
    ("Pro", "Pro Light"),
    ("Terminal Basic Dark", "Terminal Basic"),
    ("No Clown Fiesta", "No Clown Fiesta Light"),
    ("Rose Pine Moon", "Rose Pine Dawn"),
    ("Rose Pine", "Rose Pine Dawn"),
    ("TokyoNight Night", "TokyoNight Day"),
    ("TokyoNight Moon", "TokyoNight Day"),
    ("TokyoNight Storm", "TokyoNight Day"),
    ("TokyoNight", "TokyoNight Day"),
    ("Ayu", "Ayu Light"),
    ("Ayu Mirage", "Ayu Light"),
    ("Everforest Dark Hard", "Everforest Light Med"),
    ("Tomorrow Night", "Tomorrow"),
    ("Tomorrow Night Blue", "Tomorrow"),
    ("Tomorrow Night Bright", "Tomorrow"),
    ("Tomorrow Night Burns", "Tomorrow"),
    ("Tomorrow Night Eighties", "Tomorrow"),
    ("Catppuccin Frappe", "Catppuccin Latte"),
    ("Catppuccin Macchiato", "Catppuccin Latte"),
    ("Catppuccin Mocha", "Catppuccin Latte"),
];

pub fn lookup_variant_pair(name: &str) -> Option<(&'static str, &'static str)> {
    let lower = name.to_lowercase();
    for &(dark, light) in VARIANT_PAIRS {
        if dark.to_lowercase() == lower || light.to_lowercase() == lower {
            return Some((dark, light));
        }
    }
    None
}

#[derive(Debug)]
pub enum Iterm2Error {
    NetworkError(String),
    ParseError(String),
    NotFound(String),
    IoError(String),
}

impl fmt::Display for Iterm2Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NetworkError(msg) => write!(f, "Network error: {}", msg),
            Self::ParseError(msg) => write!(f, "Parse error: {}", msg),
            Self::NotFound(msg) => write!(f, "Not found: {}", msg),
            Self::IoError(msg) => write!(f, "IO error: {}", msg),
        }
    }
}

impl std::error::Error for Iterm2Error {}

const MIN_CONTRAST_RATIO: f64 = 4.6;

#[derive(Debug, Clone)]
pub struct Iterm2Color {
    pub r: f64,
    pub g: f64,
    pub b: f64,
}

impl Iterm2Color {
    pub fn to_hex(&self) -> String {
        let r = (self.r * 255.0).round() as u8;
        let g = (self.g * 255.0).round() as u8;
        let b = (self.b * 255.0).round() as u8;
        format!("#{:02x}{:02x}{:02x}", r, g, b)
    }

    fn blend(&self, other: &Iterm2Color, ratio: f64) -> Iterm2Color {
        Iterm2Color {
            r: self.r * (1.0 - ratio) + other.r * ratio,
            g: self.g * (1.0 - ratio) + other.g * ratio,
            b: self.b * (1.0 - ratio) + other.b * ratio,
        }
    }

    fn linearize(val: f64) -> f64 {
        if val <= 0.03928 {
            val / 12.92
        } else {
            ((val + 0.055) / 1.055).powf(2.4)
        }
    }

    fn luminance(&self) -> f64 {
        0.2126 * Self::linearize(self.r)
            + 0.7152 * Self::linearize(self.g)
            + 0.0722 * Self::linearize(self.b)
    }

    fn contrast_ratio(&self, other: &Iterm2Color) -> f64 {
        let l1 = self.luminance();
        let l2 = other.luminance();
        let lighter = l1.max(l2);
        let darker = l1.min(l2);
        (lighter + 0.05) / (darker + 0.05)
    }

    fn lighten(&self, amount: f64) -> Iterm2Color {
        Iterm2Color {
            r: self.r + (1.0 - self.r) * amount,
            g: self.g + (1.0 - self.g) * amount,
            b: self.b + (1.0 - self.b) * amount,
        }
    }

    fn darken(&self, amount: f64) -> Iterm2Color {
        Iterm2Color {
            r: self.r * (1.0 - amount),
            g: self.g * (1.0 - amount),
            b: self.b * (1.0 - amount),
        }
    }

    fn ensure_contrast(&self, bg: &Iterm2Color, min_ratio: f64) -> Iterm2Color {
        if self.contrast_ratio(bg) >= min_ratio {
            return self.clone();
        }

        let lighten_result = self.adjust_for_contrast(bg, min_ratio, true);
        let darken_result = self.adjust_for_contrast(bg, min_ratio, false);

        let lighten_passes = lighten_result.contrast_ratio(bg) >= min_ratio;
        let darken_passes = darken_result.contrast_ratio(bg) >= min_ratio;

        match (lighten_passes, darken_passes) {
            (true, false) => lighten_result,
            (false, true) => darken_result,
            (true, true) => {
                let lighten_dist = self.color_distance(&lighten_result);
                let darken_dist = self.color_distance(&darken_result);
                if lighten_dist <= darken_dist {
                    lighten_result
                } else {
                    darken_result
                }
            }
            (false, false) => {
                if lighten_result.contrast_ratio(bg) > darken_result.contrast_ratio(bg) {
                    lighten_result
                } else {
                    darken_result
                }
            }
        }
    }

    fn adjust_for_contrast(&self, bg: &Iterm2Color, min_ratio: f64, lighten: bool) -> Iterm2Color {
        let mut low = 0.0;
        let mut high = 1.0;
        let mut best = self.clone();

        for _ in 0..20 {
            let mid = (low + high) / 2.0;
            let adjusted = if lighten {
                self.lighten(mid)
            } else {
                self.darken(mid)
            };

            if adjusted.contrast_ratio(bg) >= min_ratio {
                best = adjusted;
                high = mid;
            } else {
                low = mid;
            }
        }

        best
    }

    fn color_distance(&self, other: &Iterm2Color) -> f64 {
        let dr = self.r - other.r;
        let dg = self.g - other.g;
        let db = self.b - other.b;
        (dr * dr + dg * dg + db * db).sqrt()
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SchemeVariant {
    Dark,
    Light,
    Unknown,
}

#[derive(Debug)]
pub struct Iterm2Scheme {
    pub background: Iterm2Color,
    pub foreground: Iterm2Color,
    pub selection_bg: Iterm2Color,
    pub selection_fg: Iterm2Color,
    pub ansi: [Iterm2Color; 16],
}

fn detect_variant(name: &str) -> SchemeVariant {
    if let Some((dark, light)) = lookup_variant_pair(name) {
        let lower = name.to_lowercase();
        if dark.to_lowercase() == lower {
            return SchemeVariant::Dark;
        } else if light.to_lowercase() == lower {
            return SchemeVariant::Light;
        }
    }

    let lower = name.to_lowercase();
    if lower.contains("light") || lower.contains("day") || lower.contains("dawn") {
        SchemeVariant::Light
    } else if lower.contains("dark") || lower.contains("night") || lower.contains("moon") {
        SchemeVariant::Dark
    } else {
        SchemeVariant::Unknown
    }
}

fn find_counterpart_name(name: &str) -> Option<String> {
    if let Some((dark, light)) = lookup_variant_pair(name) {
        let lower = name.to_lowercase();
        if dark.to_lowercase() == lower {
            return Some(light.to_string());
        } else {
            return Some(dark.to_string());
        }
    }

    let variant = detect_variant(name);
    match variant {
        SchemeVariant::Dark => {
            let lower = name.to_lowercase();
            if let Some(pos) = lower.find("dark") {
                let mut result = name.to_string();
                let replacement = if &name[pos..pos + 4] == "Dark" {
                    "Light"
                } else {
                    "light"
                };
                result.replace_range(pos..pos + 4, replacement);
                Some(result)
            } else {
                None
            }
        }
        SchemeVariant::Light => {
            let lower = name.to_lowercase();
            if let Some(pos) = lower.find("light") {
                let mut result = name.to_string();
                let replacement = if &name[pos..pos + 5] == "Light" {
                    "Dark"
                } else {
                    "dark"
                };
                result.replace_range(pos..pos + 5, replacement);
                Some(result)
            } else {
                None
            }
        }
        SchemeVariant::Unknown => None,
    }
}

fn find_variant_names(name: &str) -> Vec<String> {
    let variant = detect_variant(name);

    if variant == SchemeVariant::Unknown {
        vec![
            format!("{} Light", name),
            format!("{} Dark", name),
            format!("{}-light", name),
            format!("{}-dark", name),
        ]
    } else {
        vec![]
    }
}

fn parse_color_dict(dict: &plist::Dictionary) -> Option<Iterm2Color> {
    let r = dict.get("Red Component")?.as_real()?;
    let g = dict.get("Green Component")?.as_real()?;
    let b = dict.get("Blue Component")?.as_real()?;
    Some(Iterm2Color { r, g, b })
}

fn extract_color(root: &plist::Dictionary, key: &str) -> Option<Iterm2Color> {
    let dict = root.get(key)?.as_dictionary()?;
    parse_color_dict(dict)
}

pub fn parse_scheme(plist_content: &[u8]) -> Result<Iterm2Scheme, Iterm2Error> {
    let value: plist::Value =
        plist::from_bytes(plist_content).map_err(|e| Iterm2Error::ParseError(e.to_string()))?;

    let root = value
        .as_dictionary()
        .ok_or_else(|| Iterm2Error::ParseError("Expected dictionary at root".to_string()))?;

    let background = extract_color(root, "Background Color")
        .ok_or_else(|| Iterm2Error::ParseError("Missing Background Color".to_string()))?;

    let foreground = extract_color(root, "Foreground Color")
        .ok_or_else(|| Iterm2Error::ParseError("Missing Foreground Color".to_string()))?;

    let selection_bg = extract_color(root, "Selection Color")
        .unwrap_or_else(|| background.blend(&foreground, 0.3));

    let selection_fg =
        extract_color(root, "Selected Text Color").unwrap_or_else(|| foreground.clone());

    let mut ansi = Vec::with_capacity(16);
    for i in 0..16 {
        let key = format!("Ansi {} Color", i);
        let color = extract_color(root, &key).unwrap_or(if i < 8 {
            Iterm2Color {
                r: 0.5,
                g: 0.5,
                b: 0.5,
            }
        } else {
            Iterm2Color {
                r: 0.7,
                g: 0.7,
                b: 0.7,
            }
        });
        ansi.push(color);
    }

    Ok(Iterm2Scheme {
        background,
        foreground,
        selection_bg,
        selection_fg,
        ansi: ansi.try_into().unwrap(),
    })
}

impl Iterm2Scheme {
    fn to_colors_toml(&self) -> String {
        let bg = &self.background;
        let fg = self.foreground.ensure_contrast(bg, MIN_CONTRAST_RATIO);
        let dialog_bg = bg.blend(&self.ansi[8], 0.15);
        let border_color = bg.blend(&self.ansi[8], 0.4);

        let accent = self.ansi[4]
            .ensure_contrast(bg, MIN_CONTRAST_RATIO)
            .ensure_contrast(&dialog_bg, MIN_CONTRAST_RATIO);
        let accent_secondary = self.ansi[5]
            .ensure_contrast(bg, MIN_CONTRAST_RATIO)
            .ensure_contrast(&dialog_bg, MIN_CONTRAST_RATIO);
        let highlight = self.ansi[3].ensure_contrast(bg, MIN_CONTRAST_RATIO);
        let success = self.ansi[2].ensure_contrast(bg, MIN_CONTRAST_RATIO);
        let danger = self.ansi[1].ensure_contrast(bg, MIN_CONTRAST_RATIO);

        let muted = self.derive_muted_color(bg);
        let warning = self.derive_warning_color(bg);

        let graph_line = accent.clone();

        let selection_fg = self
            .selection_fg
            .ensure_contrast(&self.selection_bg, MIN_CONTRAST_RATIO);

        format!(
            r##"bg = "{}"
dialog_bg = "{}"
fg = "{}"
accent = "{}"
accent_secondary = "{}"
highlight = "{}"
muted = "{}"
success = "{}"
warning = "{}"
danger = "{}"
border = "{}"
selection_bg = "{}"
selection_fg = "{}"
graph_line = "{}""##,
            bg.to_hex(),
            dialog_bg.to_hex(),
            fg.to_hex(),
            accent.to_hex(),
            accent_secondary.to_hex(),
            highlight.to_hex(),
            muted.to_hex(),
            success.to_hex(),
            warning.to_hex(),
            danger.to_hex(),
            border_color.to_hex(),
            self.selection_bg.to_hex(),
            selection_fg.to_hex(),
            graph_line.to_hex(),
        )
    }

    fn derive_muted_color(&self, bg: &Iterm2Color) -> Iterm2Color {
        let candidates = [&self.ansi[8], &self.foreground.blend(bg, 0.5)];

        for candidate in candidates {
            let adjusted = candidate.ensure_contrast(bg, MIN_CONTRAST_RATIO);
            if adjusted.contrast_ratio(bg) >= MIN_CONTRAST_RATIO {
                return adjusted;
            }
        }

        self.foreground.blend(bg, 0.4)
    }

    fn derive_warning_color(&self, bg: &Iterm2Color) -> Iterm2Color {
        let candidates = [&self.ansi[11], &self.ansi[3], &self.ansi[9]];

        for candidate in candidates {
            let adjusted = candidate.ensure_contrast(bg, MIN_CONTRAST_RATIO);
            if adjusted.contrast_ratio(bg) >= MIN_CONTRAST_RATIO {
                return adjusted;
            }
        }

        self.ansi[3].ensure_contrast(bg, MIN_CONTRAST_RATIO)
    }
}

pub struct ImportResult {
    pub path: PathBuf,
    pub dark_source: Option<String>,
    pub light_source: Option<String>,
}

fn try_fetch_scheme(name: &str) -> Option<Iterm2Scheme> {
    let url = format!("{}/{}.itermcolors", ITERM2_REPO_URL, name);

    let response = ureq::get(&url).call().ok()?;

    let bytes = response.into_body().read_to_vec().ok()?;

    parse_scheme(&bytes).ok()
}

pub fn fetch_scheme(name: &str) -> Result<Iterm2Scheme, Iterm2Error> {
    let url = format!("{}/{}.itermcolors", ITERM2_REPO_URL, name);

    let response = ureq::get(&url)
        .call()
        .map_err(|e| Iterm2Error::NetworkError(e.to_string()))?;

    if response.status() == 404 {
        return Err(Iterm2Error::NotFound(format!(
            "Scheme '{}' not found. Browse available themes at: {}",
            name, ITERM2_GALLERY_URL
        )));
    }

    let bytes = response
        .into_body()
        .read_to_vec()
        .map_err(|e| Iterm2Error::NetworkError(e.to_string()))?;

    parse_scheme(&bytes)
}

#[derive(Debug, serde::Deserialize)]
struct GitHubFile {
    name: String,
    #[serde(rename = "type")]
    file_type: String,
}

pub fn list_available_schemes() -> Result<Vec<String>, Iterm2Error> {
    let response = ureq::get(ITERM2_API_URL)
        .header("User-Agent", "jolt-theme-importer")
        .call()
        .map_err(|e: ureq::Error| Iterm2Error::NetworkError(e.to_string()))?;

    let body = response
        .into_body()
        .read_to_string()
        .map_err(|e: ureq::Error| Iterm2Error::NetworkError(e.to_string()))?;

    let files: Vec<GitHubFile> =
        serde_json::from_str(&body).map_err(|e| Iterm2Error::ParseError(e.to_string()))?;

    let schemes: Vec<String> = files
        .into_iter()
        .filter(|f| f.file_type == "file" && f.name.ends_with(".itermcolors"))
        .map(|f| f.name.trim_end_matches(".itermcolors").to_string())
        .collect();

    Ok(schemes)
}

fn derive_base_name(name: &str) -> String {
    let lower = name.to_lowercase();

    for suffix in [" light", " dark", "-light", "-dark"] {
        if lower.ends_with(suffix) {
            return name[..name.len() - suffix.len()].to_string();
        }
    }

    name.to_string()
}

pub fn import_scheme(
    name: &str,
    custom_name: Option<&str>,
    themes_dir: &Path,
) -> Result<ImportResult, Iterm2Error> {
    let primary = fetch_scheme(name)?;
    let primary_variant = detect_variant(name);

    let mut dark_scheme: Option<Iterm2Scheme> = None;
    let mut light_scheme: Option<Iterm2Scheme> = None;
    let mut dark_source: Option<String> = None;
    let mut light_source: Option<String> = None;

    match primary_variant {
        SchemeVariant::Dark => {
            dark_scheme = Some(primary);
            dark_source = Some(name.to_string());

            if let Some(counterpart) = find_counterpart_name(name) {
                if let Some(light) = try_fetch_scheme(&counterpart) {
                    light_scheme = Some(light);
                    light_source = Some(counterpart);
                }
            }
        }
        SchemeVariant::Light => {
            light_scheme = Some(primary);
            light_source = Some(name.to_string());

            if let Some(counterpart) = find_counterpart_name(name) {
                if let Some(dark) = try_fetch_scheme(&counterpart) {
                    dark_scheme = Some(dark);
                    dark_source = Some(counterpart);
                }
            }
        }
        SchemeVariant::Unknown => {
            dark_scheme = Some(primary);
            dark_source = Some(name.to_string());

            for variant_name in find_variant_names(name) {
                if let Some(scheme) = try_fetch_scheme(&variant_name) {
                    let variant = detect_variant(&variant_name);
                    if variant == SchemeVariant::Light && light_scheme.is_none() {
                        light_scheme = Some(scheme);
                        light_source = Some(variant_name);
                        break;
                    }
                }
            }
        }
    }

    let base_name = custom_name
        .map(|s| s.to_string())
        .unwrap_or_else(|| derive_base_name(name));

    let file_name = base_name.to_lowercase().replace(' ', "-");

    let mut toml_content = format!("name = \"{}\"\n", base_name);

    if let Some(ref dark) = dark_scheme {
        toml_content.push_str("\n[dark]\n");
        toml_content.push_str(&dark.to_colors_toml());
        toml_content.push('\n');
    }

    if let Some(ref light) = light_scheme {
        toml_content.push_str("\n[light]\n");
        toml_content.push_str(&light.to_colors_toml());
        toml_content.push('\n');
    }

    std::fs::create_dir_all(themes_dir).map_err(|e| Iterm2Error::IoError(e.to_string()))?;

    let theme_path = themes_dir.join(format!("{}.toml", file_name));

    std::fs::write(&theme_path, toml_content).map_err(|e| Iterm2Error::IoError(e.to_string()))?;

    Ok(ImportResult {
        path: theme_path,
        dark_source,
        light_source,
    })
}

pub fn search_schemes(query: &str) -> Result<Vec<String>, Iterm2Error> {
    let all_schemes = list_available_schemes()?;
    let query_lower = query.to_lowercase();

    let matches: Vec<String> = all_schemes
        .into_iter()
        .filter(|s| s.to_lowercase().contains(&query_lower))
        .collect();

    Ok(matches)
}

pub fn find_variant_suggestions(
    name: &str,
    target_variant: SchemeVariant,
) -> Result<Vec<String>, Iterm2Error> {
    let base_name = derive_base_name(name);
    let all_schemes = list_available_schemes()?;
    let base_lower = base_name.to_lowercase();

    let mut suggestions: Vec<String> = all_schemes
        .into_iter()
        .filter(|s| {
            let s_lower = s.to_lowercase();
            if s_lower == name.to_lowercase() {
                return false;
            }

            let matches_base = s_lower.contains(&base_lower)
                || base_lower.contains(&derive_base_name(s).to_lowercase());

            if !matches_base {
                return false;
            }

            let variant = detect_variant(s);
            variant == target_variant || variant == SchemeVariant::Unknown
        })
        .collect();

    suggestions.sort();
    Ok(suggestions)
}

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

    #[test]
    fn test_color_to_hex() {
        let color = Iterm2Color {
            r: 1.0,
            g: 0.5,
            b: 0.0,
        };
        assert_eq!(color.to_hex(), "#ff8000");
    }

    #[test]
    fn test_color_blend() {
        let black = Iterm2Color {
            r: 0.0,
            g: 0.0,
            b: 0.0,
        };
        let white = Iterm2Color {
            r: 1.0,
            g: 1.0,
            b: 1.0,
        };
        let gray = black.blend(&white, 0.5);
        assert!((gray.r - 0.5).abs() < 0.01);
        assert!((gray.g - 0.5).abs() < 0.01);
        assert!((gray.b - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_detect_variant() {
        assert_eq!(detect_variant("Gruvbox Dark"), SchemeVariant::Dark);
        assert_eq!(detect_variant("Gruvbox Light"), SchemeVariant::Light);
        assert_eq!(detect_variant("Dracula"), SchemeVariant::Unknown);
        assert_eq!(detect_variant("One Dark"), SchemeVariant::Dark);
        assert_eq!(detect_variant("Solarized Light"), SchemeVariant::Light);
    }

    #[test]
    fn test_find_counterpart_name() {
        assert_eq!(
            find_counterpart_name("Gruvbox Dark"),
            Some("Gruvbox Light".to_string())
        );
        assert_eq!(
            find_counterpart_name("Gruvbox Light"),
            Some("Gruvbox Dark".to_string())
        );
        assert_eq!(
            find_counterpart_name("Gruvbox Dark Hard"),
            Some("Gruvbox Light Hard".to_string())
        );
        assert_eq!(find_counterpart_name("Dracula"), None);
    }

    #[test]
    fn test_derive_base_name() {
        assert_eq!(derive_base_name("Gruvbox Dark"), "Gruvbox");
        assert_eq!(derive_base_name("Gruvbox Light"), "Gruvbox");
        assert_eq!(derive_base_name("Gruvbox Dark Hard"), "Gruvbox Dark Hard");
        assert_eq!(derive_base_name("One Dark"), "One");
        assert_eq!(derive_base_name("Dracula"), "Dracula");
    }

    #[test]
    fn test_lookup_variant_pair() {
        let mocha = lookup_variant_pair("Catppuccin Mocha");
        assert!(mocha.is_some());
        assert_eq!(mocha.unwrap().1, "Catppuccin Latte");

        let latte = lookup_variant_pair("Catppuccin Latte");
        assert!(latte.is_some());
        assert_eq!(latte.unwrap().1, "Catppuccin Latte");

        assert_eq!(
            lookup_variant_pair("Tomorrow Night"),
            Some(("Tomorrow Night", "Tomorrow"))
        );
        assert_eq!(lookup_variant_pair("Nord"), Some(("Nord", "Nord Light")));
        assert_eq!(lookup_variant_pair("Dracula"), None);
    }

    #[test]
    fn test_find_counterpart_via_lookup() {
        assert_eq!(
            find_counterpart_name("Catppuccin Mocha"),
            Some("Catppuccin Latte".to_string())
        );
        let latte_counterpart = find_counterpart_name("Catppuccin Latte");
        assert!(latte_counterpart.is_some());
        assert!(latte_counterpart.unwrap().starts_with("Catppuccin"));

        assert_eq!(
            find_counterpart_name("Tomorrow Night"),
            Some("Tomorrow".to_string())
        );
        assert_eq!(
            find_counterpart_name("Nord"),
            Some("Nord Light".to_string())
        );
        assert_eq!(
            find_counterpart_name("Nord Light"),
            Some("Nord".to_string())
        );
    }

    #[test]
    fn test_detect_variant_via_lookup() {
        assert_eq!(detect_variant("Catppuccin Mocha"), SchemeVariant::Dark);
        assert_eq!(detect_variant("Catppuccin Latte"), SchemeVariant::Light);
        assert_eq!(detect_variant("Nord"), SchemeVariant::Dark);
        assert_eq!(detect_variant("Tomorrow"), SchemeVariant::Light);
    }
}