lx-ls 0.8.0

The file lister with personality! 🌟
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
use nu_ansi_term::Style;

use crate::theme::lsc::Pair;


#[derive(Debug, Default, PartialEq)]
pub struct UiStyles {
    pub colourful: bool,

    pub filekinds:  FileKinds,
    pub perms:      Permissions,
    pub size:       Size,
    pub users:      Users,
    pub links:      Links,
    pub vcs:        Git,

    pub punctuation:  Style,
    pub date:         Style,
    pub inode:        Style,
    pub blocks:       Style,
    pub header:       Style,
    pub octal:        Style,
    pub flags:        Style,

    pub symlink_path:         Style,
    pub control_char:         Style,
    pub broken_symlink:       Style,
    pub broken_path_overlay:  Style,
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct FileKinds {
    pub normal: Style,
    pub directory: Style,
    pub symlink: Style,
    pub pipe: Style,
    pub block_device: Style,
    pub char_device: Style,
    pub socket: Style,
    pub special: Style,
    pub executable: Style,
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Permissions {
    pub user_read:          Style,
    pub user_write:         Style,
    pub user_execute_file:  Style,
    pub user_execute_other: Style,

    pub group_read:    Style,
    pub group_write:   Style,
    pub group_execute: Style,

    pub other_read:    Style,
    pub other_write:   Style,
    pub other_execute: Style,

    pub special_user_file: Style,
    pub special_other:     Style,

    pub attribute: Style,
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Size {
    pub major: Style,
    pub minor: Style,

    pub number_byte: Style,
    pub number_kilo: Style,
    pub number_mega: Style,
    pub number_giga: Style,
    pub number_huge: Style,

    pub unit_byte: Style,
    pub unit_kilo: Style,
    pub unit_mega: Style,
    pub unit_giga: Style,
    pub unit_huge: Style,
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Users {
    pub user_you: Style,
    pub user_someone_else: Style,
    pub group_yours: Style,
    pub group_not_yours: Style,

    /// `--uid` column styles.  `None` means cascade from `user_you`
    /// / `user_someone_else` — so themes that set only `user-you`
    /// implicitly also set the UID column colour, but themes that
    /// set `uid-you` explicitly get fine-grained control.
    pub uid_you: Option<Style>,
    pub uid_someone_else: Option<Style>,

    /// `--gid` column styles.  `None` means cascade from
    /// `group_yours` / `group_not_yours`.
    pub gid_yours: Option<Style>,
    pub gid_not_yours: Option<Style>,
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Links {
    pub normal: Style,
    pub multi_link_file: Style,
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Git {
    pub new: Style,
    pub modified: Style,
    pub deleted: Style,
    pub renamed: Style,
    pub typechange: Style,
    pub ignored: Style,
    pub conflicted: Style,
}

// ── Compiled default values for the cascading theme slots ──────
//
// These are the single source of truth for what the compiled
// default theme installs for `user_you` / `group_yours` and the
// corresponding `uid_*` / `gid_*` placeholder values.  Both
// `default_theme::colourful()` and `UiStyles::finalise_defaults()`
// reference these constants so the "is this still the compiled
// default?" check can't drift out of sync.
//
// The rationale is a hybrid of guaranteed 256-colour-safe defaults
// for the compiled theme plus a dim cascade for curated themes;
// see `UiStyles::finalise_defaults` below for the two-stage
// algorithm.

/// Compiled default `user_you` / `group_yours`: bold yellow.
pub fn default_user_you() -> Style {
    use nu_ansi_term::Color::Yellow;
    Yellow.bold()
}

/// Compiled default `user_someone_else` / `group_not_yours`: plain
/// terminal foreground.
pub fn default_user_other() -> Style {
    Style::default()
}

/// Compiled default placeholder for the `uid_you` / `gid_yours`
/// slot: plain (non-bold) yellow, from the 8-colour ANSI base
/// palette.  Universal rendering on any terminal.
pub fn default_uid_placeholder_you() -> Style {
    use nu_ansi_term::Color::Yellow;
    Yellow.normal()
}

/// Compiled default placeholder for the `uid_someone_else` /
/// `gid_not_yours` slot: 256-palette medium grey.
pub fn default_uid_placeholder_other() -> Style {
    use nu_ansi_term::Color::Fixed;
    Fixed(244).normal()
}


impl UiStyles {
    pub fn plain() -> Self {
        Self::default()
    }

    /// Fill in cascading theme slots with sensible defaults derived
    /// from their parent slots.  Called once at the end of theme
    /// construction, after all overrides (compiled defaults, env
    /// vars, named-theme config) have been applied.
    ///
    /// The algorithm is two-stage:
    ///
    /// 1. **Invalidate stale placeholders.** If `user_you` has been
    ///    overridden away from the compiled default but `uid_you`
    ///    still holds the compiled placeholder, clear the
    ///    placeholder — it was a safe fallback for the default
    ///    theme and is no longer relevant once the user or a
    ///    curated theme picked a different `user_you` colour.
    ///    The same logic applies to `uid_someone_else` / `gid_*`.
    ///
    /// 2. **Dim cascade.** Any slot still `None` is filled with a
    ///    `is_dimmed` copy of its parent slot (`user_you` →
    ///    `uid_you`, etc.) so that curated themes which don't set
    ///    the UID/GID slots explicitly still get a visually
    ///    differentiated column.
    ///
    /// Result: the compiled default theme renders with 8-colour
    /// ANSI `Yellow.normal()` for the UID column (guaranteed
    /// rendering on any terminal), while curated themes automatically
    /// cascade their own colours into a dimmed variant.  Explicit
    /// `uid-you` settings in any theme survive both stages.
    pub fn finalise_defaults(&mut self) {
        fn dimmed(base: Style) -> Style {
            let mut s = base;
            s.is_dimmed = true;
            s
        }

        // Stage 1: invalidate stale placeholders.
        let you_overridden = self.users.user_you != default_user_you();
        let other_overridden = self.users.user_someone_else != default_user_other();
        let yours_overridden = self.users.group_yours != default_user_you();
        let not_yours_overridden = self.users.group_not_yours != default_user_other();

        if you_overridden && self.users.uid_you == Some(default_uid_placeholder_you()) {
            self.users.uid_you = None;
        }
        if other_overridden
            && self.users.uid_someone_else == Some(default_uid_placeholder_other())
        {
            self.users.uid_someone_else = None;
        }
        if yours_overridden && self.users.gid_yours == Some(default_uid_placeholder_you()) {
            self.users.gid_yours = None;
        }
        if not_yours_overridden
            && self.users.gid_not_yours == Some(default_uid_placeholder_other())
        {
            self.users.gid_not_yours = None;
        }

        // Stage 2: dim cascade for any remaining None slots.
        if self.users.uid_you.is_none() {
            self.users.uid_you = Some(dimmed(self.users.user_you));
        }
        if self.users.uid_someone_else.is_none() {
            self.users.uid_someone_else = Some(dimmed(self.users.user_someone_else));
        }
        if self.users.gid_yours.is_none() {
            self.users.gid_yours = Some(dimmed(self.users.group_yours));
        }
        if self.users.gid_not_yours.is_none() {
            self.users.gid_not_yours = Some(dimmed(self.users.group_not_yours));
        }
    }
}


impl UiStyles {

    /// Sets a value on this set of colours using one of the keys understood
    /// by the `LS_COLORS` environment variable. Invalid keys set nothing, but
    /// return false.
    pub fn set_ls(&mut self, pair: &Pair<'_>) -> bool {
        match pair.key {
            "di" => self.filekinds.directory    = pair.to_style(),  // DIR
            "ex" => self.filekinds.executable   = pair.to_style(),  // EXEC
            "fi" => self.filekinds.normal       = pair.to_style(),  // FILE
            "pi" => self.filekinds.pipe         = pair.to_style(),  // FIFO
            "so" => self.filekinds.socket       = pair.to_style(),  // SOCK
            "bd" => self.filekinds.block_device = pair.to_style(),  // BLK
            "cd" => self.filekinds.char_device  = pair.to_style(),  // CHR
            "ln" => self.filekinds.symlink      = pair.to_style(),  // LINK
            "or" => self.broken_symlink         = pair.to_style(),  // ORPHAN
             _   => return false,
             // Codes we don’t do anything with:
             // MULTIHARDLINK, DOOR, SETUID, SETGID, CAPABILITY,
             // STICKY_OTHER_WRITABLE, OTHER_WRITABLE, STICKY, MISSING
        }
        true
    }

    /// Sets a value on this set of colours using one of the keys understood
    /// by the `LX_COLORS` environment variable. Invalid keys set nothing,
    /// but return false. This doesn’t take the `LS_COLORS` keys into account,
    /// so `set_ls` should have been run first.
    pub fn set_lx(&mut self, pair: &Pair<'_>) -> bool {
        match pair.key {
            "ur" => self.perms.user_read          = pair.to_style(),
            "uw" => self.perms.user_write         = pair.to_style(),
            "ux" => self.perms.user_execute_file  = pair.to_style(),
            "ue" => self.perms.user_execute_other = pair.to_style(),
            "gr" => self.perms.group_read         = pair.to_style(),
            "gw" => self.perms.group_write        = pair.to_style(),
            "gx" => self.perms.group_execute      = pair.to_style(),
            "tr" => self.perms.other_read         = pair.to_style(),
            "tw" => self.perms.other_write        = pair.to_style(),
            "tx" => self.perms.other_execute      = pair.to_style(),
            "su" => self.perms.special_user_file  = pair.to_style(),
            "sf" => self.perms.special_other      = pair.to_style(),
            "xa" => self.perms.attribute          = pair.to_style(),

            "sn" => self.set_number_style(pair.to_style()),
            "sb" => self.set_unit_style(pair.to_style()),
            "nb" => self.size.number_byte         = pair.to_style(),
            "nk" => self.size.number_kilo         = pair.to_style(),
            "nm" => self.size.number_mega         = pair.to_style(),
            "ng" => self.size.number_giga         = pair.to_style(),
            "nh" => self.size.number_huge         = pair.to_style(),
            "ub" => self.size.unit_byte           = pair.to_style(),
            "uk" => self.size.unit_kilo           = pair.to_style(),
            "um" => self.size.unit_mega           = pair.to_style(),
            "ug" => self.size.unit_giga           = pair.to_style(),
            "uh" => self.size.unit_huge           = pair.to_style(),
            "df" => self.size.major               = pair.to_style(),
            "ds" => self.size.minor               = pair.to_style(),

            "uu" => self.users.user_you           = pair.to_style(),
            "un" => self.users.user_someone_else  = pair.to_style(),
            "gu" => self.users.group_yours        = pair.to_style(),
            "gn" => self.users.group_not_yours    = pair.to_style(),
            // Capital U/G = the numeric ID version of the user/group
            // columns.  Case-sensitive, so these don't collide with the
            // lowercase `uu`/`un`/`gu`/`gn` keys above.
            "Uy" => self.users.uid_you            = Some(pair.to_style()),
            "Un" => self.users.uid_someone_else   = Some(pair.to_style()),
            "Gy" => self.users.gid_yours          = Some(pair.to_style()),
            "Gn" => self.users.gid_not_yours      = Some(pair.to_style()),

            "lc" => self.links.normal             = pair.to_style(),
            "lm" => self.links.multi_link_file    = pair.to_style(),

            "ga" => self.vcs.new                  = pair.to_style(),
            "gm" => self.vcs.modified             = pair.to_style(),
            "gd" => self.vcs.deleted              = pair.to_style(),
            "gv" => self.vcs.renamed              = pair.to_style(),
            "gt" => self.vcs.typechange           = pair.to_style(),

            "xx" => self.punctuation              = pair.to_style(),
            "da" => self.date                     = pair.to_style(),
            "in" => self.inode                    = pair.to_style(),
            "bl" => self.blocks                   = pair.to_style(),
            "hd" => self.header                   = pair.to_style(),
            "lp" => self.symlink_path             = pair.to_style(),
            "cc" => self.control_char             = pair.to_style(),
            "bO" => self.broken_path_overlay      = pair.to_style(),

             _   => return false,
        }

        true
    }

    pub fn set_number_style(&mut self, style: Style) {
        self.size.number_byte = style;
        self.size.number_kilo = style;
        self.size.number_mega = style;
        self.size.number_giga = style;
        self.size.number_huge = style;
    }

    pub fn set_unit_style(&mut self, style: Style) {
        self.size.unit_byte = style;
        self.size.unit_kilo = style;
        self.size.unit_mega = style;
        self.size.unit_giga = style;
        self.size.unit_huge = style;
    }

    /// Set a UI style from a human-readable config key and value.
    ///
    /// Returns `true` if the key was recognised.  The value is parsed
    /// via `parse_style()`, which accepts named colours, hex, X11
    /// names, modifiers, and raw ANSI codes.
    pub fn set_config(&mut self, key: &str, value: &str) -> bool {
        use super::lsc::parse_style;
        let style = parse_style(value);

        match key {
            // File kinds
            "normal"           => self.filekinds.normal       = style,
            "directory"        => self.filekinds.directory     = style,
            "symlink"          => self.filekinds.symlink       = style,
            "pipe"             => self.filekinds.pipe          = style,
            "block-device"     => self.filekinds.block_device  = style,
            "char-device"      => self.filekinds.char_device   = style,
            "socket"           => self.filekinds.socket        = style,
            "special"          => self.filekinds.special       = style,
            "executable"       => self.filekinds.executable    = style,

            // Permissions
            "perm-user-read"   => self.perms.user_read          = style,
            "perm-user-write"  => self.perms.user_write         = style,
            "perm-user-exec"   => self.perms.user_execute_file  = style,
            "perm-user-exec-other" => self.perms.user_execute_other = style,
            "perm-group-read"  => self.perms.group_read         = style,
            "perm-group-write" => self.perms.group_write        = style,
            "perm-group-exec"  => self.perms.group_execute      = style,
            "perm-other-read"  => self.perms.other_read         = style,
            "perm-other-write" => self.perms.other_write        = style,
            "perm-other-exec"  => self.perms.other_execute      = style,
            "perm-special-user" => self.perms.special_user_file = style,
            "perm-special-other" => self.perms.special_other    = style,
            "perm-attribute"   => self.perms.attribute          = style,

            // Size (individual magnitudes)
            "size-number-byte" => self.size.number_byte = style,
            "size-number-kilo" => self.size.number_kilo = style,
            "size-number-mega" => self.size.number_mega = style,
            "size-number-giga" => self.size.number_giga = style,
            "size-number-huge" => self.size.number_huge = style,
            "size-unit-byte"   => self.size.unit_byte   = style,
            "size-unit-kilo"   => self.size.unit_kilo   = style,
            "size-unit-mega"   => self.size.unit_mega    = style,
            "size-unit-giga"   => self.size.unit_giga    = style,
            "size-unit-huge"   => self.size.unit_huge    = style,
            // Size (bulk setters)
            "size-number"      => self.set_number_style(style),
            "size-unit"        => self.set_unit_style(style),
            "size-major"       => self.size.major = style,
            "size-minor"       => self.size.minor = style,

            // Users
            "user-you"         => self.users.user_you          = style,
            "user-other"       => self.users.user_someone_else = style,
            "group-yours"      => self.users.group_yours       = style,
            "group-other"      => self.users.group_not_yours   = style,
            // UID / GID columns — if unset, they cascade from user/group.
            "uid-you"          => self.users.uid_you           = Some(style),
            "uid-other"        => self.users.uid_someone_else  = Some(style),
            "gid-yours"        => self.users.gid_yours         = Some(style),
            "gid-other"        => self.users.gid_not_yours     = Some(style),

            // Links
            "links"            => self.links.normal            = style,
            "links-multi"      => self.links.multi_link_file   = style,

            // VCS
            "vcs-new"          => self.vcs.new         = style,
            "vcs-modified"     => self.vcs.modified     = style,
            "vcs-deleted"      => self.vcs.deleted      = style,
            "vcs-renamed"      => self.vcs.renamed      = style,
            "vcs-typechange"   => self.vcs.typechange    = style,
            "vcs-ignored"      => self.vcs.ignored       = style,
            "vcs-conflicted"   => self.vcs.conflicted    = style,

            // UI elements
            "punctuation"      => self.punctuation      = style,
            "date"             => self.date              = style,
            "inode"            => self.inode             = style,
            "blocks"           => self.blocks            = style,
            "header"           => self.header            = style,
            "octal"            => self.octal             = style,
            "flags"            => self.flags             = style,
            "symlink-path"     => self.symlink_path      = style,
            "control-char"     => self.control_char      = style,
            "broken-symlink"   => self.broken_symlink    = style,
            "broken-overlay"   => self.broken_path_overlay = style,

            _ => return false,
        }

        true
    }
}