fresh-editor 0.3.7

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
//! Theme loading and registry.
//!
//! This module provides:
//! - `ThemeRegistry`: A pure data structure holding all loaded themes
//! - `ThemeLoader`: Scans and loads themes into a registry

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use super::types::{Theme, ThemeFile, ThemeInfo, BUILTIN_THEMES};

/// Normalize a theme name for consistent lookup and storage.
///
/// Converts to lowercase and replaces underscores and spaces with hyphens.
/// This ensures that theme names can be matched regardless of how they appear
/// in filenames vs. JSON content (e.g., "Catppuccin Mocha" matches "catppuccin-mocha").
pub fn normalize_theme_name(name: &str) -> String {
    name.to_lowercase().replace(['_', ' '], "-")
}

/// Expand `~`, `$VAR`, and `${VAR}` references in a config-supplied path.
///
/// Minimal shell-like expansion (no crate dependency). Unknown variables are
/// left literal so a typo surfaces as a resolution failure rather than
/// silently collapsing to an empty path. Used for theme config values so a
/// shared dotfiles repo can write `file://${HOME}/.config/fresh/themes/x.json`
/// and have it resolve correctly on any machine.
pub(crate) fn expand_env_vars(input: &str) -> String {
    let input = if let Some(rest) = input.strip_prefix('~') {
        match std::env::var("HOME") {
            Ok(home) => format!("{}{}", home, rest),
            Err(_) => input.to_string(),
        }
    } else {
        input.to_string()
    };

    let bytes = input.as_bytes();
    let mut out = String::with_capacity(input.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] != b'$' {
            out.push(bytes[i] as char);
            i += 1;
            continue;
        }
        // `$` at end of string — keep literal.
        if i + 1 >= bytes.len() {
            out.push('$');
            i += 1;
            continue;
        }
        if bytes[i + 1] == b'{' {
            if let Some(close) = input[i + 2..].find('}') {
                let name = &input[i + 2..i + 2 + close];
                match std::env::var(name) {
                    Ok(v) => out.push_str(&v),
                    Err(_) => out.push_str(&input[i..i + 2 + close + 1]),
                }
                i += 2 + close + 1;
                continue;
            }
            // Unterminated `${…` — keep literal.
            out.push('$');
            i += 1;
            continue;
        }
        // `$VAR` — consume ASCII alphanumerics and underscores.
        let start = i + 1;
        let mut end = start;
        while end < bytes.len() && (bytes[end].is_ascii_alphanumeric() || bytes[end] == b'_') {
            end += 1;
        }
        if end == start {
            out.push('$');
            i += 1;
            continue;
        }
        let name = &input[start..end];
        match std::env::var(name) {
            Ok(v) => out.push_str(&v),
            Err(_) => out.push_str(&input[i..end]),
        }
        i = end;
    }
    out
}

/// A registry holding all loaded themes.
///
/// This is a pure data structure - no I/O operations.
/// Use `ThemeLoader` to create and populate a registry.
///
/// Themes are keyed by their unique `ThemeInfo::key` (typically a repository
/// URL or `pack/name` path). Lookups fall back to matching by display name
/// for backward compatibility with configs that store just e.g. `"dark"`.
#[derive(Debug, Clone)]
pub struct ThemeRegistry {
    /// All loaded themes, keyed by ThemeInfo.key
    themes: HashMap<String, Theme>,
    /// Theme metadata for listing
    theme_list: Vec<ThemeInfo>,
    /// User themes directory — used to resolve relative-path config values
    /// (e.g. `"s-dark.json"`, `"packages/nord/dark.json"`) against the
    /// machine's themes dir, so configs stay portable across machines.
    themes_dir: Option<PathBuf>,
}

impl ThemeRegistry {
    /// Look up a theme by its config value (key, scheme URI, relative path,
    /// or legacy bare name). See [`ThemeRegistry::resolve_key`].
    pub fn get(&self, key_or_name: &str) -> Option<&Theme> {
        self.resolve_key(key_or_name)
            .and_then(|key| self.themes.get(&key))
    }

    /// Get a cloned theme by key or name.
    pub fn get_cloned(&self, key_or_name: &str) -> Option<Theme> {
        self.get(key_or_name).cloned()
    }

    /// Resolve a config-value to the canonical registry key.
    ///
    /// Accepted config forms (issue #1621):
    ///
    /// | Form | Example | Resolves to |
    /// |------|---------|-------------|
    /// | `builtin://NAME` | `builtin://dark` | built-in theme by name |
    /// | `file://PATH` (env-expanded) | `file://${HOME}/.config/fresh/themes/x.json` | exact user-theme key |
    /// | `http(s)://...` | `https://github.com/...#dark` | URL-packaged theme |
    /// | relative `.json` path | `s-dark.json`, `packages/nord/dark.json` | user theme under themes dir |
    /// | bare name (legacy) | `dark` | exact key, else normalized-name match |
    ///
    /// `$HOME`, `${HOME}`, `${XDG_CONFIG_HOME}` and a leading `~` are
    /// expanded before path matching so shared dotfile configs work on any
    /// machine. Returns an owned `String` because some forms (e.g. a relative
    /// path) must be reconstructed as an absolute `file://` key.
    pub fn resolve_key(&self, value: &str) -> Option<String> {
        // 1. Exact key match — fast path; covers already-canonical keys
        //    (repo URLs, `file://…` written as-is, plain built-in names).
        if self.themes.contains_key(value) {
            return Some(value.to_string());
        }

        // 2. `builtin://NAME` — look up a built-in by normalized name.
        if let Some(name) = value.strip_prefix("builtin://") {
            let normalized = normalize_theme_name(name);
            return self
                .theme_list
                .iter()
                .find(|info| info.pack.is_empty() && normalize_theme_name(&info.name) == normalized)
                .map(|info| info.key.clone());
        }

        // 3. `file://PATH` — env-expand and retry exact match.
        if let Some(raw_path) = value.strip_prefix("file://") {
            let expanded = expand_env_vars(raw_path);
            // Normalize forward slashes to the OS-native separator so the
            // candidate matches the registry key built from
            // `entry.path().display()` (backslashes on Windows). No-op on
            // Unix where `MAIN_SEPARATOR_STR == "/"`.
            let expanded_native = expanded.replace('/', std::path::MAIN_SEPARATOR_STR);
            let candidate = format!("file://{}", expanded_native);
            if self.themes.contains_key(&candidate) {
                return Some(candidate);
            }
            return None;
        }

        // 4. `http(s)://…` — URL-packaged theme. Only exact match is valid;
        //    don't fall through to name fallback (would mask typos).
        if value.starts_with("http://") || value.starts_with("https://") {
            return None;
        }

        // 5. Relative path (ends with `.json`) — resolve against themes dir.
        //    This is the portable form for user themes (`s-dark.json` or
        //    `packages/nord/dark.json`). Env-var expansion is applied for
        //    the rare case of a hand-edited path like `${HOME}/foo.json`.
        if value.ends_with(".json") {
            if let Some(themes_dir) = self.themes_dir.as_deref() {
                let expanded = expand_env_vars(value);
                // Registry keys are built from `entry.path().display()`,
                // which uses the OS-native separator (`\` on Windows).
                // Normalize the config input's forward slashes to match so
                // the HashMap lookup hits on Windows too. No-op on Unix
                // (MAIN_SEPARATOR_STR is `/`).
                let expanded_native = expanded.replace('/', std::path::MAIN_SEPARATOR_STR);
                let expanded_path = std::path::Path::new(&expanded_native);
                let abs = if expanded_path.is_absolute() {
                    expanded_path.to_path_buf()
                } else {
                    themes_dir.join(expanded_path)
                };
                let candidate = format!("file://{}", abs.display());
                if self.themes.contains_key(&candidate) {
                    return Some(candidate);
                }
            }
            return None;
        }

        // 6. Legacy bare name — keeps `"theme": "dark"` configs working.
        let normalized = normalize_theme_name(value);
        self.theme_list
            .iter()
            .find(|info| normalize_theme_name(&info.name) == normalized)
            .map(|info| info.key.clone())
    }

    /// Portable config form for the given theme, suitable for persisting to
    /// `config.json` and sharing across machines.
    ///
    /// - Built-ins  → `"builtin://NAME"`
    /// - User theme under themes dir → relative path (`"s-dark.json"` or
    ///   `"nord/dark.json"`)
    /// - User theme outside themes dir → `"file://{abs}"` (user can hand-edit
    ///   to use `${HOME}` / `${XDG_CONFIG_HOME}` if they want)
    /// - URL-packaged theme → repo-URL key kept as-is
    pub fn portable_form(&self, key: &str) -> Option<String> {
        let info = self.theme_list.iter().find(|i| i.key == key)?;

        // Built-in: empty pack, key is just the name.
        if info.pack.is_empty() {
            return Some(format!("builtin://{}", info.name));
        }

        // User theme (file:// key): rewrite to a path relative to themes_dir
        // when possible, so the config is portable across machines.
        if let Some(path_str) = info.key.strip_prefix("file://") {
            let path = std::path::Path::new(path_str);
            if let Some(themes_dir) = self.themes_dir.as_deref() {
                if let Ok(rel) = path.strip_prefix(themes_dir) {
                    // Normalize to forward slashes so the config is
                    // reproducible across OSes.
                    let rel_str = rel
                        .components()
                        .map(|c| c.as_os_str().to_string_lossy())
                        .collect::<Vec<_>>()
                        .join("/");
                    return Some(rel_str);
                }
            }
            // Outside themes dir — keep as absolute file:// URI.
            return Some(info.key.clone());
        }

        // Anything else (URL-packaged themes) is already portable.
        Some(info.key.clone())
    }

    /// List all available themes with metadata.
    pub fn list(&self) -> &[ThemeInfo] {
        &self.theme_list
    }

    /// Get all theme display names.
    pub fn names(&self) -> Vec<String> {
        self.theme_list.iter().map(|t| t.name.clone()).collect()
    }

    /// Check if a theme exists (by key or name).
    pub fn contains(&self, key_or_name: &str) -> bool {
        self.get(key_or_name).is_some()
    }

    /// Number of themes in the registry.
    pub fn len(&self) -> usize {
        self.themes.len()
    }

    /// Check if registry is empty.
    pub fn is_empty(&self) -> bool {
        self.themes.is_empty()
    }

    /// Convert all themes to a JSON map (key → serde_json::Value).
    ///
    /// Keyed by the unique registry key. Each value is the theme data with
    /// added `_key` and `_pack` metadata fields so plugins can distinguish
    /// themes and show display names.
    pub fn to_json_map(&self) -> HashMap<String, serde_json::Value> {
        use super::types::ThemeFile;

        let mut map = HashMap::new();
        for info in &self.theme_list {
            if let Some(theme) = self.themes.get(&info.key) {
                let theme_file: ThemeFile = theme.clone().into();
                if let Ok(mut v) = serde_json::to_value(theme_file) {
                    if let Some(obj) = v.as_object_mut() {
                        obj.insert("_key".to_string(), serde_json::json!(info.key));
                        obj.insert("_pack".to_string(), serde_json::json!(info.pack));
                    }
                    map.insert(info.key.clone(), v);
                }
            }
        }
        map
    }
}

/// Loads themes and creates a ThemeRegistry.
pub struct ThemeLoader {
    user_themes_dir: Option<PathBuf>,
}

impl ThemeLoader {
    /// Create a ThemeLoader with the given user themes directory.
    pub fn new(user_themes_dir: PathBuf) -> Self {
        Self {
            user_themes_dir: Some(user_themes_dir),
        }
    }

    /// Create a ThemeLoader for embedded themes only (no user themes).
    pub fn embedded_only() -> Self {
        Self {
            user_themes_dir: None,
        }
    }

    /// Get the user themes directory path.
    pub fn user_themes_dir(&self) -> Option<&Path> {
        self.user_themes_dir.as_deref()
    }

    /// Load all themes (embedded + user + packages + bundle dirs) into a registry.
    ///
    /// Pass `&[]` for `bundle_theme_dirs` if there are no bundle themes.
    /// Each bundle directory should contain a `package.json` with a `fresh.themes`
    /// array (same format as theme packages).
    pub fn load_all(&self, bundle_theme_dirs: &[PathBuf]) -> ThemeRegistry {
        let mut themes = HashMap::new();
        let mut theme_list = Vec::new();

        // Load all embedded themes (key = name for builtins)
        for builtin in BUILTIN_THEMES {
            if let Ok(theme_file) = serde_json::from_str::<ThemeFile>(builtin.json) {
                let theme: Theme = theme_file.into();
                let normalized = normalize_theme_name(builtin.name);
                let info = ThemeInfo::new(&normalized, builtin.pack);
                themes.insert(info.key.clone(), theme);
                theme_list.push(info);
            }
        }

        // Load user themes from ~/.config/fresh/themes/ (recursively)
        if let Some(ref user_dir) = self.user_themes_dir {
            self.scan_directory(user_dir, "user", None, &mut themes, &mut theme_list);
        }

        // Load theme packages from ~/.config/fresh/themes/packages/*/
        if let Some(ref user_dir) = self.user_themes_dir {
            let packages_dir = user_dir.join("packages");
            if packages_dir.exists() {
                if let Ok(entries) = std::fs::read_dir(&packages_dir) {
                    for entry in entries.flatten() {
                        let path = entry.path();
                        if path.is_dir() {
                            if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                                if !name.starts_with('.') {
                                    let manifest_path = path.join("package.json");
                                    if manifest_path.exists() {
                                        self.load_package_themes(
                                            &path,
                                            name,
                                            &mut themes,
                                            &mut theme_list,
                                        );
                                    } else {
                                        let pack_name = format!("pkg/{}", name);
                                        self.scan_directory(
                                            &path,
                                            &pack_name,
                                            None,
                                            &mut themes,
                                            &mut theme_list,
                                        );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Load themes from bundle packages
        for bundle_dir in bundle_theme_dirs {
            if let Some(name) = bundle_dir.file_name().and_then(|n| n.to_str()) {
                let manifest_path = bundle_dir.join("package.json");
                if manifest_path.exists() {
                    self.load_package_themes(bundle_dir, name, &mut themes, &mut theme_list);
                }
            }
        }

        ThemeRegistry {
            themes,
            theme_list,
            themes_dir: self.user_themes_dir.clone(),
        }
    }

    /// Read the `repository` field from a package.json manifest value.
    fn read_repository(manifest: &serde_json::Value) -> Option<String> {
        manifest
            .get("repository")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
    }

    /// Load themes from a package with package.json manifest.
    fn load_package_themes(
        &self,
        pkg_dir: &Path,
        pkg_name: &str,
        themes: &mut HashMap<String, Theme>,
        theme_list: &mut Vec<ThemeInfo>,
    ) {
        let manifest_path = pkg_dir.join("package.json");
        let manifest_content = match std::fs::read_to_string(&manifest_path) {
            Ok(c) => c,
            Err(_) => return,
        };

        let manifest: serde_json::Value = match serde_json::from_str(&manifest_content) {
            Ok(v) => v,
            Err(_) => return,
        };

        let repository = Self::read_repository(&manifest);
        let pack_name = format!("pkg/{}", pkg_name);

        // Check for fresh.themes array in manifest
        if let Some(fresh) = manifest.get("fresh") {
            if let Some(theme_entries) = fresh.get("themes").and_then(|t| t.as_array()) {
                for entry in theme_entries {
                    if let (Some(file), Some(name)) = (
                        entry.get("file").and_then(|f| f.as_str()),
                        entry.get("name").and_then(|n| n.as_str()),
                    ) {
                        let theme_path = pkg_dir.join(file);
                        if theme_path.exists() {
                            if let Ok(content) = std::fs::read_to_string(&theme_path) {
                                if let Ok(theme) = Theme::from_json(&content) {
                                    let normalized_name = normalize_theme_name(name);
                                    let info = if let Some(ref repo) = repository {
                                        ThemeInfo::with_key(
                                            &normalized_name,
                                            &pack_name,
                                            format!("{}#{}", repo, normalized_name),
                                        )
                                    } else {
                                        ThemeInfo::new(&normalized_name, &pack_name)
                                    };
                                    if !themes.contains_key(&info.key) {
                                        themes.insert(info.key.clone(), theme);
                                        theme_list.push(info);
                                    }
                                }
                            }
                        }
                    }
                }
                return;
            }
        }

        // Fallback: if no fresh.themes, scan for JSON files
        self.scan_directory(
            pkg_dir,
            &pack_name,
            repository.as_deref(),
            themes,
            theme_list,
        );
    }

    /// Recursively scan a directory for theme files.
    ///
    /// If `repository` is provided (from a package.json), it is used to form
    /// the registry key as `{repository}#{theme_name}`.
    fn scan_directory(
        &self,
        dir: &Path,
        pack: &str,
        repository: Option<&str>,
        themes: &mut HashMap<String, Theme>,
        theme_list: &mut Vec<ThemeInfo>,
    ) {
        let entries = match std::fs::read_dir(dir) {
            Ok(e) => e,
            Err(_) => return,
        };

        for entry in entries.flatten() {
            let path = entry.path();

            if path.is_dir() {
                let subdir_name = path.file_name().unwrap().to_string_lossy();

                // Skip "packages" subdirectory at top level - it's handled separately
                // by load_package_themes for proper package metadata
                if pack == "user" && subdir_name == "packages" {
                    continue;
                }

                let new_pack = if pack == "user" {
                    format!("user/{}", subdir_name)
                } else {
                    format!("{}/{}", pack, subdir_name)
                };
                self.scan_directory(&path, &new_pack, repository, themes, theme_list);
            } else if path.extension().is_some_and(|ext| ext == "json") {
                if let Ok(content) = std::fs::read_to_string(&path) {
                    if let Ok(theme) = Theme::from_json(&content) {
                        let name = normalize_theme_name(&theme.name);
                        let info = if let Some(repo) = repository {
                            ThemeInfo::with_key(&name, pack, format!("{}#{}", repo, name))
                        } else if pack.starts_with("user") {
                            // User-saved themes: use file:// URL as key
                            ThemeInfo::with_key(&name, pack, format!("file://{}", path.display()))
                        } else {
                            ThemeInfo::new(&name, pack)
                        };

                        // Only skip exact key duplicates
                        if themes.contains_key(&info.key) {
                            continue;
                        }

                        themes.insert(info.key.clone(), theme);
                        theme_list.push(info);
                    }
                }
            }
        }
    }
}

// Cursor color methods on Theme (no I/O for theme loading)
impl Theme {
    /// Set the terminal cursor color using OSC 12 escape sequence.
    /// This makes the hardware cursor visible on any background.
    pub fn set_terminal_cursor_color(&self) {
        use super::types::color_to_rgb;
        use std::io::Write;
        if let Some((r, g, b)) = color_to_rgb(self.cursor) {
            // OSC 12 sets cursor color: \x1b]12;#RRGGBB\x07
            // Best-effort terminal escape writes
            #[allow(clippy::let_underscore_must_use)]
            let _ = write!(
                std::io::stdout(),
                "\x1b]12;#{:02x}{:02x}{:02x}\x07",
                r,
                g,
                b
            );
            #[allow(clippy::let_underscore_must_use)]
            let _ = std::io::stdout().flush();
        }
    }

    /// Reset the terminal cursor color to default.
    pub fn reset_terminal_cursor_color() {
        use std::io::Write;
        // OSC 112 resets cursor color to default
        // Best-effort terminal escape writes
        #[allow(clippy::let_underscore_must_use)]
        let _ = write!(std::io::stdout(), "\x1b]112\x07");
        #[allow(clippy::let_underscore_must_use)]
        let _ = std::io::stdout().flush();
    }
}

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

    #[test]
    fn test_theme_registry_get() {
        let loader = ThemeLoader::embedded_only();
        let registry = loader.load_all(&[]);

        // Should find builtin themes
        assert!(registry.get("dark").is_some());
        assert!(registry.get("light").is_some());
        assert!(registry.get("high-contrast").is_some());

        // Name normalization: casing, underscores, spaces
        assert!(registry.get("Dark").is_some());
        assert!(registry.get("DARK").is_some());
        assert!(registry.get("high_contrast").is_some());
        assert!(registry.get("high contrast").is_some());

        // Non-existent
        assert!(registry.get("nonexistent-theme").is_none());
    }

    #[test]
    fn test_theme_registry_list() {
        let loader = ThemeLoader::embedded_only();
        let registry = loader.load_all(&[]);

        let list = registry.list();
        assert!(list.len() >= 7); // At least the builtin themes

        // Check some expected themes
        assert!(list.iter().any(|t| t.name == "dark"));
        assert!(list.iter().any(|t| t.name == "light"));
    }

    #[test]
    fn test_theme_registry_contains() {
        let loader = ThemeLoader::embedded_only();
        let registry = loader.load_all(&[]);

        assert!(registry.contains("dark"));
        assert!(registry.contains("Dark")); // normalized
        assert!(!registry.contains("nonexistent"));
    }

    #[test]
    fn test_theme_loader_load_all() {
        let loader = ThemeLoader::embedded_only();
        let registry = loader.load_all(&[]);

        // Should have loaded all embedded themes
        assert!(registry.len() >= 7); // 7 root themes (xscriptor moved to external repo)

        // Verify theme content is correct
        let dark = registry.get("dark").unwrap();
        assert_eq!(dark.name, "dark");
    }

    /// Test that custom themes in user themes directory are loaded and available.
    /// This is a regression test for the macOS bug where themes in ~/.config/fresh/themes/
    /// were not appearing in the "Select Theme" command because ThemeLoader was using
    /// the wrong directory path on macOS.
    #[test]
    fn test_custom_theme_loading_from_user_dir() {
        // Create isolated temp directory for this test
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let themes_dir = temp_dir.path().to_path_buf();

        // Create a custom theme file directly in the themes directory
        let theme_json = r#"{
            "name": "my-custom-theme",
            "editor": {},
            "ui": {},
            "search": {},
            "diagnostic": {},
            "syntax": {}
        }"#;
        std::fs::write(themes_dir.join("my-custom-theme.json"), theme_json)
            .expect("Failed to write theme file");

        // Load themes with the custom themes directory
        let loader = ThemeLoader::new(themes_dir.clone());
        let registry = loader.load_all(&[]);

        // Verify the custom theme is loaded
        assert!(
            registry.contains("my-custom-theme"),
            "Custom theme should be loaded from user themes directory"
        );
        assert!(
            registry.get("my-custom-theme").is_some(),
            "Custom theme should be retrievable"
        );

        // Verify it appears in the theme list (used for "Select Theme" menu)
        let theme_list = registry.list();
        assert!(
            theme_list.iter().any(|t| t.name == "my-custom-theme"),
            "Custom theme should appear in theme list for Select Theme menu"
        );

        // Verify the theme has the correct pack metadata
        let theme_info = theme_list
            .iter()
            .find(|t| t.name == "my-custom-theme")
            .unwrap();
        assert_eq!(
            theme_info.pack, "user",
            "Custom theme should have 'user' pack"
        );

        // Verify the theme is also available via generate_dynamic_items
        // (the function used for Select Theme menu items).
        // The "theme" arg should be the key (file:// URL for user themes).
        #[cfg(not(target_arch = "wasm32"))]
        {
            let menu_items = crate::config::generate_dynamic_items("copy_with_theme", &themes_dir);
            let theme_keys: Vec<_> = menu_items
                .iter()
                .filter_map(|item| match item {
                    crate::config::MenuItem::Action { args, .. } => args
                        .get("theme")
                        .map(|v| v.as_str().unwrap_or_default().to_string()),
                    _ => None,
                })
                .collect();
            assert!(
                theme_keys.iter().any(|k| k.contains("my-custom-theme")),
                "Custom theme key should appear in dynamic menu items, got: {:?}",
                theme_keys
            );
        }
    }

    /// Test that custom themes in a package directory (with package.json) are loaded.
    #[test]
    fn test_custom_theme_package_loading() {
        // Create isolated temp directory for this test
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let themes_dir = temp_dir.path().to_path_buf();

        // Create packages subdirectory
        let packages_dir = themes_dir.join("packages");
        let pkg_dir = packages_dir.join("my-theme-pack");
        std::fs::create_dir_all(&pkg_dir).expect("Failed to create package dir");

        // Create package.json manifest
        let manifest = r#"{
            "name": "my-theme-pack",
            "fresh": {
                "themes": [
                    { "name": "Packaged Theme", "file": "packaged-theme.json" }
                ]
            }
        }"#;
        std::fs::write(pkg_dir.join("package.json"), manifest)
            .expect("Failed to write package.json");

        // Create the theme file referenced in package.json
        let theme_json = r#"{
            "name": "packaged-theme",
            "editor": {},
            "ui": {},
            "search": {},
            "diagnostic": {},
            "syntax": {}
        }"#;
        std::fs::write(pkg_dir.join("packaged-theme.json"), theme_json)
            .expect("Failed to write theme file");

        // Load themes
        let loader = ThemeLoader::new(themes_dir);
        let registry = loader.load_all(&[]);

        // Verify the packaged theme is loaded (name is normalized from "Packaged Theme")
        assert!(
            registry.contains("packaged-theme"),
            "Packaged theme should be loaded"
        );

        // Verify it appears in the theme list with correct pack name
        let theme_list = registry.list();
        let theme_info = theme_list
            .iter()
            .find(|t| t.name == "packaged-theme")
            .expect("Packaged theme should be in theme list");
        assert_eq!(
            theme_info.pack, "pkg/my-theme-pack",
            "Packaged theme should have correct pack name"
        );
    }

    #[test]
    fn test_normalize_theme_name() {
        assert_eq!(normalize_theme_name("dark"), "dark");
        assert_eq!(normalize_theme_name("Dark"), "dark");
        assert_eq!(normalize_theme_name("high_contrast"), "high-contrast");
        assert_eq!(normalize_theme_name("Catppuccin Mocha"), "catppuccin-mocha");
        assert_eq!(normalize_theme_name("My_Custom Theme"), "my-custom-theme");
        assert_eq!(normalize_theme_name("SOLARIZED_DARK"), "solarized-dark");
    }

    /// Regression test for #1001: theme whose JSON "name" field differs from the
    /// filename (e.g., filename "catppuccin-mocha.json" but JSON name "Catppuccin Mocha")
    /// should be findable by either name after normalization.
    #[test]
    fn test_theme_name_mismatch_json_vs_filename() {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let themes_dir = temp_dir.path().to_path_buf();

        // Simulate a theme where the JSON name has spaces/mixed case
        // but the filename uses hyphens (common for community themes)
        let theme_json = r#"{
            "name": "Catppuccin Mocha",
            "editor": {},
            "ui": {},
            "search": {},
            "diagnostic": {},
            "syntax": {}
        }"#;
        std::fs::write(themes_dir.join("catppuccin-mocha.json"), theme_json)
            .expect("Failed to write theme file");

        let loader = ThemeLoader::new(themes_dir);
        let registry = loader.load_all(&[]);

        // Should be findable by the normalized filename
        assert!(
            registry.contains("catppuccin-mocha"),
            "Theme should be found by normalized filename"
        );

        // Should also be findable by the JSON name (spaces normalized to hyphens)
        assert!(
            registry.contains("Catppuccin Mocha"),
            "Theme should be found by JSON name with spaces (normalized to hyphens)"
        );

        // Should also be findable with mixed casing
        assert!(
            registry.contains("CATPPUCCIN-MOCHA"),
            "Theme should be found regardless of casing"
        );

        // The registry key should be the normalized form
        let theme_list = registry.list();
        let theme_info = theme_list
            .iter()
            .find(|t| t.name == "catppuccin-mocha")
            .expect("Theme should appear with normalized name in theme list");
        assert_eq!(theme_info.pack, "user");
    }

    /// Test that themes in subdirectories of the user themes directory are loaded.
    #[test]
    fn test_custom_theme_in_subdirectory() {
        // Create isolated temp directory for this test
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let themes_dir = temp_dir.path().to_path_buf();

        // Create a subdirectory
        let subdir = themes_dir.join("my-collection");
        std::fs::create_dir_all(&subdir).expect("Failed to create subdir");

        // Create a theme in the subdirectory
        let theme_json = r#"{
            "name": "nested-theme",
            "editor": {},
            "ui": {},
            "search": {},
            "diagnostic": {},
            "syntax": {}
        }"#;
        std::fs::write(subdir.join("nested-theme.json"), theme_json)
            .expect("Failed to write theme file");

        // Load themes
        let loader = ThemeLoader::new(themes_dir);
        let registry = loader.load_all(&[]);

        // Verify the nested theme is loaded
        assert!(
            registry.contains("nested-theme"),
            "Theme in subdirectory should be loaded"
        );

        // Verify pack name includes the subdirectory
        let theme_list = registry.list();
        let theme_info = theme_list
            .iter()
            .find(|t| t.name == "nested-theme")
            .expect("Nested theme should be in theme list");
        assert_eq!(
            theme_info.pack, "user/my-collection",
            "Nested theme should have subdirectory in pack name"
        );
    }

    /// Regression test for #1621: a `config.json` shared via a dotfiles repo
    /// must be able to reference user themes without baking in the absolute
    /// `file://` path of the author's machine. Accepted portable forms:
    ///   - `builtin://NAME` for built-ins (disambiguates against same-named
    ///     user themes)
    ///   - relative paths like `s-dark.json` or `nord/dark.json` resolved
    ///     from the themes dir
    ///   - `file://` URIs with `~` / `${HOME}` / `$VAR` expansion so the
    ///     same config works on any machine
    ///   - legacy bare names (e.g. `"dark"`) keep resolving to a built-in
    #[test]
    fn test_resolve_key_portable_config_forms_for_user_themes() {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let themes_dir = temp_dir.path().to_path_buf();

        // User theme whose name collides with built-in `dark`.
        let user_dark = r#"{
            "name": "dark",
            "editor": {},
            "ui": {},
            "search": {},
            "diagnostic": {},
            "syntax": {}
        }"#;
        std::fs::write(themes_dir.join("dark.json"), user_dark)
            .expect("Failed to write user dark theme");

        // User theme in a subdirectory (simulates a shipped theme pack).
        let subdir = themes_dir.join("my-collection");
        std::fs::create_dir_all(&subdir).expect("Failed to create subdir");
        let nested = r#"{
            "name": "s-dark",
            "editor": {},
            "ui": {},
            "search": {},
            "diagnostic": {},
            "syntax": {}
        }"#;
        std::fs::write(subdir.join("s-dark.json"), nested).expect("Failed to write nested theme");

        let loader = ThemeLoader::new(themes_dir.clone());
        let registry = loader.load_all(&[]);

        // 1. `builtin://dark` disambiguates against the user theme.
        assert_eq!(
            registry.resolve_key("builtin://dark").as_deref(),
            Some("dark"),
            "`builtin://dark` must resolve to the built-in"
        );

        // 2. Relative path resolves against the themes dir — the user theme,
        //    not the built-in.
        let user_key = registry
            .resolve_key("dark.json")
            .expect("`dark.json` should resolve");
        assert!(
            user_key.starts_with("file://") && user_key.ends_with("dark.json"),
            "`dark.json` must resolve to the user theme file, got: {}",
            user_key
        );
        let theme = registry
            .get("dark.json")
            .expect("theme should be retrievable by relative path");
        assert_eq!(theme.name, "dark");

        // 3. Nested relative path with subdirectory.
        let nested_key = registry
            .resolve_key("my-collection/s-dark.json")
            .expect("nested relative path should resolve");
        assert!(
            nested_key.starts_with("file://") && nested_key.contains("my-collection"),
            "nested path should resolve under themes dir, got: {}",
            nested_key
        );

        // 4. `file://` with `${VAR}` expansion: we route through a test-owned
        //    env var so the expansion is exercised deterministically.
        std::env::set_var(
            "FRESH_TEST_THEMES_ROOT",
            themes_dir.to_string_lossy().to_string(),
        );
        let uri = "file://${FRESH_TEST_THEMES_ROOT}/dark.json";
        let resolved = registry
            .resolve_key(uri)
            .expect("env-var-expanded file:// URI should resolve");
        assert_eq!(
            resolved, user_key,
            "env-expanded URI should match user theme"
        );
        std::env::remove_var("FRESH_TEST_THEMES_ROOT");

        // 5. Legacy bare name still resolves to a built-in.
        assert_eq!(
            registry.resolve_key("high-contrast").as_deref(),
            Some("high-contrast"),
            "legacy bare-name config must keep working"
        );

        // 6. Unknown reference yields None (no fuzzy masking of typos).
        assert!(registry.resolve_key("does-not-exist").is_none());
        assert!(registry.resolve_key("builtin://no-such-theme").is_none());
        assert!(registry.resolve_key("missing.json").is_none());
    }

    /// `portable_form` round-trips a theme's registry key into the value
    /// that should be persisted to `config.json`. Resolving the persisted
    /// value must find the same theme again.
    #[test]
    fn test_portable_form_round_trip() {
        let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
        let themes_dir = temp_dir.path().to_path_buf();
        let theme_json = r#"{
            "name": "s-dark",
            "editor": {},
            "ui": {},
            "search": {},
            "diagnostic": {},
            "syntax": {}
        }"#;
        std::fs::write(themes_dir.join("s-dark.json"), theme_json).expect("Failed to write theme");
        let loader = ThemeLoader::new(themes_dir.clone());
        let registry = loader.load_all(&[]);

        // Built-in portable form is `builtin://NAME`.
        let builtin_portable = registry
            .portable_form("dark")
            .expect("built-in should have a portable form");
        assert_eq!(builtin_portable, "builtin://dark");
        assert_eq!(
            registry.resolve_key(&builtin_portable).as_deref(),
            Some("dark")
        );

        // User-theme portable form is a relative path — never an absolute
        // `file://` URI with the author's home dir.
        let user_info = registry
            .list()
            .iter()
            .find(|i| i.name == "s-dark")
            .expect("user theme should be listed")
            .clone();
        let user_portable = registry
            .portable_form(&user_info.key)
            .expect("user theme should have a portable form");
        assert_eq!(
            user_portable, "s-dark.json",
            "user theme must persist as a relative path, got: {}",
            user_portable
        );
        assert!(
            !user_portable.contains(themes_dir.to_string_lossy().as_ref()),
            "portable form must not embed the absolute themes dir path"
        );
        // And it resolves back to the same key.
        assert_eq!(registry.resolve_key(&user_portable), Some(user_info.key));
    }

    #[test]
    fn test_expand_env_vars() {
        std::env::set_var("FRESH_TEST_VAR_A", "/foo/bar");
        std::env::set_var("FRESH_TEST_VAR_B", "baz");
        assert_eq!(expand_env_vars("${FRESH_TEST_VAR_A}/x"), "/foo/bar/x");
        assert_eq!(expand_env_vars("$FRESH_TEST_VAR_A/x"), "/foo/bar/x");
        assert_eq!(expand_env_vars("a/${FRESH_TEST_VAR_B}/c"), "a/baz/c");
        // Unknown vars remain literal.
        assert_eq!(
            expand_env_vars("${FRESH_NO_SUCH_VAR_XYZ}/x"),
            "${FRESH_NO_SUCH_VAR_XYZ}/x"
        );
        // Unterminated `${` stays literal.
        assert_eq!(expand_env_vars("${oops/x"), "${oops/x");
        // `~` is expanded iff HOME is set (which it is in the test harness).
        if let Ok(home) = std::env::var("HOME") {
            assert_eq!(expand_env_vars("~/foo"), format!("{}/foo", home));
        }
        std::env::remove_var("FRESH_TEST_VAR_A");
        std::env::remove_var("FRESH_TEST_VAR_B");
    }

    #[test]
    fn test_to_json_map() {
        let loader = ThemeLoader::embedded_only();
        let registry = loader.load_all(&[]);

        let json_map = registry.to_json_map();

        // Should contain all themes
        assert_eq!(json_map.len(), registry.len());

        // Each entry should be a valid JSON object with a "name" field
        let dark = json_map
            .get("dark")
            .expect("dark theme should be in json map");
        assert!(dark.is_object(), "theme should serialize to a JSON object");
        assert_eq!(
            dark.get("name").and_then(|v| v.as_str()),
            Some("dark"),
            "theme JSON should have correct name"
        );

        // Should have the expected section keys
        assert!(dark.get("editor").is_some(), "should have editor section");
        assert!(dark.get("ui").is_some(), "should have ui section");
        assert!(dark.get("syntax").is_some(), "should have syntax section");

        // Should have metadata fields
        assert_eq!(
            dark.get("_key").and_then(|v| v.as_str()),
            Some("dark"),
            "theme JSON should have _key metadata"
        );
        assert!(
            dark.get("_pack").is_some(),
            "theme JSON should have _pack metadata"
        );
    }
}