bestls 1.5.0

A fast and colorful Rust-based ls replacement CLI tool with JSON output and sorting options.
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
//! # Color & Theme Module
//!
//! This module handles color theming for bestls, supporting:
//! - File extension-based coloring
//! - Customizable theme configuration via `~/.config/bestls/config.toml`
//! - Default theme with built-in color mappings
//!
//! ## Configuration
//!
//! Users can customize colors by creating `~/.config/bestls/config.toml`:
//!
//! ```toml
//! [colors]
//! # File type colors
//! file = "bright_cyan"
//! directory = "bright_blue"
//! symlink = "bright_magenta"
//!
//! # Extension-based colors (optional)
//! [colors.extensions]
//! rs = "yellow"
//! toml = "red"
//! json = "green"
//! md = "cyan"
//! ```
//!
//! ## Supported Colors
//!
//! - `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`
//! - `bright_black`, `bright_red`, `bright_green`, `bright_yellow`
//! - `bright_blue`, `bright_magenta`, `bright_cyan`, `bright_white`

use crate::fsops::FileType;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use tabled::settings::Color;

/// Represents ANSI color codes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ColorValue {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
    BrightBlack,
    BrightRed,
    BrightGreen,
    BrightYellow,
    BrightBlue,
    BrightMagenta,
    BrightCyan,
    BrightWhite,
}

impl ColorValue {
    /// Convert to tabled::settings::Color
    pub fn to_tabled_color(self) -> Color {
        match self {
            ColorValue::Black => Color::FG_BLACK,
            ColorValue::Red => Color::FG_RED,
            ColorValue::Green => Color::FG_GREEN,
            ColorValue::Yellow => Color::FG_YELLOW,
            ColorValue::Blue => Color::FG_BLUE,
            ColorValue::Magenta => Color::FG_MAGENTA,
            ColorValue::Cyan => Color::FG_CYAN,
            ColorValue::White => Color::FG_WHITE,
            ColorValue::BrightBlack => Color::FG_BRIGHT_BLACK,
            ColorValue::BrightRed => Color::FG_BRIGHT_RED,
            ColorValue::BrightGreen => Color::FG_BRIGHT_GREEN,
            ColorValue::BrightYellow => Color::FG_BRIGHT_YELLOW,
            ColorValue::BrightBlue => Color::FG_BRIGHT_BLUE,
            ColorValue::BrightMagenta => Color::FG_BRIGHT_MAGENTA,
            ColorValue::BrightCyan => Color::FG_BRIGHT_CYAN,
            ColorValue::BrightWhite => Color::FG_BRIGHT_WHITE,
        }
    }

    /// Parse from string (e.g., "bright_cyan")
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "black" => Some(ColorValue::Black),
            "red" => Some(ColorValue::Red),
            "green" => Some(ColorValue::Green),
            "yellow" => Some(ColorValue::Yellow),
            "blue" => Some(ColorValue::Blue),
            "magenta" => Some(ColorValue::Magenta),
            "cyan" => Some(ColorValue::Cyan),
            "white" => Some(ColorValue::White),
            "bright_black" => Some(ColorValue::BrightBlack),
            "bright_red" => Some(ColorValue::BrightRed),
            "bright_green" => Some(ColorValue::BrightGreen),
            "bright_yellow" => Some(ColorValue::BrightYellow),
            "bright_blue" => Some(ColorValue::BrightBlue),
            "bright_magenta" => Some(ColorValue::BrightMagenta),
            "bright_cyan" => Some(ColorValue::BrightCyan),
            "bright_white" => Some(ColorValue::BrightWhite),
            _ => None,
        }
    }
}

impl std::fmt::Display for ColorValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            ColorValue::Black => "black",
            ColorValue::Red => "red",
            ColorValue::Green => "green",
            ColorValue::Yellow => "yellow",
            ColorValue::Blue => "blue",
            ColorValue::Magenta => "magenta",
            ColorValue::Cyan => "cyan",
            ColorValue::White => "white",
            ColorValue::BrightBlack => "bright_black",
            ColorValue::BrightRed => "bright_red",
            ColorValue::BrightGreen => "bright_green",
            ColorValue::BrightYellow => "bright_yellow",
            ColorValue::BrightBlue => "bright_blue",
            ColorValue::BrightMagenta => "bright_magenta",
            ColorValue::BrightCyan => "bright_cyan",
            ColorValue::BrightWhite => "bright_white",
        };
        write!(f, "{}", s)
    }
}

/// File type color mappings
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct FileTypeColors {
    pub file: ColorValue,
    pub directory: ColorValue,
    pub symlink: ColorValue,
}

impl Default for FileTypeColors {
    fn default() -> Self {
        Self {
            file: ColorValue::BrightCyan,
            directory: ColorValue::BrightBlue,
            symlink: ColorValue::BrightMagenta,
        }
    }
}

/// Theme configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Theme {
    /// File type colors
    pub file_types: FileTypeColors,
    /// Extension-based colors (e.g., "rs" -> "yellow")
    pub extensions: HashMap<String, ColorValue>,
    /// Table column colors
    pub table: TableColors,
}

impl Default for Theme {
    fn default() -> Self {
        Self {
            file_types: FileTypeColors::default(),
            extensions: default_extension_colors(),
            table: TableColors::default(),
        }
    }
}

/// Table column color settings
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct TableColors {
    pub name: ColorValue,
    pub size: ColorValue,
    pub date: ColorValue,
    pub header: ColorValue,
}

impl Default for TableColors {
    fn default() -> Self {
        Self {
            name: ColorValue::BrightCyan,
            size: ColorValue::BrightMagenta,
            date: ColorValue::BrightYellow,
            header: ColorValue::BrightGreen,
        }
    }
}

/// Get default extension color mapping
fn default_extension_colors() -> HashMap<String, ColorValue> {
    [
        // Programming languages
        ("rs", ColorValue::Yellow),      // Rust
        ("py", ColorValue::Blue),        // Python
        ("js", ColorValue::Yellow),      // JavaScript
        ("ts", ColorValue::Blue),        // TypeScript
        ("go", ColorValue::BrightCyan),  // Go
        ("c", ColorValue::BrightBlue),   // C
        ("cpp", ColorValue::BrightBlue), // C++
        ("java", ColorValue::Red),       // Java
        // Documents
        ("md", ColorValue::Cyan),   // Markdown
        ("txt", ColorValue::White), // Text
        ("pdf", ColorValue::Red),   // PDF
        // Configuration
        ("toml", ColorValue::Red),     // TOML
        ("json", ColorValue::Green),   // JSON
        ("yaml", ColorValue::Magenta), // YAML
        ("yml", ColorValue::Magenta),  // YAML
        ("xml", ColorValue::Yellow),   // XML
        // Archives
        ("zip", ColorValue::Red), // ZIP
        ("tar", ColorValue::Red), // TAR
        ("gz", ColorValue::Red),  // GZIP
        // Images
        ("png", ColorValue::Magenta),  // PNG
        ("jpg", ColorValue::Magenta),  // JPEG
        ("jpeg", ColorValue::Magenta), // JPEG
        ("gif", ColorValue::Magenta),  // GIF
        ("svg", ColorValue::Yellow),   // SVG
    ]
    .iter()
    .map(|(k, v)| (k.to_string(), *v))
    .collect()
}

/// Load theme from config file or use default
pub fn load_theme() -> Theme {
    if let Ok(theme) = load_theme_from_config() {
        return theme;
    }
    Theme::default()
}

/// Try to load theme from config file
fn load_theme_from_config() -> Result<Theme, Box<dyn std::error::Error>> {
    let config_dir = dirs::config_dir()
        .ok_or("Could not determine config directory")?
        .join("bestls");

    let config_path = config_dir.join("config.toml");

    if !config_path.exists() {
        return Err("Config file not found".into());
    }

    let content = std::fs::read_to_string(&config_path)?;
    let config: ThemeConfig = toml::from_str(&content)?;

    Ok(config.into_theme())
}

/// Configuration file structure
#[derive(Debug, Serialize, Deserialize)]
struct ThemeConfig {
    #[serde(default)]
    colors: ColorConfig,
}

#[derive(Debug, Serialize, Deserialize, Default)]
struct ColorConfig {
    #[serde(default)]
    file_types: Option<FileTypeColors>,
    #[serde(default)]
    extensions: Option<HashMap<String, String>>,
    #[serde(default)]
    table: Option<TableColors>,
}

impl ThemeConfig {
    fn into_theme(self) -> Theme {
        let mut theme = Theme::default();

        if let Some(ft) = self.colors.file_types {
            theme.file_types = ft;
        }

        if let Some(exts) = self.colors.extensions {
            for (ext, color_str) in exts {
                if let Some(color) = ColorValue::from_str(&color_str) {
                    theme.extensions.insert(ext, color);
                }
            }
        }

        if let Some(tc) = self.colors.table {
            theme.table = tc;
        }

        theme
    }
}

/// Get color for a file based on type and extension
///
/// Used by library consumers and in tests. Marked with #[allow(dead_code)]
/// because the binary doesn't use it directly, but it's part of the public API.
#[allow(dead_code)]
pub fn get_file_color(file_type: &FileType, filename: &str, theme: &Theme) -> ColorValue {
    match file_type {
        FileType::File => {
            // Check extension-based coloring first
            if let Some(pos) = filename.rfind('.') {
                let ext = &filename[pos + 1..].to_lowercase();
                if let Some(color) = theme.extensions.get(ext) {
                    return *color;
                }
            }
            // Fall back to default file color
            theme.file_types.file
        }
        FileType::Directory => theme.file_types.directory,
        FileType::Symlink => theme.file_types.symlink,
    }
}

/// Create a sample config file for the user
pub fn create_sample_config() -> Result<PathBuf, Box<dyn std::error::Error>> {
    let config_dir = dirs::config_dir()
        .ok_or("Could not determine config directory")?
        .join("bestls");

    std::fs::create_dir_all(&config_dir)?;

    let config_path = config_dir.join("config.toml");

    if !config_path.exists() {
        let sample_config = r#"# bestls Configuration File
# Location: ~/.config/bestls/config.toml

[colors]
# File type colors
file = "bright_cyan"
directory = "bright_blue"
symlink = "bright_magenta"

[colors.table]
# Table column colors
name = "bright_cyan"
size = "bright_magenta"
date = "bright_yellow"
header = "bright_green"

[colors.extensions]
# Extension-based file colors (case-insensitive)
rs = "yellow"
py = "blue"
js = "yellow"
ts = "blue"
go = "bright_cyan"
md = "cyan"
json = "green"
toml = "red"
yaml = "magenta"
yml = "magenta"
"#;

        std::fs::write(&config_path, sample_config)?;
    }

    Ok(config_path)
}

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

    #[test]
    fn test_color_value_from_str() {
        assert_eq!(
            ColorValue::from_str("bright_cyan"),
            Some(ColorValue::BrightCyan)
        );
        assert_eq!(ColorValue::from_str("red"), Some(ColorValue::Red));
        assert_eq!(ColorValue::from_str("invalid"), None);
    }

    #[test]
    fn test_get_file_color() {
        let theme = Theme::default();

        // Test extension-based coloring
        let color = get_file_color(&FileType::File, "test.rs", &theme);
        assert_eq!(color, ColorValue::Yellow);

        // Test default file color
        let color = get_file_color(&FileType::File, "test.unknown", &theme);
        assert_eq!(color, theme.file_types.file);

        // Test directory color
        let color = get_file_color(&FileType::Directory, "src", &theme);
        assert_eq!(color, theme.file_types.directory);
    }

    #[test]
    fn test_default_extension_colors() {
        let colors = default_extension_colors();
        assert!(colors.contains_key("rs"));
        assert_eq!(colors.get("rs"), Some(&ColorValue::Yellow));
        assert!(colors.contains_key("py"));
    }
}