freshl 0.20260602.2

Modern ls replacement with git awareness
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
// Copyright © 2026 Michael Shields
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use anstyle::{Ansi256Color, AnsiColor, Color, Effects, RgbColor, Style};
use lscolors::{Color as LsColor, FontStyle, Indicator, LsColors, Style as LsStyle};

use crate::entry::{Entry, EntryKind};

/// Resolves the right `anstyle::Style` for each `Entry` from `$LS_COLORS`.
///
/// The lookup classifies the entry into an `LS_COLORS` `Indicator` using only
/// data already on the entry (mode, name, kind) — no extra stat calls.
/// Extension and filename rules apply to regular files only, matching `ls`.
#[derive(Debug, Clone)]
pub struct Palette {
    inner: LsColors,
}

impl Palette {
    /// Read `$LS_COLORS`, falling back to the GNU `dircolors` defaults when
    /// the variable is unset.
    #[must_use]
    pub fn from_env() -> Self {
        Self {
            inner: LsColors::from_env().unwrap_or_default(),
        }
    }

    #[must_use]
    pub fn from_string(input: &str) -> Self {
        Self {
            inner: LsColors::from_string(input),
        }
    }

    #[must_use]
    pub fn empty() -> Self {
        Self {
            inner: LsColors::empty(),
        }
    }

    #[must_use]
    pub fn style_for(&self, entry: &Entry) -> Style {
        let indicator = self.indicator_for(entry);
        // Extension/filename rules only fire when classification stayed at
        // `RegularFile`. `file_indicator` only promotes to ex/su/sg/mh when
        // the user has actually configured that indicator, so an unset `ex`
        // lets `*.sh=…` color executable scripts; setting `ex` makes it win.
        // This matches GNU `ls` (`ls.c`: `if (type == C_FILE)` gates the
        // suffix lookup) and the `lscolors` crate.
        //
        // `to_string_lossy` (rather than `to_str`) keeps ASCII suffixes
        // matchable when the rest of the name has invalid UTF-8 bytes —
        // GNU `ls` matches suffixes byte-wise, so the lossy substitution is
        // strictly more permissive than dropping non-UTF-8 names entirely.
        if indicator == Indicator::RegularFile
            && let Some(style) = self.inner.style_for_str(&entry.name.to_string_lossy())
        {
            return to_anstyle(style);
        }
        // `style_for_indicator` has its own fallback chain — e.g. an
        // OrphanedSymbolicLink with no `or` set falls through to `ln`, then
        // `no` — so we don't need to second-guess it here.
        self.inner
            .style_for_indicator(indicator)
            .map_or_else(Style::new, to_anstyle)
    }

    /// Style for a broken symlink's target text (`mi`), if the user has
    /// configured it. Returns `None` when `mi` is unset so the caller can
    /// pick its own visual cue.
    #[must_use]
    pub fn style_for_missing_target(&self) -> Option<Style> {
        self.inner
            .has_explicit_style_for(Indicator::MissingFile)
            .then(|| {
                self.inner
                    .style_for_indicator(Indicator::MissingFile)
                    .map_or_else(Style::new, to_anstyle)
            })
    }

    /// `ln` indicator's style. Used by the chain renderer for the left side
    /// of `name → target`, where the entry's `kind` has been reclassified to
    /// the target's kind so `style_for` would no longer return the symlink
    /// style.
    #[must_use]
    pub fn style_for_symlink(&self) -> Style {
        self.inner
            .style_for_indicator(Indicator::SymbolicLink)
            .map_or_else(Style::new, to_anstyle)
    }

    fn indicator_for(&self, entry: &Entry) -> Indicator {
        match entry.kind {
            EntryKind::Directory => self.dir_indicator(entry.mode),
            // A surviving `Symlink` kind is a broken link — resolved links take
            // their target's kind. `or` colors the link itself; `mi` is for the
            // target string, left to the caller that renders it.
            EntryKind::Symlink => Indicator::OrphanedSymbolicLink,
            EntryKind::RegularFile => self.file_indicator(entry.mode, entry.nlink),
            EntryKind::CharDevice => Indicator::CharacterDevice,
            EntryKind::BlockDevice => Indicator::BlockDevice,
            EntryKind::Fifo => Indicator::FIFO,
            EntryKind::Socket => Indicator::Socket,
            EntryKind::Other => Indicator::Normal,
        }
    }

    fn dir_indicator(&self, mode: u32) -> Indicator {
        // Mirror lscolors' own precedence: most specific match wins, but only
        // if the user has explicitly configured a style for that indicator —
        // otherwise we'd shadow a more general indicator that *is* set.
        if mode & 0o1002 == 0o1002
            && self
                .inner
                .has_explicit_style_for(Indicator::StickyAndOtherWritable)
        {
            Indicator::StickyAndOtherWritable
        } else if mode & 0o0002 != 0 && self.inner.has_explicit_style_for(Indicator::OtherWritable)
        {
            Indicator::OtherWritable
        } else if mode & 0o1000 != 0 && self.inner.has_explicit_style_for(Indicator::Sticky) {
            Indicator::Sticky
        } else {
            Indicator::Directory
        }
    }

    fn file_indicator(&self, mode: u32, nlink: u64) -> Indicator {
        if mode & 0o4000 != 0 && self.inner.has_explicit_style_for(Indicator::Setuid) {
            Indicator::Setuid
        } else if mode & 0o2000 != 0 && self.inner.has_explicit_style_for(Indicator::Setgid) {
            Indicator::Setgid
        } else if mode & 0o0111 != 0 && self.inner.has_explicit_style_for(Indicator::ExecutableFile)
        {
            Indicator::ExecutableFile
        } else if nlink > 1
            && self
                .inner
                .has_explicit_style_for(Indicator::MultipleHardLinks)
        {
            Indicator::MultipleHardLinks
        } else {
            Indicator::RegularFile
        }
    }
}

fn to_anstyle(s: &LsStyle) -> Style {
    let mut style = Style::new();
    if let Some(fg) = s.foreground {
        style = style.fg_color(Some(to_anstyle_color(fg)));
    }
    if let Some(bg) = s.background {
        style = style.bg_color(Some(to_anstyle_color(bg)));
    }
    style.effects(to_effects(s.font_style))
}

const fn to_anstyle_color(c: LsColor) -> Color {
    match c {
        LsColor::Black => Color::Ansi(AnsiColor::Black),
        LsColor::Red => Color::Ansi(AnsiColor::Red),
        LsColor::Green => Color::Ansi(AnsiColor::Green),
        LsColor::Yellow => Color::Ansi(AnsiColor::Yellow),
        LsColor::Blue => Color::Ansi(AnsiColor::Blue),
        LsColor::Magenta => Color::Ansi(AnsiColor::Magenta),
        LsColor::Cyan => Color::Ansi(AnsiColor::Cyan),
        LsColor::White => Color::Ansi(AnsiColor::White),
        LsColor::BrightBlack => Color::Ansi(AnsiColor::BrightBlack),
        LsColor::BrightRed => Color::Ansi(AnsiColor::BrightRed),
        LsColor::BrightGreen => Color::Ansi(AnsiColor::BrightGreen),
        LsColor::BrightYellow => Color::Ansi(AnsiColor::BrightYellow),
        LsColor::BrightBlue => Color::Ansi(AnsiColor::BrightBlue),
        LsColor::BrightMagenta => Color::Ansi(AnsiColor::BrightMagenta),
        LsColor::BrightCyan => Color::Ansi(AnsiColor::BrightCyan),
        LsColor::BrightWhite => Color::Ansi(AnsiColor::BrightWhite),
        LsColor::Fixed(n) => Color::Ansi256(Ansi256Color(n)),
        LsColor::RGB(r, g, b) => Color::Rgb(RgbColor(r, g, b)),
    }
}

fn to_effects(f: FontStyle) -> Effects {
    let mut e = Effects::new();
    if f.bold {
        e |= Effects::BOLD;
    }
    if f.dimmed {
        e |= Effects::DIMMED;
    }
    if f.italic {
        e |= Effects::ITALIC;
    }
    if f.underline {
        e |= Effects::UNDERLINE;
    }
    if f.slow_blink || f.rapid_blink {
        e |= Effects::BLINK;
    }
    if f.reverse {
        e |= Effects::INVERT;
    }
    if f.hidden {
        e |= Effects::HIDDEN;
    }
    if f.strikethrough {
        e |= Effects::STRIKETHROUGH;
    }
    e
}

#[cfg(test)]
mod tests {
    use super::Palette;
    use crate::entry::{Entry, EntryKind};
    use std::ffi::OsString;
    use std::path::PathBuf;
    use std::time::SystemTime;

    fn entry(name: &str, kind: EntryKind) -> Entry {
        Entry {
            name: OsString::from(name),
            path: PathBuf::from(name),
            kind,
            mode: 0,
            nlink: 1,
            uid: 0,
            gid: 0,
            size: 0,
            rdev: 0,
            mtime: SystemTime::UNIX_EPOCH,
            dev: 0,
            ino: 0,
            follow_chain: Vec::new(),
        }
    }

    #[test]
    fn gnu_defaults_color_directory_bold_blue() {
        let palette = Palette::from_string("");
        let style = palette.style_for(&entry("d", EntryKind::Directory));
        let s = format!("{style}");
        assert!(s.contains("34"), "expected blue SGR: {s:?}");
        assert!(s.contains('1'), "expected bold SGR: {s:?}");
    }

    #[test]
    fn regular_file_has_no_style_under_gnu_defaults() {
        let palette = Palette::from_string("");
        let style = palette.style_for(&entry("f", EntryKind::RegularFile));
        assert_eq!(format!("{style}"), String::new());
    }

    #[test]
    fn extension_rule_paints_regular_file() {
        let palette = Palette::from_string("*.rs=38;5;202");
        let style = palette.style_for(&entry("main.rs", EntryKind::RegularFile));
        let s = format!("{style}");
        assert!(s.contains("202"), "expected fixed-256 color 202: {s:?}");
    }

    #[test]
    fn executable_indicator_kicks_in_when_set() {
        let palette = Palette::from_string("ex=31");
        let mut e = entry("run", EntryKind::RegularFile);
        e.mode = 0o100_755;
        let style = palette.style_for(&e);
        let s = format!("{style}");
        assert!(s.contains("31"), "expected red SGR for ex: {s:?}");
    }

    #[test]
    fn setuid_overrides_executable() {
        let palette = Palette::from_string("ex=31:su=37;41");
        let mut e = entry("priv", EntryKind::RegularFile);
        e.mode = 0o104_755;
        let style = palette.style_for(&e);
        let s = format!("{style}");
        assert!(s.contains("41"), "expected red bg for setuid: {s:?}");
    }

    #[test]
    fn other_writable_dir_uses_ow_when_set() {
        let palette = Palette::from_string("ow=30;43");
        let mut e = entry("shared", EntryKind::Directory);
        e.mode = 0o040_777;
        let style = palette.style_for(&e);
        let s = format!("{style}");
        assert!(s.contains("43"), "expected yellow bg: {s:?}");
    }

    #[test]
    fn sticky_other_writable_prefers_tw() {
        let palette = Palette::from_string("tw=30;42:ow=30;43");
        let mut e = entry("tmp", EntryKind::Directory);
        e.mode = 0o041_777;
        let style = palette.style_for(&e);
        let s = format!("{style}");
        assert!(s.contains("42"), "expected green bg: {s:?}");
    }

    #[test]
    fn orphan_symlink_uses_or_when_set() {
        let palette = Palette::from_string("or=40;31;01");
        let style = palette.style_for(&entry("broken", EntryKind::Symlink));
        let s = format!("{style}");
        assert!(s.contains("31"), "expected red SGR: {s:?}");
    }

    #[test]
    fn live_symlink_uses_ln_not_or() {
        // Live links never reach `style_for` with a `Symlink` kind — they're
        // reclassified to the target's kind. The chain renderer styles the link
        // side via `style_for_symlink`, which resolves to `ln`, not `or`.
        let palette = Palette::from_string("ln=35:or=31");
        let style = palette.style_for_symlink();
        let s = format!("{style}");
        assert!(s.contains("35"), "expected magenta ln SGR: {s:?}");
        assert!(
            !s.contains("31"),
            "or must not leak onto the link side: {s:?}"
        );
    }

    #[test]
    fn fifo_socket_devices_map_to_indicators() {
        let palette = Palette::from_string("pi=33:so=01;35:bd=34;46:cd=34;43");
        for (kind, expect) in [
            (EntryKind::Fifo, "33"),
            (EntryKind::Socket, "35"),
            (EntryKind::BlockDevice, "46"),
            (EntryKind::CharDevice, "43"),
        ] {
            let style = palette.style_for(&entry("x", kind));
            let s = format!("{style}");
            assert!(s.contains(expect), "{kind:?} expected {expect}: {s:?}");
        }
    }

    #[test]
    fn empty_palette_leaves_everything_unstyled() {
        let palette = Palette::empty();
        for kind in [
            EntryKind::Directory,
            EntryKind::RegularFile,
            EntryKind::Symlink,
            EntryKind::Fifo,
            EntryKind::Socket,
            EntryKind::BlockDevice,
            EntryKind::CharDevice,
            EntryKind::Other,
        ] {
            let style = palette.style_for(&entry("x", kind));
            assert_eq!(format!("{style}"), String::new(), "{kind:?}");
        }
    }

    #[test]
    fn non_utf8_name_still_matches_ascii_suffix() {
        use std::os::unix::ffi::OsStringExt;
        let palette = Palette::from_string("*.rs=31");
        let mut e = entry("placeholder", EntryKind::RegularFile);
        // U+FFFD substitution preserves the ".rs" suffix so the extension
        // rule still fires, matching GNU ls's byte-wise suffix matching.
        e.name = OsString::from_vec(vec![b'b', 0xFF, b'.', b'r', b's']);
        let style = palette.style_for(&e);
        assert!(format!("{style}").contains("31"));
    }

    #[test]
    fn from_env_does_not_panic() {
        let _ = Palette::from_env();
    }

    #[test]
    fn malformed_ls_colors_does_not_panic_and_keeps_valid_rules() {
        // freshl delegates LS_COLORS parsing to the infallible `lscolors` crate.
        // Garbage tokens — empty keys, missing `=`, a non-numeric SGR value,
        // stray separators — must be skipped without panicking, and a
        // well-formed rule in the same string must still take effect (the
        // parser doesn't bail on the whole value). Exact handling of exotic
        // codes is lscolors' contract, not freshl's; the only promise here is
        // "don't crash on hostile env input, and don't let garbage shadow a
        // valid rule".
        let palette = Palette::from_string(":=:bogus:*.rs=not-a-number:::di=1;34:x=");
        let dir = palette.style_for(&entry("d", EntryKind::Directory));
        assert!(
            format!("{dir}").contains("34"),
            "a valid `di` rule must survive the surrounding garbage: {dir:?}"
        );
    }

    #[test]
    fn orphan_symlink_falls_back_to_ln_when_or_unset() {
        // Without `or`, lscolors' own fallback chain hands OrphanedSymbolicLink
        // off to SymbolicLink — a broken link should still get the `ln` color.
        let palette = Palette::from_string("ln=35");
        let style = palette.style_for(&entry("broken", EntryKind::Symlink));
        let s = format!("{style}");
        assert!(
            s.contains("35"),
            "or→ln fallback should give magenta: {s:?}"
        );
    }

    #[test]
    fn style_for_missing_target_returns_mi_style_when_set() {
        let palette = Palette::from_string("mi=01;33");
        let style = palette.style_for_missing_target().expect("mi style");
        let s = format!("{style}");
        assert!(s.contains("33"), "expected mi yellow SGR: {s:?}");
    }

    #[test]
    fn style_for_missing_target_returns_none_when_mi_unset() {
        let palette = Palette::from_string("or=31");
        assert!(palette.style_for_missing_target().is_none());
    }

    #[test]
    fn sticky_only_dir_uses_st_indicator() {
        let palette = Palette::from_string("st=37;44");
        let mut e = entry("d", EntryKind::Directory);
        e.mode = 0o041_755; // sticky bit set, world bits unset
        let style = palette.style_for(&e);
        let s = format!("{style}");
        assert!(s.contains("44"), "expected blue bg for st: {s:?}");
    }

    #[test]
    fn setgid_indicator_applies_to_sgid_file() {
        let palette = Palette::from_string("sg=30;46");
        let mut e = entry("svc", EntryKind::RegularFile);
        e.mode = 0o102_755; // setgid bit set
        let style = palette.style_for(&e);
        let s = format!("{style}");
        assert!(s.contains("46"), "expected cyan bg for sg: {s:?}");
    }

    #[test]
    fn multiple_hard_links_indicator_applies_when_nlink_gt_one() {
        let palette = Palette::from_string("mh=35");
        let mut e = entry("twin", EntryKind::RegularFile);
        e.nlink = 2;
        let style = palette.style_for(&e);
        let s = format!("{style}");
        assert!(s.contains("35"), "expected magenta for mh: {s:?}");
    }

    #[test]
    fn bright_colors_convert_through_anstyle() {
        // SGR 90–97 are the bright foreground colors; pair each with a suffix
        // so the conversion path runs for every BrightX variant.
        let palette =
            Palette::from_string("*.bk=90:*.br=91:*.bg=92:*.by=93:*.bu=94:*.bm=95:*.bc=96:*.bw=97");
        for (name, code) in [
            ("a.bk", "90"),
            ("a.br", "91"),
            ("a.bg", "92"),
            ("a.by", "93"),
            ("a.bu", "94"),
            ("a.bm", "95"),
            ("a.bc", "96"),
            ("a.bw", "97"),
        ] {
            let style = palette.style_for(&entry(name, EntryKind::RegularFile));
            let s = format!("{style}");
            assert!(s.contains(code), "{name} expected SGR {code}: {s:?}");
        }
    }

    #[test]
    fn rgb_color_converts_through_anstyle() {
        let palette = Palette::from_string("*.rgb=38;2;100;50;25");
        let style = palette.style_for(&entry("a.rgb", EntryKind::RegularFile));
        let s = format!("{style}");
        assert!(s.contains("100"), "expected RGB component: {s:?}");
    }

    #[test]
    fn all_font_effects_convert_through_anstyle() {
        // SGR 1;2;3;4;5;7;8;9 = bold;dim;italic;underline;blink;reverse;hidden;strike.
        let palette = Palette::from_string("*.fx=1;2;3;4;5;7;8;9");
        let style = palette.style_for(&entry("a.fx", EntryKind::RegularFile));
        let s = format!("{style}");
        for code in ['1', '2', '3', '4', '5', '7', '8', '9'] {
            assert!(s.contains(code), "expected effect SGR {code}: {s:?}");
        }
    }

    #[test]
    fn rapid_blink_also_maps_to_blink_effect() {
        // SGR 6 = rapid_blink, distinct from 5 = slow_blink but both map to
        // anstyle's BLINK effect.
        let palette = Palette::from_string("*.fx=6");
        let style = palette.style_for(&entry("a.fx", EntryKind::RegularFile));
        assert!(
            format!("{style}").contains('5'),
            "rapid_blink → anstyle BLINK (SGR 5)"
        );
    }
}