f00 0.7.2

f00 — a modern, friendly directory lister (ls rewrite)
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
use std::io::{self, IsTerminal, Write};
use std::path::PathBuf;
use std::time::Instant;

use anyhow::{Context, Result};
use f00_compat::{apply_gnu_list_options, apply_gnu_output, parse_format_word, parse_sort_word};
#[cfg(feature = "archives")]
use f00_core::list_path;
use f00_core::{
    list_paths_with_errors, BlockSize, CliSymlinkMode, Config, ControlChars, HyperlinkWhen,
    IndicatorStyle, ListOptions, ListOutcome, ListTiming, OutputMode, QuotingStyle, SortBy,
    TimeField, TimeStyle,
};
use f00_format::format_listings;

use crate::cli::Args;
use crate::config::{load_user_config, resolve_args};

/// Detect terminal width; fall back to 80.
fn terminal_width() -> usize {
    std::env::var("COLUMNS")
        .ok()
        .and_then(|s| s.parse().ok())
        .filter(|&n: &usize| n > 0)
        .or_else(|| terminal_size::terminal_size().map(|(terminal_size::Width(w), _)| w as usize))
        .unwrap_or(80)
}

fn resolve_sort(args: &Args) -> SortBy {
    if let Some(ref word) = args.sort {
        if let Some(s) = parse_sort_word(word) {
            return s;
        }
    }
    if args.sort_none || args.unsorted_all {
        SortBy::None
    } else if args.sort_version {
        SortBy::Version
    } else if args.sort_time || args.access_time || args.change_time {
        SortBy::Time
    } else if args.sort_size {
        SortBy::Size
    } else if args.sort_extension {
        SortBy::Extension
    } else {
        SortBy::Name
    }
}

fn resolve_time_field(args: &Args) -> TimeField {
    if let Some(ref word) = args.time {
        return match word.to_ascii_lowercase().as_str() {
            "atime" | "access" | "use" => TimeField::Accessed,
            "ctime" | "status" | "change" => TimeField::Changed,
            "birth" | "creation" => TimeField::Birth,
            _ => TimeField::Modified,
        };
    }
    if args.access_time {
        TimeField::Accessed
    } else if args.change_time {
        TimeField::Changed
    } else {
        TimeField::Modified
    }
}

fn resolve_output(args: &Args) -> OutputMode {
    if let Some(ref word) = args.format {
        if let Some(m) = parse_format_word(word) {
            return m;
        }
    }
    if args.csv {
        OutputMode::Csv
    } else if args.tsv {
        OutputMode::Tsv
    } else if args.json {
        OutputMode::Json
    } else if args.tree {
        OutputMode::Tree
    } else if args.long
        || args.no_owner
        || args.no_group_long
        || args.numeric_uid_gid
        || args.author
    {
        // -g/-o/-n/--author imply long format in GNU ls
        OutputMode::Long
    } else if args.one_per_line || args.zero {
        // --zero implies -1 in GNU ls
        OutputMode::OnePerLine
    } else if args.commas {
        OutputMode::Commas
    } else if args.across {
        OutputMode::Across
    } else if args.columns {
        OutputMode::Columns
    } else {
        OutputMode::Default
    }
}

fn resolve_indicator(args: &Args) -> IndicatorStyle {
    if let Some(ref word) = args.indicator_style {
        if let Some(s) = IndicatorStyle::parse(word) {
            return s;
        }
    }
    if args.classify {
        IndicatorStyle::Classify
    } else if args.file_type {
        IndicatorStyle::FileType
    } else if args.indicator_slash {
        IndicatorStyle::Slash
    } else {
        IndicatorStyle::None
    }
}

fn resolve_quoting(args: &Args) -> QuotingStyle {
    if args.literal {
        return QuotingStyle::Literal;
    }
    if args.escape {
        return QuotingStyle::Escape;
    }
    if args.quote_name {
        return QuotingStyle::C;
    }
    if let Some(ref word) = args.quoting_style {
        if let Some(s) = QuotingStyle::parse(word) {
            return s;
        }
    }
    QuotingStyle::from_env().unwrap_or(QuotingStyle::Literal)
}

fn resolve_control_chars(args: &Args) -> ControlChars {
    if args.hide_control_chars {
        ControlChars::Hide
    } else if args.show_control_chars {
        ControlChars::Show
    } else {
        ControlChars::Auto
    }
}

fn resolve_time_style(args: &Args) -> TimeStyle {
    if args.full_time {
        return TimeStyle::FullIso;
    }
    if let Some(ref s) = args.time_style {
        if let Some(ts) = TimeStyle::parse(s) {
            return ts;
        }
    }
    TimeStyle::from_env().unwrap_or(TimeStyle::Locale)
}

fn resolve_block_size(args: &Args) -> BlockSize {
    if let Some(ref s) = args.block_size {
        if let Some(bs) = BlockSize::parse(s) {
            return bs;
        }
    }
    if args.human_readable {
        BlockSize::HumanBinary
    } else if args.si {
        BlockSize::HumanSi
    } else {
        BlockSize::Bytes(1)
    }
}

fn resolve_hyperlink(args: &Args) -> HyperlinkWhen {
    match args.hyperlink.as_deref() {
        None => HyperlinkWhen::Never,
        Some(s) => HyperlinkWhen::parse(s).unwrap_or(HyperlinkWhen::Always),
    }
}

fn resolve_cli_symlink(args: &Args) -> CliSymlinkMode {
    if args.dereference {
        // -L supersedes -H
        return CliSymlinkMode::Never; // handled via follow_links
    }
    if args.dereference_command_line {
        CliSymlinkMode::Always
    } else if args.dereference_command_line_symlink_to_dir {
        CliSymlinkMode::DirOnly
    } else {
        CliSymlinkMode::Never
    }
}

fn resolve_width(args: &Args) -> usize {
    if let Some(w) = args.width {
        return w;
    }
    terminal_width()
}

pub fn build_config(args: &Args) -> Config {
    let is_stdout_tty = io::stdout().is_terminal();
    let sort_by = resolve_sort(args);
    let mut output = resolve_output(args);
    output = apply_gnu_output(output, is_stdout_tty, args.gnu);

    let mut all = args.all || args.unsorted_all;
    let mut almost_all = args.almost_all;

    let mut list = ListOptions {
        all,
        almost_all: almost_all || all,
        sort_by,
        reverse: args.reverse,
        dirs_first: args.dirs_first && !args.gnu,
        recursive: (args.recursive || args.tree) && !args.directory,
        max_depth: args.max_depth,
        gnu_mode: args.gnu,
        follow_links: args.dereference,
        directory: args.directory,
        ignore_backups: args.ignore_backups,
        ignore_patterns: args.ignore.clone(),
        hide_patterns: args.hide.clone(),
        use_ignore_files: args.ignore_files && !args.no_ignore && !args.gnu,
        list_archives: args.archive && !args.gnu && !args.directory,
        time_field: resolve_time_field(args),
        cli_symlink: resolve_cli_symlink(args),
        // `--threads 1` forces serial; `0` = auto rayon; `N>1` = fixed pool.
        parallel: args.threads != 1,
        threads: args.threads,
        collect_timing: args.profile,
        // Filled after we know output mode (long / -Z need expensive fields).
        resolve_owner_group: false,
        read_selinux: false,
        linux_statx: true,
        io_uring: cfg!(feature = "io-uring"),
        // Adjusted below for `--tree` (headers not needed).
        emit_dir_headers: true,
    };

    apply_gnu_list_options(&mut list, args.gnu);

    // Classic ls: `-a` includes `.`/`..`; almost_all alone does not.
    if all {
        list.all = true;
        list.almost_all = false;
    } else if almost_all {
        list.almost_all = true;
        list.all = false;
    }

    // Strict GNU / -f: no icons, no git decorations.
    let icons = if args.gnu || args.unsorted_all {
        false
    } else {
        f00_core::IconsWhen::from(args.icons).enabled(is_stdout_tty)
    };
    let show_git = args.git && !args.gnu && !args.unsorted_all;

    // -g implies long without owner; -o implies long without group.
    let show_owner = !args.no_owner;
    let show_group = !(args.no_group || args.no_group_long);

    if args.full_time && !matches!(output, OutputMode::Long) {
        output = OutputMode::Long;
    }
    // -Z alone often implies long in some ls builds; we show context in all modes.
    if args.dired && !matches!(output, OutputMode::Long | OutputMode::OnePerLine) {
        // dired is most useful with long; keep chosen mode otherwise.
    }

    // Expensive metadata only when the presentation needs it.
    // JSON/CSV/TSV are machine dumps: resolve owner/group names unless `-n`.
    let machine = matches!(output, OutputMode::Json | OutputMode::Csv | OutputMode::Tsv);
    let needs_names = (matches!(output, OutputMode::Long)
        && ((show_owner && !args.numeric_uid_gid) || (show_group && !args.numeric_uid_gid)))
        || (machine && !args.numeric_uid_gid);
    list.resolve_owner_group = needs_names || args.author;
    list.read_selinux = args.context;
    list.linux_statx = true;
    list.io_uring = args.io_uring && cfg!(feature = "io-uring");
    // Tree does not use section headers; skip them for less work and cleaner depths.
    list.emit_dir_headers = !matches!(output, OutputMode::Tree);

    let _ = (&mut all, &mut almost_all);

    // --zero disables color and hyperlinks in GNU ls.
    let color = if args.zero {
        f00_core::ColorWhen::Never
    } else {
        args.color.into()
    };

    let mut hyperlink = resolve_hyperlink(args);
    if args.zero {
        hyperlink = HyperlinkWhen::Never;
    }

    Config {
        list,
        output,
        color,
        human_sizes: args.human_readable || args.si,
        si_sizes: args.si,
        icons,
        classify: args.classify,
        indicator: resolve_indicator(args),
        terminal_width: resolve_width(args),
        is_stdout_tty,
        show_owner,
        show_group,
        numeric_uid_gid: args.numeric_uid_gid,
        show_inode: args.inode,
        show_blocks: args.size_blocks,
        full_time: args.full_time,
        show_git,
        quoting_style: resolve_quoting(args),
        control_chars: resolve_control_chars(args),
        show_author: args.author,
        block_size: resolve_block_size(args),
        kibibytes: args.kibibytes,
        tabsize: args.tabsize.unwrap_or(8),
        hyperlink,
        show_context: args.context,
        zero: args.zero,
        dired: args.dired,
        time_style: resolve_time_style(args),
    }
}

/// Prepare args: load config, apply argv0 / env merges. Returns owned Args.
pub fn prepare_args(mut args: Args, as_ls: bool) -> Result<Args> {
    let file = load_user_config(args.config.as_deref())?;
    resolve_args(&mut args, file.as_ref(), as_ls);
    // Strict GNU disables git unless user re-enables after — config may set git=true;
    // apply_gnu at build_config handles decorations. Also force git=false when --gnu.
    if args.gnu {
        args.git = false;
        args.icons = crate::cli::IconsArg::Never;
        args.dirs_first = false;
    }
    Ok(args)
}

/// Run the lister. Returns a GNU-aligned exit code: 0 / 1 / 2.
pub fn run(args: Args) -> Result<i32> {
    run_with_argv0(args, false)
}

/// Run with explicit argv0-as-ls mode (for tests / main).
pub fn run_with_argv0(args: Args, as_ls: bool) -> Result<i32> {
    let args = prepare_args(args, as_ls)?;

    // Interactive browser (feature-gated).
    if args.browse {
        #[cfg(feature = "tui")]
        {
            let start = args
                .paths
                .first()
                .map(PathBuf::as_path)
                .unwrap_or_else(|| std::path::Path::new("."));
            let is_tty = io::stdout().is_terminal();
            let icons = !args.gnu && f00_core::IconsWhen::from(args.icons).enabled(is_tty);
            let code = f00_tui::run_browser(
                start,
                f00_tui::BrowserOptions {
                    show_hidden: args.almost_all || args.all,
                    icons,
                    git: args.git && !args.gnu,
                },
            )?;
            return Ok(code);
        }
        #[cfg(not(feature = "tui"))]
        {
            anyhow::bail!("TUI browser requires building with --features tui");
        }
    }

    let config = build_config(&args);
    let paths: Vec<PathBuf> = if args.paths.is_empty() {
        vec![PathBuf::from(".")]
    } else {
        args.paths.clone()
    };

    let t_total = Instant::now();
    let outcome = list_paths_with_archives(&paths, &config.list);
    let core_timing = outcome.total_timing();

    for err in &outcome.path_errors {
        eprintln!("f00: {err}");
    }

    let exit_code = outcome.exit_code();
    #[cfg(feature = "git")]
    let listings = {
        let mut listings = outcome.listings;
        if config.show_git && args.git {
            f00_git::annotate_listings(&mut listings);
        }
        #[cfg(feature = "plugins")]
        {
            crate::plugins_cmd::decorate_listings(listings)
        }
        #[cfg(not(feature = "plugins"))]
        {
            listings
        }
    };
    #[cfg(all(not(feature = "git"), feature = "plugins"))]
    let listings = crate::plugins_cmd::decorate_listings(outcome.listings);
    #[cfg(all(not(feature = "git"), not(feature = "plugins")))]
    let listings = outcome.listings;

    let mut format_ms = 0u128;
    if !listings.is_empty() {
        let t_fmt = Instant::now();
        let rendered = format_listings(&listings, &config).map_err(|e| anyhow::anyhow!(e))?;
        format_ms = t_fmt.elapsed().as_millis();

        let mut stdout = io::stdout().lock();
        stdout
            .write_all(rendered.as_bytes())
            .context("writing output")?;
    } else if exit_code == 0 {
        eprintln!("f00: no listings produced");
        return Ok(2);
    }

    if args.profile {
        print_profile(&core_timing, format_ms, t_total.elapsed().as_millis());
    }

    Ok(exit_code)
}

fn print_profile(core: &ListTiming, format_ms: u128, total_ms: u128) {
    eprintln!(
        "f00 profile: readdir_ms={} stat_ms={} sort_ms={} format_ms={} total_ms={}",
        core.readdir_ms, core.stat_ms, core.sort_ms, format_ms, total_ms
    );
}

/// Like [`list_paths_with_errors`], but expands zip/tar when `list_archives` is set.
fn list_paths_with_archives(paths: &[PathBuf], opts: &ListOptions) -> ListOutcome {
    #[cfg(feature = "archives")]
    {
        if opts.list_archives {
            let mut listings = Vec::new();
            let mut path_errors = Vec::new();
            let mut minor = 0usize;
            for p in paths {
                if f00_archive::is_archive(p) {
                    match f00_archive::list_archive_as_listing(p) {
                        Ok(l) => {
                            minor += l.minor_errors;
                            listings.push(l);
                        }
                        Err(e) => path_errors.push(f00_core::Error::Io(std::io::Error::other(
                            format!("{}: {e}", p.display()),
                        ))),
                    }
                } else {
                    match list_path(p, opts) {
                        Ok(l) => {
                            minor += l.minor_errors;
                            listings.push(l);
                        }
                        Err(e) => path_errors.push(e),
                    }
                }
            }
            return ListOutcome {
                listings,
                path_errors,
                minor_errors: minor,
            };
        }
    }
    let _ = opts.list_archives;
    list_paths_with_errors(paths, opts)
}