Skip to main content

f00_cli/
run.rs

1use std::io::{self, IsTerminal, Write};
2use std::path::PathBuf;
3use std::time::Instant;
4
5use anyhow::{Context, Result};
6use f00_compat::{apply_gnu_list_options, apply_gnu_output, parse_format_word, parse_sort_word};
7#[cfg(feature = "archives")]
8use f00_core::list_path;
9use f00_core::{
10    list_paths_with_errors, BlockSize, CliSymlinkMode, Config, ControlChars, HyperlinkWhen,
11    IndicatorStyle, ListOptions, ListOutcome, ListTiming, OutputMode, QuotingStyle, SortBy,
12    TimeField, TimeStyle,
13};
14use f00_format::format_listings;
15
16use crate::cli::Args;
17use crate::config::{load_user_config, resolve_args};
18
19/// Detect terminal width; fall back to 80.
20fn terminal_width() -> usize {
21    std::env::var("COLUMNS")
22        .ok()
23        .and_then(|s| s.parse().ok())
24        .filter(|&n: &usize| n > 0)
25        .or_else(|| terminal_size::terminal_size().map(|(terminal_size::Width(w), _)| w as usize))
26        .unwrap_or(80)
27}
28
29fn resolve_sort(args: &Args) -> SortBy {
30    if let Some(ref word) = args.sort {
31        if let Some(s) = parse_sort_word(word) {
32            return s;
33        }
34    }
35    if args.sort_none || args.unsorted_all {
36        SortBy::None
37    } else if args.sort_version {
38        SortBy::Version
39    } else if args.sort_time || args.access_time || args.change_time {
40        SortBy::Time
41    } else if args.sort_size {
42        SortBy::Size
43    } else if args.sort_extension {
44        SortBy::Extension
45    } else {
46        SortBy::Name
47    }
48}
49
50fn resolve_time_field(args: &Args) -> TimeField {
51    if let Some(ref word) = args.time {
52        return match word.to_ascii_lowercase().as_str() {
53            "atime" | "access" | "use" => TimeField::Accessed,
54            "ctime" | "status" | "change" => TimeField::Changed,
55            "birth" | "creation" => TimeField::Birth,
56            _ => TimeField::Modified,
57        };
58    }
59    if args.access_time {
60        TimeField::Accessed
61    } else if args.change_time {
62        TimeField::Changed
63    } else {
64        TimeField::Modified
65    }
66}
67
68fn resolve_output(args: &Args) -> OutputMode {
69    if let Some(ref word) = args.format {
70        if let Some(m) = parse_format_word(word) {
71            return m;
72        }
73    }
74    if args.csv {
75        OutputMode::Csv
76    } else if args.tsv {
77        OutputMode::Tsv
78    } else if args.json {
79        OutputMode::Json
80    } else if args.tree {
81        OutputMode::Tree
82    } else if args.long
83        || args.no_owner
84        || args.no_group_long
85        || args.numeric_uid_gid
86        || args.author
87    {
88        // -g/-o/-n/--author imply long format in GNU ls
89        OutputMode::Long
90    } else if args.one_per_line || args.zero {
91        // --zero implies -1 in GNU ls
92        OutputMode::OnePerLine
93    } else if args.commas {
94        OutputMode::Commas
95    } else if args.across {
96        OutputMode::Across
97    } else if args.columns {
98        OutputMode::Columns
99    } else {
100        OutputMode::Default
101    }
102}
103
104fn resolve_indicator(args: &Args, is_tty: bool) -> IndicatorStyle {
105    if let Some(ref word) = args.indicator_style {
106        if let Some(s) = IndicatorStyle::parse(word) {
107            return s;
108        }
109    }
110    // GNU: `-F` / `--classify[=WHEN]` — WHEN uses the same vocabulary as `--color`.
111    if let Some(when) = args.classify {
112        let when = f00_core::ColorWhen::from(when);
113        if when.enabled(is_tty) {
114            return IndicatorStyle::Classify;
115        }
116        // `--classify=never` or auto on non-TTY: do not classify (still honor -p / --file-type).
117    }
118    if args.file_type {
119        IndicatorStyle::FileType
120    } else if args.indicator_slash {
121        IndicatorStyle::Slash
122    } else {
123        IndicatorStyle::None
124    }
125}
126
127fn resolve_quoting(args: &Args) -> QuotingStyle {
128    if args.literal {
129        return QuotingStyle::Literal;
130    }
131    if args.escape {
132        return QuotingStyle::Escape;
133    }
134    if args.quote_name {
135        return QuotingStyle::C;
136    }
137    if let Some(ref word) = args.quoting_style {
138        if let Some(s) = QuotingStyle::parse(word) {
139            return s;
140        }
141    }
142    QuotingStyle::from_env().unwrap_or(QuotingStyle::Literal)
143}
144
145fn resolve_control_chars(args: &Args) -> ControlChars {
146    if args.hide_control_chars {
147        ControlChars::Hide
148    } else if args.show_control_chars {
149        ControlChars::Show
150    } else {
151        ControlChars::Auto
152    }
153}
154
155fn resolve_time_style(args: &Args) -> TimeStyle {
156    if args.full_time {
157        return TimeStyle::FullIso;
158    }
159    if let Some(ref s) = args.time_style {
160        if let Some(ts) = TimeStyle::parse(s) {
161            return ts;
162        }
163    }
164    TimeStyle::from_env().unwrap_or(TimeStyle::Locale)
165}
166
167fn resolve_block_size(args: &Args) -> BlockSize {
168    if let Some(ref s) = args.block_size {
169        if let Some(bs) = BlockSize::parse(s) {
170            return bs;
171        }
172    }
173    if args.human_readable {
174        BlockSize::HumanBinary
175    } else if args.si {
176        BlockSize::HumanSi
177    } else {
178        BlockSize::Bytes(1)
179    }
180}
181
182fn resolve_hyperlink(args: &Args) -> HyperlinkWhen {
183    match args.hyperlink {
184        None => HyperlinkWhen::Never,
185        Some(crate::cli::ColorArg::Auto) => HyperlinkWhen::Auto,
186        Some(crate::cli::ColorArg::Always) => HyperlinkWhen::Always,
187        Some(crate::cli::ColorArg::Never) => HyperlinkWhen::Never,
188    }
189}
190
191fn resolve_cli_symlink(args: &Args) -> CliSymlinkMode {
192    if args.dereference {
193        // -L supersedes -H
194        return CliSymlinkMode::Never; // handled via follow_links
195    }
196    if args.dereference_command_line {
197        CliSymlinkMode::Always
198    } else if args.dereference_command_line_symlink_to_dir {
199        CliSymlinkMode::DirOnly
200    } else {
201        CliSymlinkMode::Never
202    }
203}
204
205fn resolve_width(args: &Args) -> usize {
206    if let Some(w) = args.width {
207        return w;
208    }
209    terminal_width()
210}
211
212pub fn build_config(args: &Args) -> Config {
213    let is_stdout_tty = io::stdout().is_terminal();
214    let sort_by = resolve_sort(args);
215    let mut output = resolve_output(args);
216    output = apply_gnu_output(output, is_stdout_tty, args.gnu);
217
218    let mut all = args.all || args.unsorted_all;
219    let mut almost_all = args.almost_all;
220
221    let mut list = ListOptions {
222        all,
223        almost_all: almost_all || all,
224        sort_by,
225        reverse: args.reverse,
226        // Honor explicit `--group-directories-first` even under `--gnu` (GNU does).
227        dirs_first: args.dirs_first,
228        recursive: (args.recursive || args.tree) && !args.directory,
229        max_depth: args.max_depth,
230        gnu_mode: args.gnu,
231        follow_links: args.dereference,
232        directory: args.directory,
233        ignore_backups: args.ignore_backups,
234        ignore_patterns: args.ignore.clone(),
235        hide_patterns: args.hide.clone(),
236        use_ignore_files: args.ignore_files && !args.no_ignore && !args.gnu,
237        list_archives: args.archive && !args.gnu && !args.directory,
238        time_field: resolve_time_field(args),
239        cli_symlink: resolve_cli_symlink(args),
240        // `--threads 1` forces serial; `0` = auto rayon; `N>1` = fixed pool.
241        parallel: args.threads != 1,
242        threads: args.threads,
243        collect_timing: args.profile,
244        // Filled after we know output mode (long / -Z need expensive fields).
245        resolve_owner_group: false,
246        read_selinux: false,
247        linux_statx: true,
248        io_uring: cfg!(feature = "io-uring"),
249        // Adjusted below for `--tree` (headers not needed).
250        emit_dir_headers: true,
251    };
252
253    apply_gnu_list_options(&mut list, args.gnu);
254
255    // Classic ls: `-a` includes `.`/`..`; almost_all alone does not.
256    if all {
257        list.all = true;
258        list.almost_all = false;
259    } else if almost_all {
260        list.almost_all = true;
261        list.all = false;
262    }
263
264    // Strict GNU / -f: no icons, no git decorations.
265    let icons = if args.gnu || args.unsorted_all {
266        false
267    } else {
268        f00_core::IconsWhen::from(args.icons).enabled(is_stdout_tty)
269    };
270    let show_git = args.git && !args.gnu && !args.unsorted_all;
271
272    // -g implies long without owner; -o implies long without group.
273    let show_owner = !args.no_owner;
274    let show_group = !(args.no_group || args.no_group_long);
275
276    if args.full_time && !matches!(output, OutputMode::Long) {
277        output = OutputMode::Long;
278    }
279    // -Z alone often implies long in some ls builds; we show context in all modes.
280    if args.dired && !matches!(output, OutputMode::Long | OutputMode::OnePerLine) {
281        // dired is most useful with long; keep chosen mode otherwise.
282    }
283
284    // Expensive metadata only when the presentation needs it.
285    // JSON/CSV/TSV are machine dumps: resolve owner/group names unless `-n`.
286    let machine = matches!(output, OutputMode::Json | OutputMode::Csv | OutputMode::Tsv);
287    let needs_names = (matches!(output, OutputMode::Long)
288        && ((show_owner && !args.numeric_uid_gid) || (show_group && !args.numeric_uid_gid)))
289        || (machine && !args.numeric_uid_gid);
290    list.resolve_owner_group = needs_names || args.author;
291    list.read_selinux = args.context;
292    list.linux_statx = true;
293    list.io_uring = args.io_uring && cfg!(feature = "io-uring");
294    // Tree does not use section headers; skip them for less work and cleaner depths.
295    list.emit_dir_headers = !matches!(output, OutputMode::Tree);
296
297    let _ = (&mut all, &mut almost_all);
298
299    // --zero disables color and hyperlinks in GNU ls.
300    let color = if args.zero {
301        f00_core::ColorWhen::Never
302    } else {
303        args.color.into()
304    };
305
306    let mut hyperlink = resolve_hyperlink(args);
307    if args.zero {
308        hyperlink = HyperlinkWhen::Never;
309    }
310
311    let indicator = resolve_indicator(args, is_stdout_tty);
312
313    Config {
314        list,
315        output,
316        color,
317        human_sizes: args.human_readable || args.si,
318        si_sizes: args.si,
319        icons,
320        classify: matches!(indicator, IndicatorStyle::Classify),
321        indicator,
322        terminal_width: resolve_width(args),
323        is_stdout_tty,
324        show_owner,
325        show_group,
326        numeric_uid_gid: args.numeric_uid_gid,
327        show_inode: args.inode,
328        show_blocks: args.size_blocks,
329        full_time: args.full_time,
330        show_git,
331        quoting_style: resolve_quoting(args),
332        control_chars: resolve_control_chars(args),
333        show_author: args.author,
334        block_size: resolve_block_size(args),
335        kibibytes: args.kibibytes,
336        tabsize: args.tabsize.unwrap_or(8),
337        hyperlink,
338        show_context: args.context,
339        zero: args.zero,
340        dired: args.dired,
341        time_style: resolve_time_style(args),
342        // Per-listing override in format_listings for file operands.
343        emit_block_total: true,
344    }
345}
346
347/// Prepare args: load config, apply argv0 / env merges. Returns owned Args.
348pub fn prepare_args(mut args: Args, as_ls: bool) -> Result<Args> {
349    let file = load_user_config(args.config.as_deref())?;
350    resolve_args(&mut args, file.as_ref(), as_ls);
351    // Strict GNU disables git unless user re-enables after — config may set git=true;
352    // apply_gnu at build_config handles decorations. Also force git=false when --gnu.
353    if args.gnu {
354        args.git = false;
355        args.icons = crate::cli::IconsArg::Never;
356        // Do **not** clear `dirs_first` here: `--group-directories-first` must
357        // still work in GNU mode (coreutils accepts it). Default stays off.
358    }
359    Ok(args)
360}
361
362/// Run the lister. Returns a GNU-aligned exit code: 0 / 1 / 2.
363pub fn run(args: Args) -> Result<i32> {
364    run_with_argv0(args, false)
365}
366
367/// Run with explicit argv0-as-ls mode (for tests / main).
368pub fn run_with_argv0(args: Args, as_ls: bool) -> Result<i32> {
369    let args = prepare_args(args, as_ls)?;
370
371    // Interactive browser (feature-gated).
372    if args.browse {
373        #[cfg(feature = "tui")]
374        {
375            let start = args
376                .paths
377                .first()
378                .map(PathBuf::as_path)
379                .unwrap_or_else(|| std::path::Path::new("."));
380            let is_tty = io::stdout().is_terminal();
381            let icons = !args.gnu && f00_core::IconsWhen::from(args.icons).enabled(is_tty);
382            let code = f00_tui::run_browser(
383                start,
384                f00_tui::BrowserOptions {
385                    show_hidden: args.almost_all || args.all,
386                    icons,
387                    git: args.git && !args.gnu,
388                },
389            )?;
390            return Ok(code);
391        }
392        #[cfg(not(feature = "tui"))]
393        {
394            anyhow::bail!("TUI browser requires building with --features tui");
395        }
396    }
397
398    let config = build_config(&args);
399    let paths: Vec<PathBuf> = if args.paths.is_empty() {
400        vec![PathBuf::from(".")]
401    } else {
402        args.paths.clone()
403    };
404
405    let t_total = Instant::now();
406    let outcome = list_paths_with_archives(&paths, &config.list);
407    let core_timing = outcome.total_timing();
408
409    for err in &outcome.path_errors {
410        eprintln!("f00: {err}");
411    }
412
413    let exit_code = outcome.exit_code();
414    #[cfg(feature = "git")]
415    let listings = {
416        let mut listings = outcome.listings;
417        if config.show_git && args.git {
418            f00_git::annotate_listings(&mut listings);
419        }
420        #[cfg(feature = "plugins")]
421        {
422            crate::plugins_cmd::decorate_listings(listings)
423        }
424        #[cfg(not(feature = "plugins"))]
425        {
426            listings
427        }
428    };
429    #[cfg(all(not(feature = "git"), feature = "plugins"))]
430    let listings = crate::plugins_cmd::decorate_listings(outcome.listings);
431    #[cfg(all(not(feature = "git"), not(feature = "plugins")))]
432    let listings = outcome.listings;
433
434    let mut format_ms = 0u128;
435    if !listings.is_empty() {
436        let t_fmt = Instant::now();
437        let rendered = format_listings(&listings, &config).map_err(|e| anyhow::anyhow!(e))?;
438        format_ms = t_fmt.elapsed().as_millis();
439
440        let mut stdout = io::stdout().lock();
441        stdout
442            .write_all(rendered.as_bytes())
443            .context("writing output")?;
444    } else if exit_code == 0 {
445        eprintln!("f00: no listings produced");
446        return Ok(2);
447    }
448
449    if args.profile {
450        print_profile(&core_timing, format_ms, t_total.elapsed().as_millis());
451    }
452
453    Ok(exit_code)
454}
455
456fn print_profile(core: &ListTiming, format_ms: u128, total_ms: u128) {
457    eprintln!(
458        "f00 profile: readdir_ms={} stat_ms={} sort_ms={} format_ms={} total_ms={}",
459        core.readdir_ms, core.stat_ms, core.sort_ms, format_ms, total_ms
460    );
461}
462
463/// Like [`list_paths_with_errors`], but expands zip/tar when `list_archives` is set.
464fn list_paths_with_archives(paths: &[PathBuf], opts: &ListOptions) -> ListOutcome {
465    #[cfg(feature = "archives")]
466    {
467        if opts.list_archives {
468            let mut listings = Vec::new();
469            let mut path_errors = Vec::new();
470            let mut minor = 0usize;
471            for p in paths {
472                if f00_archive::is_archive(p) {
473                    match f00_archive::list_archive_as_listing(p) {
474                        Ok(l) => {
475                            minor += l.minor_errors;
476                            listings.push(l);
477                        }
478                        Err(e) => path_errors.push(f00_core::Error::Io(std::io::Error::other(
479                            format!("{}: {e}", p.display()),
480                        ))),
481                    }
482                } else {
483                    match list_path(p, opts) {
484                        Ok(l) => {
485                            minor += l.minor_errors;
486                            listings.push(l);
487                        }
488                        Err(e) => path_errors.push(e),
489                    }
490                }
491            }
492            return ListOutcome {
493                listings,
494                path_errors,
495                minor_errors: minor,
496            };
497        }
498    }
499    let _ = opts.list_archives;
500    list_paths_with_errors(paths, opts)
501}