f00 0.10.0

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
//! User TOML configuration for f00.
//!
//! Search order (first found wins):
//! 1. `--config PATH` if provided
//! 2. `$F00_CONFIG` if set
//! 3. Platform config dir:
//!    - Unix: `$XDG_CONFIG_HOME/f00/config.toml` or `~/.config/f00/config.toml`
//!      (macOS via `directories`: `~/Library/Application Support/f00/config.toml`)
//!    - Windows: `%APPDATA%\f00\config.toml`

use std::fs;
use std::path::{Path, PathBuf};

use serde::Deserialize;

use crate::cli::{Args, ColorArg, IconsArg};
use f00_core::IconsWhen;

/// Optional defaults from a TOML config file.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(default)]
pub struct ConfigDefaults {
    pub all: Option<bool>,
    pub almost_all: Option<bool>,
    pub long: Option<bool>,
    #[serde(alias = "human")]
    pub human_readable: Option<bool>,
    pub color: Option<String>,
    /// Icons when: bool (`true`/`false`) or string (`auto`/`always`/`never`).
    #[serde(default, deserialize_with = "deserialize_opt_icons")]
    pub icons: Option<IconsArg>,
    pub dirs_first: Option<bool>,
    pub git: Option<bool>,
    pub classify: Option<bool>,
}

/// Root of `config.toml`. Fields may live under `[defaults]` or at the root.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(default)]
pub struct FileConfig {
    #[serde(default)]
    pub defaults: ConfigDefaults,
    // Root-level keys (take precedence over nested `[defaults]` when present).
    pub all: Option<bool>,
    pub almost_all: Option<bool>,
    pub long: Option<bool>,
    #[serde(alias = "human")]
    pub human_readable: Option<bool>,
    pub color: Option<String>,
    #[serde(default, deserialize_with = "deserialize_opt_icons")]
    pub icons: Option<IconsArg>,
    pub dirs_first: Option<bool>,
    pub git: Option<bool>,
    pub classify: Option<bool>,
}

impl FileConfig {
    /// Flatten root-level keys on top of `[defaults]`.
    pub fn resolved_defaults(&self) -> ConfigDefaults {
        ConfigDefaults {
            all: self.all.or(self.defaults.all),
            almost_all: self.almost_all.or(self.defaults.almost_all),
            long: self.long.or(self.defaults.long),
            human_readable: self.human_readable.or(self.defaults.human_readable),
            color: self.color.clone().or_else(|| self.defaults.color.clone()),
            icons: self.icons.or(self.defaults.icons),
            dirs_first: self.dirs_first.or(self.defaults.dirs_first),
            git: self.git.or(self.defaults.git),
            classify: self.classify.or(self.defaults.classify),
        }
    }
}

/// Parse TOML text into a [`FileConfig`].
pub fn parse_config_str(s: &str) -> Result<FileConfig, toml::de::Error> {
    toml::from_str(s)
}

/// Load config from an explicit path.
pub fn load_config_from_path(path: &Path) -> anyhow::Result<FileConfig> {
    let text = fs::read_to_string(path)
        .map_err(|e| anyhow::anyhow!("failed to read config {}: {e}", path.display()))?;
    parse_config_str(&text)
        .map_err(|e| anyhow::anyhow!("failed to parse config {}: {e}", path.display()))
}

/// Resolve the platform user config path (`…/f00/config.toml`), if available.
pub fn platform_config_path() -> Option<PathBuf> {
    directories::ProjectDirs::from("", "", "f00").map(|d| d.config_dir().join("config.toml"))
}

/// Paths to try when no `--config` override is given.
pub fn config_search_paths() -> Vec<PathBuf> {
    let mut paths = Vec::new();
    if let Ok(p) = std::env::var("F00_CONFIG") {
        if !p.is_empty() {
            paths.push(PathBuf::from(p));
        }
    }
    if let Some(p) = platform_config_path() {
        paths.push(p);
    }
    paths
}

/// Load config: explicit path, else first existing search path, else `None`.
pub fn load_user_config(explicit: Option<&Path>) -> anyhow::Result<Option<FileConfig>> {
    if let Some(path) = explicit {
        return Ok(Some(load_config_from_path(path)?));
    }
    for path in config_search_paths() {
        if path.is_file() {
            return Ok(Some(load_config_from_path(&path)?));
        }
    }
    Ok(None)
}

fn parse_color_arg(s: &str) -> Option<ColorArg> {
    match s.to_ascii_lowercase().as_str() {
        "auto" => Some(ColorArg::Auto),
        "always" | "yes" | "force" | "true" | "on" => Some(ColorArg::Always),
        "never" | "no" | "none" | "false" | "off" => Some(ColorArg::Never),
        _ => None,
    }
}

fn parse_icons_arg(s: &str) -> Option<IconsArg> {
    IconsWhen::parse(s).map(IconsArg::from)
}

/// Accept bool or string for `icons` in TOML (`true`/`false`/`"auto"`/`"always"`/`"never"`).
fn deserialize_opt_icons<'de, D>(deserializer: D) -> Result<Option<IconsArg>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::de::{self, Visitor};
    use std::fmt;

    struct IconsVisitor;

    impl<'de> Visitor<'de> for IconsVisitor {
        type Value = Option<IconsArg>;

        fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.write_str("a bool or icons when string (auto/always/never)")
        }

        fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
            Ok(None)
        }

        fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
            Ok(None)
        }

        fn visit_bool<E: de::Error>(self, v: bool) -> Result<Self::Value, E> {
            Ok(Some(if v { IconsArg::Always } else { IconsArg::Never }))
        }

        fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
            parse_icons_arg(v)
                .map(Some)
                .ok_or_else(|| de::Error::custom(format!("invalid icons value: {v}")))
        }

        fn visit_string<E: de::Error>(self, v: String) -> Result<Self::Value, E> {
            self.visit_str(&v)
        }
    }

    deserializer.deserialize_any(IconsVisitor)
}

/// Whether `flag` (e.g. `--icons` or `--git`) appears in process args.
pub fn cli_has_long(flag: &str) -> bool {
    let eq = format!("{flag}=");
    std::env::args().any(|a| a == flag || a.starts_with(&eq))
}

/// Merge file defaults into CLI args.
///
/// Precedence: built-in defaults < config < CLI flags (when set / non-default).
/// For false-default bools, config `true` enables.
/// For `git` (CLI default true), config applies unless `--git` was on the command line.
/// For `color` / `icons`, config applies when no explicit `--color` / `--icons` on the CLI.
pub fn merge_config_into_args(args: &mut Args, file: &FileConfig) {
    let d = file.resolved_defaults();

    if let Some(true) = d.all {
        args.all = true;
    }
    if let Some(true) = d.almost_all {
        args.almost_all = true;
    }
    if let Some(true) = d.long {
        args.long = true;
    }
    if let Some(true) = d.human_readable {
        args.human_readable = true;
    }
    if let Some(true) = d.classify {
        args.classify = true;
    }

    if let Some(v) = d.icons {
        if !cli_has_long("--icons") {
            args.icons = v;
        }
    }
    if let Some(v) = d.dirs_first {
        if v {
            args.dirs_first = true;
        } else if !cli_has_long("--dirs-first") {
            args.dirs_first = false;
        }
    }

    if let Some(v) = d.git {
        if !cli_has_long("--git") {
            args.git = v;
        }
    }

    if let Some(ref c) = d.color {
        if !cli_has_long("--color") {
            if let Some(parsed) = parse_color_arg(c) {
                args.color = parsed;
            }
        }
    }
}

/// Apply env overrides (`F00_GNU`).
pub fn apply_env_overrides(args: &mut Args) {
    if args.gnu {
        return;
    }
    if let Ok(v) = std::env::var("F00_GNU") {
        let v = v.to_ascii_lowercase();
        if matches!(v.as_str(), "1" | "true" | "yes" | "on") {
            args.gnu = true;
        }
    }
}

/// Detect whether the binary was invoked as `ls` / `ls.exe`.
pub fn invoked_as_ls() -> bool {
    invoked_as_ls_from(std::env::args_os().next())
}

/// Testable argv0 check.
pub fn invoked_as_ls_from(argv0: Option<std::ffi::OsString>) -> bool {
    let Some(argv0) = argv0 else {
        return false;
    };
    // Prefer Path (correct separators for the host OS).
    if let Some(stem) = Path::new(&argv0).file_stem().and_then(|s| s.to_str()) {
        if stem.eq_ignore_ascii_case("ls") {
            return true;
        }
    }
    // Also handle Windows-style paths when parsing on Unix (and vice versa).
    if let Some(s) = argv0.to_str() {
        let name = s.rsplit(['/', '\\']).next().unwrap_or(s);
        let stem = name
            .strip_suffix(".exe")
            .or_else(|| name.strip_suffix(".EXE"))
            .unwrap_or(name);
        return stem.eq_ignore_ascii_case("ls");
    }
    false
}

/// Resolve effective args after clap parse.
///
/// Order: argv0 soft defaults → config file → env (`F00_GNU`).
/// CLI flags already present in `args` win over config where merge logic allows.
pub fn resolve_args(args: &mut Args, file: Option<&FileConfig>, as_ls: bool) {
    if as_ls {
        // Icons / dirs_first off unless the user passed the flags (config may re-enable).
        let mut icons = IconsWhen::from(args.icons);
        f00_compat::prefer_ls_defaults(
            &mut icons,
            &mut args.dirs_first,
            cli_has_long("--icons"),
            cli_has_long("--dirs-first"),
        );
        args.icons = IconsArg::from(icons);
    }
    if let Some(file) = file {
        merge_config_into_args(args, file);
    }
    apply_env_overrides(args);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::{ColorArg, IconsArg};

    fn empty_args() -> Args {
        let mut a = Args::test_default();
        a.git = true;
        a.color = ColorArg::Auto;
        a
    }

    #[test]
    fn parse_defaults_section() {
        let cfg = parse_config_str(
            r#"
            [defaults]
            all = true
            icons = true
            color = "never"
            dirs_first = true
            git = false
            "#,
        )
        .unwrap();
        let d = cfg.resolved_defaults();
        assert_eq!(d.all, Some(true));
        assert_eq!(d.icons, Some(IconsArg::Always));
        assert_eq!(d.color.as_deref(), Some("never"));
        assert_eq!(d.dirs_first, Some(true));
        assert_eq!(d.git, Some(false));
    }

    #[test]
    fn parse_icons_when_string() {
        let cfg = parse_config_str(r#"icons = "auto""#).unwrap();
        assert_eq!(cfg.resolved_defaults().icons, Some(IconsArg::Auto));
        let cfg = parse_config_str(r#"icons = "never""#).unwrap();
        assert_eq!(cfg.resolved_defaults().icons, Some(IconsArg::Never));
    }

    #[test]
    fn parse_root_level_and_human_alias() {
        let cfg = parse_config_str(
            r#"
            long = true
            human = true
            classify = true
            "#,
        )
        .unwrap();
        let d = cfg.resolved_defaults();
        assert_eq!(d.long, Some(true));
        assert_eq!(d.human_readable, Some(true));
        assert_eq!(d.classify, Some(true));
    }

    #[test]
    fn merge_enables_flags_from_config() {
        let cfg = parse_config_str(
            r#"
            [defaults]
            all = true
            long = true
            icons = true
            human_readable = true
            "#,
        )
        .unwrap();
        let mut args = empty_args();
        merge_config_into_args(&mut args, &cfg);
        assert!(args.all);
        assert!(args.long);
        assert_eq!(args.icons, IconsArg::Always);
        assert!(args.human_readable);
    }

    #[test]
    fn merge_color_from_config() {
        let cfg = parse_config_str(r#"color = "never""#).unwrap();
        let mut args = empty_args();
        merge_config_into_args(&mut args, &cfg);
        assert!(matches!(args.color, ColorArg::Never));
    }

    #[test]
    fn merge_git_false_from_config() {
        let cfg = parse_config_str(r#"git = false"#).unwrap();
        let mut args = empty_args();
        assert!(args.git);
        merge_config_into_args(&mut args, &cfg);
        // Without --git on the real CLI of this test process, config applies.
        if !cli_has_long("--git") {
            assert!(!args.git);
        }
    }

    #[test]
    fn argv0_ls_detection() {
        assert!(invoked_as_ls_from(Some("ls".into())));
        assert!(invoked_as_ls_from(Some("/usr/bin/ls".into())));
        assert!(invoked_as_ls_from(Some(r"C:\bin\ls.exe".into())));
        assert!(invoked_as_ls_from(Some("LS".into())));
        assert!(!invoked_as_ls_from(Some("f00".into())));
        assert!(!invoked_as_ls_from(Some("/usr/local/bin/f00".into())));
        assert!(!invoked_as_ls_from(None));
    }

    #[test]
    fn platform_path_ends_with_config_toml() {
        if let Some(p) = platform_config_path() {
            assert_eq!(p.file_name().and_then(|s| s.to_str()), Some("config.toml"));
            assert!(p
                .components()
                .any(|c| c.as_os_str() == "f00" || c.as_os_str() == ".config"));
        }
    }

    #[test]
    fn resolve_ls_then_config_can_enable_icons() {
        let cfg = parse_config_str(r#"icons = true"#).unwrap();
        let mut args = empty_args();
        resolve_args(&mut args, Some(&cfg), true);
        assert_eq!(
            args.icons,
            IconsArg::Always,
            "config should re-enable icons under argv0 ls"
        );
    }
}