lios 0.1.27

A polished GTK4/VTE Linux terminal with baked-in themes, glass backgrounds, opt-in GPU backends, and desktop launcher install.
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
use serde::Deserialize;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use toml_edit::{DocumentMut, Item, Table, value};

use crate::terminal::{DEFAULT_IMAGE_TERMINAL_OPACITY, MAX_SCROLLBACK_LINES, TerminalConfig};
use crate::theme::TerminalTheme;

const SAMPLE_CONFIG: &str = r##"[window]
title = "Lios"
width = 960
height = 640
decorated = true
renderer = "cairo" # cairo disables GPU; auto, gl, or vulkan enable GPU
opacity = 1.00 # master/full-window opacity, 0.0 to 1.0

[terminal]
font = "Monospace 12"
scrollback_lines = 1000

[theme]
name = "xfce"
# foreground = "#ffffff"
# background = "#000000"
# cursor = "#ffffff"

[background]
# image = "/home/example/Pictures/wallpaper.jpg"
image_opacity = 1.00
terminal_opacity = 1.00
# Use terminal_opacity = 0.50 with an image, or lower values for no-image glass.
# overlay_color = "#7c3aed"
overlay_opacity = 0.00
random_overlay = true
"##;

#[derive(Debug, Clone, Default)]
pub struct AppSettings {
    pub window: WindowSettings,
    pub terminal: TerminalConfig,
}

#[derive(Debug, Clone)]
pub struct WindowSettings {
    pub title: String,
    pub default_width: i32,
    pub default_height: i32,
    pub decorated: bool,
    pub renderer: RendererPreference,
    pub opacity: f64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RendererPreference {
    Auto,
    Gl,
    Vulkan,
    Cairo,
}

#[derive(Debug, Clone, Default)]
pub struct ConfigOverrides {
    pub renderer: Option<String>,
    pub theme_name: Option<String>,
    pub font: Option<String>,
    pub background_image: Option<PathBuf>,
    pub background_image_opacity: Option<f64>,
    pub terminal_opacity: Option<f64>,
    pub overlay_color: Option<String>,
    pub overlay_opacity: Option<f64>,
    pub random_overlay: Option<bool>,
    pub decorated: Option<bool>,
    pub window_opacity: Option<f64>,
}

#[derive(Debug, Clone)]
pub enum ConfigCommand {
    Path,
    Sample,
    Init {
        path: Option<PathBuf>,
        force: bool,
    },
    Show {
        path: Option<PathBuf>,
    },
    Set {
        path: Option<PathBuf>,
        key: String,
        value: String,
    },
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct FileConfig {
    window: FileWindow,
    terminal: FileTerminal,
    theme: FileTheme,
    background: FileBackground,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct FileWindow {
    title: Option<String>,
    width: Option<i32>,
    height: Option<i32>,
    decorated: Option<bool>,
    renderer: Option<String>,
    opacity: Option<f64>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct FileTerminal {
    font: Option<String>,
    scrollback_lines: Option<i64>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct FileTheme {
    name: Option<String>,
    foreground: Option<String>,
    background: Option<String>,
    cursor: Option<String>,
    palette: Option<Vec<String>>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(default)]
struct FileBackground {
    image: Option<PathBuf>,
    image_opacity: Option<f64>,
    terminal_opacity: Option<f64>,
    overlay_color: Option<String>,
    overlay_opacity: Option<f64>,
    random_overlay: Option<bool>,
}

impl AppSettings {
    pub fn load(config_path: Option<PathBuf>, overrides: ConfigOverrides) -> Result<Self, String> {
        let mut settings = Self::default();

        if let Some(path) = config_path.or_else(default_existing_config_path) {
            let file = read_config_file(path)?;
            settings.apply_file(file)?;
        }

        settings.apply_overrides(overrides)?;
        Ok(settings)
    }

    pub fn persist(&self, path: &Path) -> Result<(), String> {
        let mut doc = DocumentMut::new();

        doc["window"] = Item::Table(Table::new());
        doc["window"]["title"] = value(self.window.title.clone());
        doc["window"]["width"] = value(i64::from(self.window.default_width));
        doc["window"]["height"] = value(i64::from(self.window.default_height));
        doc["window"]["decorated"] = value(self.window.decorated);
        doc["window"]["renderer"] = value(self.window.renderer.as_config());
        doc["window"]["opacity"] = value(self.window.opacity);

        doc["terminal"] = Item::Table(Table::new());
        doc["terminal"]["font"] = value(self.terminal.font.clone());
        doc["terminal"]["scrollback_lines"] = value(self.terminal.scrollback_lines);

        doc["theme"] = Item::Table(Table::new());
        doc["theme"]["name"] = value(self.terminal.theme_name.clone());

        doc["background"] = Item::Table(Table::new());
        if let Some(image) = &self.terminal.background.image {
            doc["background"]["image"] = value(image.to_string_lossy().to_string());
        }
        doc["background"]["image_opacity"] = value(self.terminal.background.image_opacity);
        doc["background"]["terminal_opacity"] = value(self.terminal.background.terminal_opacity);
        if let Some(color) = &self.terminal.background.overlay_color {
            doc["background"]["overlay_color"] = value(color_to_hex(color));
        }
        doc["background"]["overlay_opacity"] = value(self.terminal.background.overlay_opacity);
        doc["background"]["random_overlay"] = value(self.terminal.background.random_overlay);

        write_document(path, doc)
    }

    fn apply_file(&mut self, file: FileConfig) -> Result<(), String> {
        let image_was_configured = file.background.image.is_some();
        let terminal_opacity_was_configured = file.background.terminal_opacity.is_some();

        if let Some(title) = file.window.title {
            self.window.title = title;
        }
        if let Some(width) = file.window.width {
            self.window.default_width = positive_i32(width, "window.width")?;
        }
        if let Some(height) = file.window.height {
            self.window.default_height = positive_i32(height, "window.height")?;
        }
        if let Some(decorated) = file.window.decorated {
            self.window.decorated = decorated;
        }
        if let Some(renderer) = file.window.renderer {
            self.window.renderer = RendererPreference::parse(&renderer)?;
        }
        if let Some(opacity) = file.window.opacity {
            self.window.opacity = unit_interval(opacity, "window.opacity")?;
        }

        if let Some(font) = file.terminal.font {
            self.terminal.font = font;
        }
        if let Some(lines) = file.terminal.scrollback_lines {
            self.terminal.scrollback_lines =
                bounded_scrollback(lines, "terminal.scrollback_lines")?;
        }

        if let Some(name) = file.theme.name {
            let canonical_name = TerminalTheme::canonical_name(&name)?;
            self.terminal.theme_name = canonical_name.to_string();
            self.terminal.theme = TerminalTheme::named(canonical_name)?;
        }
        self.terminal.theme.apply_overrides(
            file.theme.foreground.as_deref(),
            file.theme.background.as_deref(),
            file.theme.cursor.as_deref(),
            file.theme.palette.as_deref(),
        )?;

        if let Some(image) = file.background.image {
            self.terminal.background.image = Some(expand_user_path(image));
        }
        if let Some(opacity) = file.background.image_opacity {
            self.terminal.background.image_opacity =
                unit_interval(opacity, "background.image_opacity")?;
        }
        if let Some(opacity) = file.background.terminal_opacity {
            self.terminal.background.terminal_opacity =
                unit_interval(opacity, "background.terminal_opacity")?;
        }
        if let Some(color) = file.background.overlay_color {
            self.terminal.background.overlay_color = Some(TerminalTheme::parse_color(&color)?);
        }
        if let Some(opacity) = file.background.overlay_opacity {
            self.terminal.background.overlay_opacity =
                unit_interval(opacity, "background.overlay_opacity")?;
        }
        if let Some(random_overlay) = file.background.random_overlay {
            self.terminal.background.random_overlay = random_overlay;
        }
        if image_was_configured
            && (!terminal_opacity_was_configured
                || self.terminal.background.terminal_opacity >= 0.999)
        {
            self.terminal.background.terminal_opacity = DEFAULT_IMAGE_TERMINAL_OPACITY;
        }

        Ok(())
    }

    fn apply_overrides(&mut self, overrides: ConfigOverrides) -> Result<(), String> {
        let image_was_overridden = overrides.background_image.is_some();
        let terminal_opacity_was_overridden = overrides.terminal_opacity.is_some();

        if let Some(renderer) = overrides.renderer {
            self.window.renderer = RendererPreference::parse(&renderer)?;
        }
        if let Some(theme_name) = overrides.theme_name {
            let canonical_name = TerminalTheme::canonical_name(&theme_name)?;
            self.terminal.theme_name = canonical_name.to_string();
            self.terminal.theme = TerminalTheme::named(canonical_name)?;
        }
        if let Some(font) = overrides.font {
            self.terminal.font = font;
        }
        if let Some(image) = overrides.background_image {
            self.terminal.background.image = Some(expand_user_path(image));
        }
        if let Some(opacity) = overrides.background_image_opacity {
            self.terminal.background.image_opacity =
                unit_interval(opacity, "--background-image-opacity")?;
        }
        if let Some(opacity) = overrides.terminal_opacity {
            self.terminal.background.terminal_opacity =
                unit_interval(opacity, "--background-opacity")?;
        }
        if let Some(color) = overrides.overlay_color {
            self.terminal.background.overlay_color = Some(TerminalTheme::parse_color(&color)?);
        }
        if let Some(opacity) = overrides.overlay_opacity {
            self.terminal.background.overlay_opacity = unit_interval(opacity, "--overlay-opacity")?;
        }
        if let Some(random_overlay) = overrides.random_overlay {
            self.terminal.background.random_overlay = random_overlay;
        }
        if let Some(decorated) = overrides.decorated {
            self.window.decorated = decorated;
        }
        if let Some(opacity) = overrides.window_opacity {
            self.window.opacity = unit_interval(opacity, "--opacity")?;
        }
        if image_was_overridden
            && !terminal_opacity_was_overridden
            && self.terminal.background.terminal_opacity >= 0.999
        {
            self.terminal.background.terminal_opacity = DEFAULT_IMAGE_TERMINAL_OPACITY;
        }

        Ok(())
    }
}

impl Default for WindowSettings {
    fn default() -> Self {
        Self {
            title: "Lios".to_string(),
            default_width: 900,
            default_height: 620,
            decorated: true,
            renderer: RendererPreference::Cairo,
            opacity: 1.0,
        }
    }
}

impl RendererPreference {
    pub const GPU_MODE_NAMES: [&'static str; 3] = ["auto", "gl", "vulkan"];

    pub fn parse(value: &str) -> Result<Self, String> {
        match value.trim().to_ascii_lowercase().as_str() {
            "auto" | "default" => Ok(Self::Auto),
            "gl" | "opengl" | "gpu" => Ok(Self::Gl),
            "vulkan" | "vk" => Ok(Self::Vulkan),
            "cairo" | "software" | "cpu" => Ok(Self::Cairo),
            _ => Err(format!(
                "unknown renderer '{value}'. Use auto, gl, vulkan, or cairo"
            )),
        }
    }

    pub fn parse_gpu_mode(value: &str) -> Result<Self, String> {
        match value.trim().to_ascii_lowercase().as_str() {
            "auto" | "default" => Ok(Self::Auto),
            "gl" | "opengl" => Ok(Self::Gl),
            "vulkan" | "vk" => Ok(Self::Vulkan),
            _ => Err(format!(
                "unknown GPU mode '{value}'. Use auto, gl, or vulkan"
            )),
        }
    }

    pub fn from_gpu_settings(enabled: bool, mode: &str) -> Result<Self, String> {
        if enabled {
            Self::parse_gpu_mode(mode)
        } else {
            Ok(Self::Cairo)
        }
    }

    pub fn gpu_enabled(self) -> bool {
        !matches!(self, Self::Cairo)
    }

    pub fn gpu_mode_config(self) -> &'static str {
        match self {
            Self::Auto | Self::Cairo => "auto",
            Self::Gl => "gl",
            Self::Vulkan => "vulkan",
        }
    }

    pub fn as_config(self) -> &'static str {
        match self {
            Self::Auto => "auto",
            Self::Gl => "gl",
            Self::Vulkan => "vulkan",
            Self::Cairo => "cairo",
        }
    }

    pub fn gsk_renderer(self) -> Option<&'static str> {
        match self {
            Self::Auto => None,
            Self::Gl => Some("gl"),
            Self::Vulkan => Some("vulkan"),
            Self::Cairo => Some("cairo"),
        }
    }
}

pub fn run_config_command(command: ConfigCommand) -> Result<String, String> {
    match command {
        ConfigCommand::Path => default_config_path()
            .map(|path| format!("{}\n", path.display()))
            .ok_or_else(|| {
                "unable to determine config path; set HOME or XDG_CONFIG_HOME".to_string()
            }),
        ConfigCommand::Sample => Ok(sample_config().to_string()),
        ConfigCommand::Init { path, force } => {
            let path = config_path_for_write(path)?;
            if path.exists() && !force {
                return Err(format!(
                    "config already exists at '{}'; use --force to overwrite",
                    path.display()
                ));
            }

            write_text(&path, sample_config())?;
            Ok(format!("wrote {}\n", path.display()))
        }
        ConfigCommand::Show { path } => {
            let path = config_path_for_write(path)?;
            fs::read_to_string(&path)
                .map_err(|error| format!("failed to read config '{}': {error}", path.display()))
        }
        ConfigCommand::Set { path, key, value } => {
            let path = config_path_for_write(path)?;
            set_config_value(&path, &key, &value)?;
            Ok(format!("set {key} in {}\n", path.display()))
        }
    }
}

pub fn sample_config() -> &'static str {
    SAMPLE_CONFIG
}

pub fn default_config_path() -> Option<PathBuf> {
    if let Ok(config_home) = env::var("XDG_CONFIG_HOME") {
        if !config_home.is_empty() {
            return Some(PathBuf::from(config_home).join("lios/config.toml"));
        }
    }

    env::var("HOME")
        .ok()
        .filter(|home| !home.is_empty())
        .map(|home| PathBuf::from(home).join(".config/lios/config.toml"))
}

pub fn config_path_for_write(path: Option<PathBuf>) -> Result<PathBuf, String> {
    path.or_else(default_config_path)
        .ok_or_else(|| "unable to determine config path; set HOME or XDG_CONFIG_HOME".to_string())
}

pub fn expand_user_path(path: PathBuf) -> PathBuf {
    let Some(text) = path.to_str() else {
        return path;
    };
    let Some(home) = env::var_os("HOME") else {
        return path;
    };

    if text == "~" {
        PathBuf::from(home)
    } else if let Some(rest) = text.strip_prefix("~/") {
        PathBuf::from(home).join(rest)
    } else {
        path
    }
}

fn default_existing_config_path() -> Option<PathBuf> {
    default_config_path().filter(|path| path.exists())
}

fn read_config_file(path: PathBuf) -> Result<FileConfig, String> {
    let text = fs::read_to_string(&path)
        .map_err(|error| format!("failed to read config '{}': {error}", path.display()))?;

    toml::from_str(&text)
        .map_err(|error| format!("failed to parse config '{}': {error}", path.display()))
}

fn set_config_value(path: &Path, key: &str, raw_value: &str) -> Result<(), String> {
    let mut doc = read_document(path)?;
    let (section, field) = config_key_path(key)?;
    ensure_table(&mut doc, section);

    doc[section][field] = match (section, field) {
        ("theme", "name") => {
            let canonical_name = TerminalTheme::canonical_name(raw_value)?;
            value(canonical_name)
        }
        ("window", "renderer") => {
            let renderer = renderer_from_config_value(key, raw_value)?;
            value(renderer.as_config())
        }
        ("background", "overlay_color") => {
            TerminalTheme::parse_color(raw_value)?;
            value(raw_value)
        }
        ("window", "decorated") | ("background", "random_overlay") => value(parse_bool(raw_value)?),
        ("terminal", "scrollback_lines") => {
            let parsed = raw_value
                .parse::<i64>()
                .map_err(|_| format!("{key} requires an integer"))?;
            value(bounded_scrollback(parsed, key)?)
        }
        ("window", "width") | ("window", "height") => value(
            raw_value
                .parse::<i64>()
                .map_err(|_| format!("{key} requires an integer"))?,
        ),
        ("window", "opacity")
        | ("background", "image_opacity")
        | ("background", "terminal_opacity")
        | ("background", "overlay_opacity") => {
            let parsed = raw_value
                .parse::<f64>()
                .map_err(|_| format!("{key} requires a number"))?;
            if !(0.0..=1.0).contains(&parsed) {
                return Err(format!("{key} must be between 0.0 and 1.0"));
            }
            value(parsed)
        }
        _ => value(raw_value),
    };

    write_document(path, doc)
}

fn config_key_path(key: &str) -> Result<(&'static str, &'static str), String> {
    match key.trim().to_ascii_lowercase().replace('-', "_").as_str() {
        "theme" | "theme.name" | "theme_name" => Ok(("theme", "name")),
        "font" | "terminal.font" => Ok(("terminal", "font")),
        "scrollback" | "scrollback_lines" | "terminal.scrollback_lines" => {
            Ok(("terminal", "scrollback_lines"))
        }
        "title" | "window.title" => Ok(("window", "title")),
        "width" | "window.width" => Ok(("window", "width")),
        "height" | "window.height" => Ok(("window", "height")),
        "renderer"
        | "gpu"
        | "gpu_acceleration"
        | "gpu_mode"
        | "window.renderer"
        | "window.gpu"
        | "window.gpu_acceleration"
        | "window.gpu_mode" => Ok(("window", "renderer")),
        "opacity" | "window.opacity" | "window_opacity" | "total_opacity" | "master_opacity" => {
            Ok(("window", "opacity"))
        }
        "topbar" | "titlebar" | "decorated" | "window.decorated" => Ok(("window", "decorated")),
        "background_image" | "background.image" | "background_image_path" => {
            Ok(("background", "image"))
        }
        "background_image_opacity" | "background.image_opacity" => {
            Ok(("background", "image_opacity"))
        }
        "background_opacity" | "terminal_opacity" | "background.terminal_opacity" => {
            Ok(("background", "terminal_opacity"))
        }
        "overlay_color" | "background.overlay_color" => Ok(("background", "overlay_color")),
        "overlay_opacity" | "background.overlay_opacity" => Ok(("background", "overlay_opacity")),
        "random_overlay" | "background.random_overlay" => Ok(("background", "random_overlay")),
        _ => Err(format!(
            "unknown config key '{key}'. Try gpu, gpu_mode, theme, font, opacity, background.image, background_opacity, overlay_color, overlay_opacity, random_overlay, or topbar"
        )),
    }
}

fn renderer_from_config_value(key: &str, raw_value: &str) -> Result<RendererPreference, String> {
    match key.trim().to_ascii_lowercase().replace('-', "_").as_str() {
        "gpu" | "gpu_acceleration" | "window.gpu" | "window.gpu_acceleration" => {
            if let Ok(enabled) = parse_bool(raw_value) {
                Ok(if enabled {
                    RendererPreference::Auto
                } else {
                    RendererPreference::Cairo
                })
            } else {
                RendererPreference::parse(raw_value)
            }
        }
        "gpu_mode" | "window.gpu_mode" => RendererPreference::parse_gpu_mode(raw_value),
        _ => RendererPreference::parse(raw_value),
    }
}

fn read_document(path: &Path) -> Result<DocumentMut, String> {
    let text = if path.exists() {
        fs::read_to_string(path)
            .map_err(|error| format!("failed to read config '{}': {error}", path.display()))?
    } else {
        sample_config().to_string()
    };

    text.parse::<DocumentMut>()
        .map_err(|error| format!("failed to parse config '{}': {error}", path.display()))
}

fn write_document(path: &Path, doc: DocumentMut) -> Result<(), String> {
    write_text(path, &doc.to_string())
}

fn write_text(path: &Path, text: &str) -> Result<(), String> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).map_err(|error| {
            format!(
                "failed to create config directory '{}': {error}",
                parent.display()
            )
        })?;
    }

    fs::write(path, text)
        .map_err(|error| format!("failed to write config '{}': {error}", path.display()))
}

fn ensure_table(doc: &mut DocumentMut, section: &str) {
    if !doc[section].is_table() {
        doc[section] = Item::Table(Table::new());
    }
}

fn parse_bool(value: &str) -> Result<bool, String> {
    match value.trim().to_ascii_lowercase().as_str() {
        "1" | "true" | "yes" | "on" => Ok(true),
        "0" | "false" | "no" | "off" => Ok(false),
        _ => Err(format!("'{value}' is not a boolean")),
    }
}

fn unit_interval(value: f64, name: &str) -> Result<f64, String> {
    if (0.0..=1.0).contains(&value) {
        Ok(value)
    } else {
        Err(format!("{name} must be between 0.0 and 1.0"))
    }
}

fn positive_i32(value: i32, name: &str) -> Result<i32, String> {
    if value > 0 {
        Ok(value)
    } else {
        Err(format!("{name} must be greater than 0"))
    }
}

fn bounded_scrollback(value: i64, name: &str) -> Result<i64, String> {
    if !(0..=MAX_SCROLLBACK_LINES).contains(&value) {
        Err(format!(
            "{name} must be between 0 and {MAX_SCROLLBACK_LINES}"
        ))
    } else {
        Ok(value)
    }
}

fn color_to_hex(color: &gtk::gdk::RGBA) -> String {
    let red = (color.red().clamp(0.0, 1.0) * 255.0).round() as u8;
    let green = (color.green().clamp(0.0, 1.0) * 255.0).round() as u8;
    let blue = (color.blue().clamp(0.0, 1.0) * 255.0).round() as u8;
    format!("#{red:02x}{green:02x}{blue:02x}")
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::{SystemTime, UNIX_EPOCH};

    #[test]
    fn empty_config_uses_reference_dark_default() {
        let path = write_temp_config("");
        let settings = AppSettings::load(Some(path.clone()), ConfigOverrides::default()).unwrap();

        assert_eq!(settings.terminal.theme_name, "xfce");
        assert_eq!(settings.window.renderer, RendererPreference::Cairo);
        assert_eq!(settings.terminal.background.terminal_opacity, 1.0);
        assert!(settings.terminal.background.random_overlay);

        let _ = fs::remove_file(path);
    }

    #[test]
    fn image_config_with_opaque_terminal_uses_reference_shade() {
        let path = write_temp_config(
            r#"
[background]
image = "/tmp/wallpaper.jpg"
terminal_opacity = 1.00
"#,
        );
        let settings = AppSettings::load(Some(path.clone()), ConfigOverrides::default()).unwrap();

        assert_eq!(
            settings.terminal.background.terminal_opacity,
            DEFAULT_IMAGE_TERMINAL_OPACITY
        );

        let _ = fs::remove_file(path);
    }

    #[test]
    fn image_override_lowers_default_terminal_opacity() {
        let path = write_temp_config("");
        let overrides = ConfigOverrides {
            background_image: Some(PathBuf::from("/tmp/wallpaper.jpg")),
            ..ConfigOverrides::default()
        };
        let settings = AppSettings::load(Some(path.clone()), overrides).unwrap();

        assert_eq!(
            settings.terminal.background.terminal_opacity,
            DEFAULT_IMAGE_TERMINAL_OPACITY
        );

        let _ = fs::remove_file(path);
    }

    #[test]
    fn window_opacity_loads_from_config() {
        let path = write_temp_config(
            r#"
[window]
opacity = 0.72
"#,
        );
        let settings = AppSettings::load(Some(path.clone()), ConfigOverrides::default()).unwrap();

        assert_eq!(settings.window.opacity, 0.72);

        let _ = fs::remove_file(path);
    }

    #[test]
    fn window_opacity_override_applies() {
        let path = write_temp_config("");
        let overrides = ConfigOverrides {
            window_opacity: Some(0.66),
            ..ConfigOverrides::default()
        };
        let settings = AppSettings::load(Some(path.clone()), overrides).unwrap();

        assert_eq!(settings.window.opacity, 0.66);

        let _ = fs::remove_file(path);
    }

    #[test]
    fn renderer_gpu_settings_map_to_expected_backends() {
        assert_eq!(
            RendererPreference::from_gpu_settings(false, "vulkan").unwrap(),
            RendererPreference::Cairo
        );
        assert_eq!(
            RendererPreference::from_gpu_settings(true, "auto").unwrap(),
            RendererPreference::Auto
        );
        assert_eq!(
            RendererPreference::from_gpu_settings(true, "gl").unwrap(),
            RendererPreference::Gl
        );
        assert_eq!(
            RendererPreference::from_gpu_settings(true, "vulkan").unwrap(),
            RendererPreference::Vulkan
        );
        assert!(!RendererPreference::Cairo.gpu_enabled());
        assert!(RendererPreference::Vulkan.gpu_enabled());
        assert_eq!(RendererPreference::Cairo.gpu_mode_config(), "auto");
    }

    #[test]
    fn config_set_gpu_alias_writes_renderer() {
        let path = write_temp_config(sample_config());

        set_config_value(&path, "gpu", "on").unwrap();
        let settings = AppSettings::load(Some(path.clone()), ConfigOverrides::default()).unwrap();
        assert_eq!(settings.window.renderer, RendererPreference::Auto);

        set_config_value(&path, "gpu", "off").unwrap();
        let settings = AppSettings::load(Some(path.clone()), ConfigOverrides::default()).unwrap();
        assert_eq!(settings.window.renderer, RendererPreference::Cairo);

        let _ = fs::remove_file(path);
    }

    #[test]
    fn config_set_gpu_mode_writes_renderer() {
        let path = write_temp_config(sample_config());

        set_config_value(&path, "gpu_mode", "vulkan").unwrap();
        let settings = AppSettings::load(Some(path.clone()), ConfigOverrides::default()).unwrap();
        assert_eq!(settings.window.renderer, RendererPreference::Vulkan);

        let _ = fs::remove_file(path);
    }

    #[test]
    fn config_set_opacity_writes_window_opacity() {
        let path = write_temp_config(sample_config());

        set_config_value(&path, "opacity", "0.73").unwrap();
        let settings = AppSettings::load(Some(path.clone()), ConfigOverrides::default()).unwrap();

        assert_eq!(settings.window.opacity, 0.73);

        let _ = fs::remove_file(path);
    }

    #[test]
    fn config_set_background_opacity_writes_terminal_shade() {
        let path = write_temp_config(sample_config());

        set_config_value(&path, "background_opacity", "0.41").unwrap();
        let settings = AppSettings::load(Some(path.clone()), ConfigOverrides::default()).unwrap();

        assert_eq!(settings.terminal.background.terminal_opacity, 0.41);

        let _ = fs::remove_file(path);
    }

    fn write_temp_config(contents: &str) -> PathBuf {
        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let path = env::temp_dir().join(format!("lios-test-{}-{nonce}.toml", std::process::id()));
        fs::write(&path, contents).unwrap();
        path
    }
}