eza 0.23.4

A modern replacement for ls
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
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
// SPDX-FileCopyrightText: 2024 Christina Sørensen
// SPDX-License-Identifier: EUPL-1.2
//
// SPDX-FileCopyrightText: 2023-2024 Christina Sørensen, eza contributors
// SPDX-FileCopyrightText: 2014 Benjamin Sago
// SPDX-License-Identifier: MIT
use std::cmp::max;
use std::ops::Deref;
#[cfg(unix)]
use std::sync::{Mutex, MutexGuard};

use chrono::prelude::*;

use log::debug;
use std::sync::LazyLock;
#[cfg(unix)]
use uzers::UsersCache;

use crate::fs::feature::git::GitCache;
use crate::fs::{fields as f, File};
use crate::options::vars::EZA_WINDOWS_ATTRIBUTES;
use crate::options::Vars;
use crate::output::cell::TextCell;
use crate::output::color_scale::ColorScaleInformation;
#[cfg(unix)]
use crate::output::render::{GroupRender, OctalPermissionsRender, UserRender};
use crate::output::render::{PermissionsPlusRender, TimeRender};
use crate::output::time::TimeFormat;
use crate::theme::Theme;

use super::color_scale::ColorScaleMode;

/// Options for displaying a table.
#[derive(PartialEq, Eq, Debug)]
pub struct Options {
    pub size_format: SizeFormat,
    pub time_format: TimeFormat,
    pub user_format: UserFormat,
    pub group_format: GroupFormat,
    pub flags_format: FlagsFormat,
    pub columns: Columns,
}

/// Extra columns to display in the table.
#[allow(clippy::struct_excessive_bools)]
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub struct Columns {
    /// At least one of these timestamps will be shown.
    pub time_types: TimeTypes,

    // The rest are just on/off
    pub inode: bool,
    pub links: bool,
    pub blocksize: bool,
    pub group: bool,
    pub git: bool,
    pub subdir_git_repos: bool,
    pub subdir_git_repos_no_stat: bool,
    pub octal: bool,
    pub security_context: bool,
    pub file_flags: bool,

    // Defaults to true:
    pub permissions: bool,
    pub filesize: bool,
    pub user: bool,
}

impl Columns {
    #[must_use]
    pub fn collect(&self, actually_enable_git: bool, git_repos: bool) -> Vec<Column> {
        let mut columns = Vec::with_capacity(4);

        if self.inode {
            #[cfg(unix)]
            columns.push(Column::Inode);
        }

        if self.octal {
            #[cfg(unix)]
            columns.push(Column::Octal);
        }

        if self.permissions {
            columns.push(Column::Permissions);
        }

        if self.links {
            #[cfg(unix)]
            columns.push(Column::HardLinks);
        }

        if self.filesize {
            columns.push(Column::FileSize);
        }

        if self.blocksize {
            #[cfg(unix)]
            columns.push(Column::Blocksize);
        }

        if self.user {
            #[cfg(unix)]
            columns.push(Column::User);
        }

        if self.group {
            #[cfg(unix)]
            columns.push(Column::Group);
        }

        if self.file_flags {
            columns.push(Column::FileFlags);
        }

        #[cfg(target_os = "linux")]
        if self.security_context {
            columns.push(Column::SecurityContext);
        }

        if self.time_types.modified {
            columns.push(Column::Timestamp(TimeType::Modified));
        }

        if self.time_types.changed {
            columns.push(Column::Timestamp(TimeType::Changed));
        }

        if self.time_types.created {
            columns.push(Column::Timestamp(TimeType::Created));
        }

        if self.time_types.accessed {
            columns.push(Column::Timestamp(TimeType::Accessed));
        }

        if self.git && actually_enable_git {
            columns.push(Column::GitStatus);
        }

        if self.subdir_git_repos && git_repos {
            columns.push(Column::SubdirGitRepo(true));
        }

        if self.subdir_git_repos_no_stat && git_repos {
            columns.push(Column::SubdirGitRepo(false));
        }

        columns
    }
}

/// A table contains these.
#[derive(Debug, Copy, Clone)]
pub enum Column {
    Permissions,
    FileSize,
    Timestamp(TimeType),
    #[cfg(unix)]
    Blocksize,
    #[cfg(unix)]
    User,
    #[cfg(unix)]
    Group,
    #[cfg(unix)]
    HardLinks,
    #[cfg(unix)]
    Inode,
    GitStatus,
    SubdirGitRepo(bool),
    #[cfg(unix)]
    Octal,
    #[cfg(unix)]
    SecurityContext,
    FileFlags,
}

/// Each column can pick its own **Alignment**. Usually, numbers are
/// right-aligned, and text is left-aligned.
#[derive(Copy, Clone)]
pub enum Alignment {
    Left,
    Right,
}

impl Column {
    /// Get the alignment this column should use.
    #[cfg(unix)]
    #[must_use]
    pub fn alignment(self) -> Alignment {
        #[allow(clippy::wildcard_in_or_patterns)]
        match self {
            Self::FileSize | Self::HardLinks | Self::Inode | Self::Blocksize | Self::GitStatus => {
                Alignment::Right
            }
            Self::Timestamp(_) | _ => Alignment::Left,
        }
    }

    #[cfg(windows)]
    pub fn alignment(self) -> Alignment {
        match self {
            Self::FileSize | Self::GitStatus => Alignment::Right,
            _ => Alignment::Left,
        }
    }

    /// Get the text that should be printed at the top, when the user elects
    /// to have a header row printed.
    #[must_use]
    pub fn header(self) -> &'static str {
        match self {
            #[cfg(unix)]
            Self::Permissions => "Permissions",
            #[cfg(windows)]
            Self::Permissions => "Mode",
            Self::FileSize => "Size",
            Self::Timestamp(t) => t.header(),
            #[cfg(unix)]
            Self::Blocksize => "Blocksize",
            #[cfg(unix)]
            Self::User => "User",
            #[cfg(unix)]
            Self::Group => "Group",
            #[cfg(unix)]
            Self::HardLinks => "Links",
            #[cfg(unix)]
            Self::Inode => "inode",
            Self::GitStatus => "Git",
            Self::SubdirGitRepo(_) => "Git Repo",
            #[cfg(unix)]
            Self::Octal => "Octal",
            #[cfg(unix)]
            Self::SecurityContext => "Security Context",
            Self::FileFlags => "Flags",
        }
    }
}

/// Formatting options for file sizes.
#[allow(clippy::enum_variant_names)]
#[derive(PartialEq, Eq, Debug, Default, Copy, Clone)]
pub enum SizeFormat {
    /// Format the file size using **decimal** prefixes, such as “kilo”,
    /// “mega”, or “giga”.
    #[default]
    DecimalBytes,

    /// Format the file size using **binary** prefixes, such as “kibi”,
    /// “mebi”, or “gibi”.
    BinaryBytes,

    /// Do no formatting and just display the size as a number of bytes.
    JustBytes,
}

/// Formatting options for user and group.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum UserFormat {
    /// The UID / GID
    Numeric,
    /// Show the name
    Name,
}

/// Formatting options for group only.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum GroupFormat {
    /// Numeric or text value
    Regular,
    /// Show ":" if user-group value is the same
    Smart,
}

/// The types of a file’s time fields. These three fields are standard
/// across most (all?) operating systems.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub enum TimeType {
    /// The file’s modified time (`st_mtime`).
    Modified,

    /// The file’s changed time (`st_ctime`)
    Changed,

    /// The file’s accessed time (`st_atime`).
    Accessed,

    /// The file’s creation time (`btime` or `birthtime`).
    Created,
}

impl TimeType {
    /// Returns the text to use for a column’s heading in the columns output.
    #[must_use]
    pub fn header(self) -> &'static str {
        match self {
            Self::Modified => "Date Modified",
            Self::Changed => "Date Changed",
            Self::Accessed => "Date Accessed",
            Self::Created => "Date Created",
        }
    }

    /// Returns the corresponding time from [File]
    pub fn get_corresponding_time(self, file: &File<'_>) -> Option<NaiveDateTime> {
        match self {
            TimeType::Modified => file.modified_time(),
            TimeType::Changed => file.changed_time(),
            TimeType::Accessed => file.accessed_time(),
            TimeType::Created => file.created_time(),
        }
    }
}

/// How display file flags.
#[derive(PartialEq, Eq, Debug, Default, Copy, Clone)]
pub enum FlagsFormat {
    /// Display flags as comma seperated descriptions
    #[default]
    Long,
    /// Display flags as single character abbreviations (Windows only)
    Short,
}

impl FlagsFormat {
    pub(crate) fn deduce<V: Vars>(vars: &V) -> FlagsFormat {
        vars.get(EZA_WINDOWS_ATTRIBUTES)
            .and_then(|v| match v.to_ascii_lowercase().to_str() {
                Some("short") => Some(FlagsFormat::Short),
                Some("long") => Some(FlagsFormat::Long),
                _ => None,
            })
            .unwrap_or_default()
    }
}

/// Fields for which of a file’s time fields should be displayed in the
/// columns output.
///
/// There should always be at least one of these — there’s no way to disable
/// the time columns entirely (yet).
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
#[rustfmt::skip]
#[allow(clippy::struct_excessive_bools)]
pub struct TimeTypes {
    pub modified: bool,
    pub changed:  bool,
    pub accessed: bool,
    pub created:  bool,
}

impl Default for TimeTypes {
    /// By default, display just the ‘modified’ time. This is the most
    /// common option, which is why it has this shorthand.
    fn default() -> Self {
        Self {
            modified: true,
            changed: false,
            accessed: false,
            created: false,
        }
    }
}

/// The **environment** struct contains any data that could change between
/// running instances of exa, depending on the user’s computer’s configuration.
///
/// Any environment field should be able to be mocked up for test runs.
pub struct Environment {
    /// The computer’s current time offset, determined from time zone.
    time_offset: FixedOffset,

    /// Localisation rules for formatting numbers.
    numeric: locale::Numeric,

    /// Mapping cache of user IDs to usernames.
    #[cfg(unix)]
    users: Mutex<UsersCache>,
}

impl Environment {
    #[cfg(unix)]
    pub fn lock_users(&self) -> MutexGuard<'_, UsersCache> {
        self.users.lock().unwrap()
    }

    fn load_all() -> Self {
        let time_offset = *Local::now().offset();

        let numeric =
            locale::Numeric::load_user_locale().unwrap_or_else(|_| locale::Numeric::english());

        #[cfg(unix)]
        let users = Mutex::new(UsersCache::new());

        Self {
            time_offset,
            numeric,
            #[cfg(unix)]
            users,
        }
    }
}

static ENVIRONMENT: LazyLock<Environment> = LazyLock::new(Environment::load_all);

pub struct Table<'a> {
    columns: Vec<Column>,
    theme: &'a Theme,
    env: &'a Environment,
    widths: TableWidths,
    time_format: TimeFormat,
    size_format: SizeFormat,
    #[cfg(unix)]
    user_format: UserFormat,
    #[cfg(unix)]
    group_format: GroupFormat,
    flags_format: FlagsFormat,
    git: Option<&'a GitCache>,
}

#[derive(Clone)]
pub struct Row {
    cells: Vec<TextCell>,
}

impl<'a> Table<'a> {
    #[must_use]
    pub fn new(
        options: &'a Options,
        git: Option<&'a GitCache>,
        theme: &'a Theme,
        git_repos: bool,
    ) -> Table<'a> {
        let columns = options.columns.collect(git.is_some(), git_repos);
        let widths = TableWidths::zero(columns.len());
        let env = &*ENVIRONMENT;

        debug!("Creating table with columns: {columns:?}");

        Table {
            theme,
            widths,
            columns,
            git,
            env,
            time_format: options.time_format.clone(),
            size_format: options.size_format,
            #[cfg(unix)]
            user_format: options.user_format,
            #[cfg(unix)]
            group_format: options.group_format,
            flags_format: options.flags_format,
        }
    }

    #[must_use]
    pub fn widths(&self) -> &TableWidths {
        &self.widths
    }

    #[must_use]
    pub fn header_row(&self) -> Row {
        let cells = self
            .columns
            .iter()
            .map(|c| TextCell::paint_str(self.theme.ui.header.unwrap_or_default(), c.header()))
            .collect();

        Row { cells }
    }

    pub fn row_for_file(
        &self,
        file: &File<'_>,
        xattrs: bool,
        color_scale_info: Option<ColorScaleInformation>,
    ) -> Row {
        let cells = self
            .columns
            .iter()
            .map(|c| self.display(file, *c, xattrs, color_scale_info))
            .collect();

        Row { cells }
    }

    pub fn add_widths(&mut self, row: &Row) {
        self.widths.add_widths(row);
    }

    #[cfg(unix)]
    fn permissions_plus(&self, file: &File<'_>, xattrs: bool) -> Option<f::PermissionsPlus> {
        file.permissions().map(|p| f::PermissionsPlus {
            file_type: file.type_char(),
            permissions: p,
            xattrs,
        })
    }

    #[allow(clippy::unnecessary_wraps)] // Needs to match Unix function
    #[cfg(windows)]
    fn permissions_plus(&self, file: &File<'_>, xattrs: bool) -> Option<f::PermissionsPlus> {
        Some(f::PermissionsPlus {
            file_type: file.type_char(),
            #[cfg(windows)]
            attributes: file.attributes()?,
            xattrs,
        })
    }

    #[cfg(unix)]
    fn octal_permissions(&self, file: &File<'_>) -> Option<f::OctalPermissions> {
        file.permissions()
            .map(|p| f::OctalPermissions { permissions: p })
    }

    fn display(
        &self,
        file: &File<'_>,
        column: Column,
        xattrs: bool,
        color_scale_info: Option<ColorScaleInformation>,
    ) -> TextCell {
        match column {
            Column::Permissions => self.permissions_plus(file, xattrs).render(self.theme),
            Column::FileSize => file.size().render(
                self.theme,
                self.size_format,
                &self.env.numeric,
                color_scale_info,
            ),
            #[cfg(unix)]
            Column::HardLinks => file.links().render(self.theme, &self.env.numeric),
            #[cfg(unix)]
            Column::Inode => file.inode().render(self.theme.ui.inode.unwrap_or_default()),
            #[cfg(unix)]
            Column::Blocksize => {
                file.blocksize()
                    .render(self.theme, self.size_format, &self.env.numeric)
            }
            #[cfg(unix)]
            Column::User => {
                file.user()
                    .render(self.theme, &*self.env.lock_users(), self.user_format)
            }
            #[cfg(unix)]
            Column::Group => file.group().render(
                self.theme,
                &*self.env.lock_users(),
                self.user_format,
                self.group_format,
                file.user(),
            ),
            #[cfg(unix)]
            Column::SecurityContext => file.security_context().render(self.theme),
            Column::FileFlags => file
                .flags()
                .render(self.theme.ui.flags.unwrap_or_default(), self.flags_format),
            Column::GitStatus => self.git_status(file).render(self.theme),
            Column::SubdirGitRepo(status) => self.subdir_git_repo(file, status).render(self.theme),
            #[cfg(unix)]
            Column::Octal => self
                .octal_permissions(file)
                .render(self.theme.ui.octal.unwrap_or_default()),

            Column::Timestamp(time_type) => time_type.get_corresponding_time(file).render(
                if color_scale_info.is_some_and(|csi| csi.options.mode == ColorScaleMode::Gradient)
                {
                    color_scale_info.unwrap().apply_time_gradient(
                        self.theme.ui.date.unwrap_or_default(),
                        file,
                        time_type,
                    )
                } else {
                    self.theme.ui.date.unwrap_or_default()
                },
                self.env.time_offset,
                self.time_format.clone(),
            ),
        }
    }

    fn git_status(&self, file: &File<'_>) -> f::Git {
        debug!("Getting Git status for file {:?}", file.path);

        self.git
            .map(|g| g.get(&file.path, file.is_directory()))
            .unwrap_or_default()
    }

    fn subdir_git_repo(&self, file: &File<'_>, status: bool) -> f::SubdirGitRepo {
        debug!("Getting subdir repo status for path {:?}", file.path);

        if file.is_directory() {
            return f::SubdirGitRepo::from_path(&file.path, status);
        }
        f::SubdirGitRepo::default()
    }

    #[must_use]
    pub fn render(&self, row: Row) -> TextCell {
        let mut cell = TextCell::default();

        let iter = row.cells.into_iter().zip(self.widths.iter()).enumerate();

        for (n, (this_cell, width)) in iter {
            let padding = width - *this_cell.width;

            match self.columns[n].alignment() {
                Alignment::Left => {
                    cell.append(this_cell);
                    cell.add_spaces(padding);
                }
                Alignment::Right => {
                    cell.add_spaces(padding);
                    cell.append(this_cell);
                }
            }

            cell.add_spaces(1);
        }

        cell
    }
}

pub struct TableWidths(Vec<usize>);

impl Deref for TableWidths {
    type Target = [usize];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl TableWidths {
    #[must_use]
    pub fn zero(count: usize) -> Self {
        Self(vec![0; count])
    }

    pub fn add_widths(&mut self, row: &Row) {
        for (old_width, cell) in self.0.iter_mut().zip(row.cells.iter()) {
            *old_width = max(*old_width, *cell.width);
        }
    }

    #[must_use]
    pub fn total(&self) -> usize {
        self.0.len() + self.0.iter().sum::<usize>()
    }
}