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
use std::{
    env,
    fmt::{self, Display, Formatter},
    path::PathBuf,
    sync::{LazyLock, OnceLock},
};

use color_eyre::eyre::Result;
use config::{Config, Environment, File};
use glob::glob;
use rat_theme3::{DarkTheme, Palette, SalsaTheme, ShellTheme};
use ratatui::style::Color;
use serde::{Deserialize, Serialize};

use crate::logging::{PROJECT_NAME, project_directory};

pub static THEME: OnceLock<&'static str> = OnceLock::new();

#[inline(always)]
fn init_theme(name: Box<str>) {
    let static_str: &'static str = Box::leak(name);
    THEME.set(static_str).unwrap();
}

#[inline(always)]
fn current_theme() -> &'static str {
    THEME.get().unwrap()
}

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub(crate) struct Settings {
    #[serde(default)]
    pub(crate) debug: bool,
    #[serde(default)]
    pub(crate) silent: bool,
    #[serde(default)]
    pub(crate) theme: Theme,
}

pub static CONFIG_FOLDER: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
    env::var(format!("{}_CONFIG", &*PROJECT_NAME))
        .ok()
        .map(PathBuf::from)
});

pub(crate) fn get_config_dir() -> PathBuf {
    if let Some(s) = CONFIG_FOLDER.clone() {
        s
    } else if let Some(proj_dirs) = project_directory() {
        proj_dirs.config_local_dir().to_path_buf()
    } else {
        PathBuf::from(".").join(".config")
    }
}

#[derive(Deserialize, Serialize, Debug, Clone, Copy, Default)]
#[allow(unused)]
pub enum DefaultTheme {
    #[default]
    ImperialDark,
    RadiumDark,
    TundraDark,
    OceanDark,
    MonochromeDark,
    BlackWhiteDark,
    Base16Dark,
    Base16RelaxDark,
    MonekaiDark,
    SolarizedDark,
    OxoCarbonDark,
    RustDark,
    VSCodeDark,
    ImperialShell,
    RadiumShell,
    TundraShell,
    OceanShell,
    MonochromeShell,
    BlackWhiteShell,
    Base16Shell,
    Base16RelaxShell,
    MonekaiShell,
    SolarizedShell,
    OxoCarbonShell,
    RustShell,
    VSCodeShell,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(untagged)]
pub enum Theme {
    Custom(Box<CustomTheme>),
    Default(DefaultTheme),
}

impl Default for Theme {
    fn default() -> Self {
        Self::Default(DefaultTheme::default())
    }
}

impl Display for DefaultTheme {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        type Theme = DefaultTheme;
        let text = match self {
            Theme::ImperialDark => "Imperial Dark",
            Theme::RadiumDark => "Radium Dark",
            Theme::TundraDark => "Tundra Dark",
            Theme::OceanDark => "Ocean Dark",
            Theme::MonochromeDark => "Monochrome Dark",
            Theme::BlackWhiteDark => "Black & White Dark",
            Theme::Base16Dark => "Base16 Dark",
            Theme::Base16RelaxDark => "Base16 Relax Dark",
            Theme::MonekaiDark => "Monekai Dark",
            Theme::SolarizedDark => "Solarized Dark",
            Theme::OxoCarbonDark => "OxoCarbon Dark",
            Theme::RustDark => "Rust Dark",
            Theme::VSCodeDark => "VSCode Dark",
            Theme::ImperialShell => "Imperial Shell",
            Theme::RadiumShell => "Radium Shell",
            Theme::TundraShell => "Tundra Shell",
            Theme::OceanShell => "Ocean Shell",
            Theme::MonochromeShell => "Monochrome Shell",
            Theme::BlackWhiteShell => "Black & White Shell",
            Theme::Base16Shell => "Base16 Shell",
            Theme::Base16RelaxShell => "Base16 Relax Shell",
            Theme::MonekaiShell => "Monekai Shell",
            Theme::SolarizedShell => "Solarized Shell",
            Theme::OxoCarbonShell => "OxoCarbon Shell",
            Theme::RustShell => "Rust Shell",
            Theme::VSCodeShell => "VSCode Shell",
        };

        write!(f, "{}", text)
    }
}

impl Settings {
    pub(crate) fn new() -> Result<Self> {
        let s = Config::builder()
            .add_source(
                glob(
                    &get_config_dir()
                        .join("config")
                        .join("*")
                        .display()
                        .to_string(),
                )?
                .map(|p| File::from(p.expect("Failed to read config file")))
                .collect::<Vec<_>>(),
            )
            .add_source(
                Environment::with_prefix(&PROJECT_NAME)
                    .separator("__")
                    .prefix_separator("_"),
            );
        let s = s.build()?;
        Ok(s.try_deserialize().unwrap_or_default())
    }
    pub(crate) fn get_theme(&self) -> &Theme {
        &self.theme
    }
}

pub(crate) struct LoggingConfig {
    silent: bool,
    debug: bool,
}

impl LoggingConfig {
    pub(crate) fn get_level(&self) -> tracing::Level {
        if self.debug {
            tracing::Level::DEBUG
        } else if self.silent {
            tracing::Level::ERROR
        } else {
            tracing::Level::INFO
        }
    }
}

impl From<&Settings> for LoggingConfig {
    fn from(settings: &Settings) -> Self {
        Self {
            silent: settings.silent,
            debug: settings.debug,
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub(crate) struct CustomTheme {
    pub(crate) name: Box<str>,
    pub(crate) palette: CustomPalette,
    pub(crate) type_: ThemeType,
}

#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub enum ThemeType {
    #[default]
    Dark,
    Shell,
}

#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct CustomPalette {
    pub name: Box<str>,

    pub text_light: Color,
    pub text_bright: Color,
    pub text_dark: Color,
    pub text_black: Color,

    pub white: [Color; 8],
    pub black: [Color; 8],
    pub gray: [Color; 8],

    pub red: [Color; 8],
    pub orange: [Color; 8],
    pub yellow: [Color; 8],
    pub limegreen: [Color; 8],
    pub green: [Color; 8],
    pub bluegreen: [Color; 8],
    pub cyan: [Color; 8],
    pub blue: [Color; 8],
    pub deepblue: [Color; 8],
    pub purple: [Color; 8],
    pub magenta: [Color; 8],
    pub redpink: [Color; 8],

    pub primary: [Color; 8],
    pub secondary: [Color; 8],
}

impl From<CustomPalette> for Palette {
    fn from(palette: CustomPalette) -> Self {
        init_theme(palette.name);
        Self {
            name: current_theme(),
            text_light: palette.text_light,
            text_bright: palette.text_bright,
            text_dark: palette.text_dark,
            text_black: palette.text_black,
            white: palette.white,
            black: palette.black,
            gray: palette.gray,
            red: palette.red,
            orange: palette.orange,
            yellow: palette.yellow,
            limegreen: palette.limegreen,
            green: palette.green,
            bluegreen: palette.bluegreen,
            cyan: palette.cyan,
            blue: palette.blue,
            deepblue: palette.deepblue,
            purple: palette.purple,
            magenta: palette.magenta,
            redpink: palette.redpink,
            primary: palette.primary,
            secondary: palette.secondary,
        }
    }
}

impl From<CustomTheme> for Box<dyn SalsaTheme> {
    fn from(theme: CustomTheme) -> Self {
        let p = theme.palette.into();
        match theme.type_ {
            ThemeType::Dark => Box::new(DarkTheme::new(&theme.name, p)),
            ThemeType::Shell => Box::new(ShellTheme::new(&theme.name, p)),
        }
    }
}

impl From<Box<CustomTheme>> for Box<dyn SalsaTheme> {
    fn from(theme: Box<CustomTheme>) -> Self {
        let p = theme.palette.into();
        match theme.type_ {
            ThemeType::Dark => Box::new(DarkTheme::new(&theme.name, p)),
            ThemeType::Shell => Box::new(ShellTheme::new(&theme.name, p)),
        }
    }
}

impl From<Box<dyn SalsaTheme>> for CustomTheme {
    fn from(theme: Box<dyn SalsaTheme>) -> Self {
        let p = theme.palette();
        if theme.name().contains("Shell") {
            CustomTheme {
                name: Box::from("Shell"),
                palette: CustomPalette {
                    name: Box::from(p.name),
                    text_light: p.text_light,
                    text_bright: p.text_bright,
                    text_dark: p.text_dark,
                    text_black: p.text_black,
                    white: p.white,
                    black: p.black,
                    gray: p.gray,
                    red: p.red,
                    orange: p.orange,
                    yellow: p.yellow,
                    limegreen: p.limegreen,
                    green: p.green,
                    bluegreen: p.bluegreen,
                    cyan: p.cyan,
                    blue: p.blue,
                    deepblue: p.deepblue,
                    purple: p.purple,
                    magenta: p.magenta,
                    redpink: p.redpink,
                    primary: p.primary,
                    secondary: p.secondary,
                },
                type_: ThemeType::Shell,
            }
        } else {
            CustomTheme {
                name: Box::from("Dark"),
                palette: CustomPalette {
                    name: Box::from(p.name),
                    text_light: p.text_light,
                    text_bright: p.text_bright,
                    text_dark: p.text_dark,
                    text_black: p.text_black,
                    white: p.white,
                    black: p.black,
                    gray: p.gray,
                    red: p.red,
                    orange: p.orange,
                    yellow: p.yellow,
                    limegreen: p.limegreen,
                    green: p.green,
                    bluegreen: p.bluegreen,
                    cyan: p.cyan,
                    blue: p.blue,
                    deepblue: p.deepblue,
                    purple: p.purple,
                    magenta: p.magenta,
                    redpink: p.redpink,
                    primary: p.primary,
                    secondary: p.secondary,
                },
                type_: ThemeType::Dark,
            }
        }
    }
}

pub fn install_manpages() -> Result<()> {
    use std::{env, fs, path::PathBuf};
    // Windows: silently skip installation
    #[cfg(target_os = "windows")]
    {
        eprintln!("Manpage installation is not supported on Windows.");
        return Ok(());
    }

    // Unix-like systems: install normally
    #[cfg(not(target_os = "windows"))]
    {
        let prefix = env::var("PREFIX").unwrap_or("/usr/local".to_string());

        let man1_dir = PathBuf::from(&prefix).join("share/man/man1");
        let man5_dir = PathBuf::from(&prefix).join("share/man/man5");

        fs::create_dir_all(&man1_dir)?;
        fs::create_dir_all(&man5_dir)?;

        // Build file names dynamically
        let man1_file = format!("{}.1", &*PROJECT_NAME).to_lowercase();
        let man5_file = format!("{}.5", &*PROJECT_NAME).to_lowercase();

        // Embed manpages
        // Adjust the include paths to your repo structure
        let man1_contents = include_str!(concat!(env!("OUT_DIR"), "/filessh.1"));
        let man5_contents = include_str!("../man/filessh.5");

        // Write them to the correct directories
        fs::write(man1_dir.join(&man1_file), man1_contents)?;
        fs::write(man5_dir.join(&man5_file), man5_contents)?;

        println!("Installed manpages:");
        println!("  {}/share/man/man1/{}", prefix, man1_file);
        println!("  {}/share/man/man5/{}", prefix, man5_file);

        Ok(())
    }
}

/// Generate a default config file to the default location
/// in TOML format
pub fn init_config() -> Result<()> {
    use std::fs;

    println!("Generating default config file...");
    let config_dir = get_config_dir().join("config");
    if !config_dir.exists() {
        fs::create_dir_all(&config_dir)?;
        let config = Settings::new()?;
        let toml = toml::to_string_pretty(&config)?;
        fs::write(config_dir.join("default.toml"), toml)?;
        println!(
            "Config file generated at {}",
            config_dir.join("default.toml").display()
        );
    } else {
        println!("Config file already exists");
    }
    Ok(())
}