freshl 0.20260603.1

Modern ls replacement with git awareness
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
// Copyright © 2026 Michael Shields
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::cmp::Ordering;
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;

use crate::case::Sensitivity;
use crate::entry::{Entry, EntryKind};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SortKey {
    #[default]
    Name,
    Size,
    Time,
}

fn strip_leading_zeros(digits: &[u8]) -> &[u8] {
    let first = digits
        .iter()
        .position(|&b| b != b'0')
        .unwrap_or(digits.len());
    digits.get(first..).unwrap_or(&[])
}

fn compare_digit_runs(a: &[u8], b: &[u8]) -> Ordering {
    let a_sig = strip_leading_zeros(a);
    let b_sig = strip_leading_zeros(b);
    a_sig.len().cmp(&b_sig.len()).then_with(|| a_sig.cmp(b_sig))
}

fn compare_within_group(a: &Entry, b: &Entry, sensitivity: Sensitivity, key: SortKey) -> Ordering {
    match key {
        SortKey::Name => natural_cmp(&a.name, &b.name, sensitivity),
        // Size and Time ascend by default (smallest / oldest first), so
        // the largest / newest entries land at the bottom of the output.
        // `-r` flips this for users who want ls-style descending order.
        // Tie-break with natural name order so equal values stay readable.
        SortKey::Size => a
            .size
            .cmp(&b.size)
            .then_with(|| natural_cmp(&a.name, &b.name, sensitivity)),
        SortKey::Time => a
            .mtime
            .cmp(&b.mtime)
            .then_with(|| natural_cmp(&a.name, &b.name, sensitivity)),
    }
}

#[must_use]
pub fn compare_by(a: &Entry, b: &Entry, sensitivity: Sensitivity, key: SortKey) -> Ordering {
    match (
        a.kind == EntryKind::Directory,
        b.kind == EntryKind::Directory,
    ) {
        (true, false) => Ordering::Less,
        (false, true) => Ordering::Greater,
        _ => compare_within_group(a, b, sensitivity, key),
    }
}

#[must_use]
pub fn compare(a: &Entry, b: &Entry, sensitivity: Sensitivity) -> Ordering {
    compare_by(a, b, sensitivity, SortKey::Name)
}

pub fn sort_with(entries: &mut [Entry], sensitivity: Sensitivity, key: SortKey, reverse: bool) {
    // -r reverses the within-group order only; directories stay grouped at
    // the top regardless of the requested key.
    entries.sort_by(|a, b| {
        let a_dir = a.kind == EntryKind::Directory;
        let b_dir = b.kind == EntryKind::Directory;
        match (a_dir, b_dir) {
            (true, false) => Ordering::Less,
            (false, true) => Ordering::Greater,
            _ => {
                let ord = compare_within_group(a, b, sensitivity, key);
                if reverse { ord.reverse() } else { ord }
            }
        }
    });
}

pub fn sort(entries: &mut [Entry], sensitivity: Sensitivity) {
    sort_with(entries, sensitivity, SortKey::Name, false);
}

/// Compare two byte strings using natural order (digit runs as numbers).
///
/// # Panics
///
/// Does not panic: internal `.expect()` sites hold by construction.
#[must_use]
pub fn natural_cmp(a: &OsStr, b: &OsStr, sensitivity: Sensitivity) -> Ordering {
    let mut i = 0;
    let mut j = 0;
    let ab = a.as_bytes();
    let bb = b.as_bytes();
    while let (Some(&ca), Some(&cb)) = (ab.get(i), bb.get(j)) {
        if ca.is_ascii_digit() && cb.is_ascii_digit() {
            let ai = digit_run_end(ab, i);
            let bj = digit_run_end(bb, j);
            let a_run = ab.get(i..ai).expect("digit_run_end keeps i..ai in bounds");
            let b_run = bb.get(j..bj).expect("digit_run_end keeps j..bj in bounds");
            match compare_digit_runs(a_run, b_run) {
                Ordering::Equal => {
                    i = ai;
                    j = bj;
                }
                other => return other,
            }
        } else {
            let (ka, kb) = match sensitivity {
                Sensitivity::Sensitive => (ca, cb),
                Sensitivity::Insensitive => (ca.to_ascii_lowercase(), cb.to_ascii_lowercase()),
            };
            match ka.cmp(&kb) {
                Ordering::Equal => {
                    i += 1;
                    j += 1;
                }
                other => return other,
            }
        }
    }
    // After the loop, at least one cursor has reached its slice's end. If
    // both have, fall back to lexicographic byte comparison so distinct names
    // that compare equal under the natural rule (e.g. `02a0` vs `2a00`) still
    // get a stable total order; otherwise the exhausted side is the shorter
    // (and therefore smaller) name.
    if i == ab.len() && j == bb.len() {
        ab.len().cmp(&bb.len()).then_with(|| ab.cmp(bb))
    } else if i == ab.len() {
        Ordering::Less
    } else {
        Ordering::Greater
    }
}

fn digit_run_end(bytes: &[u8], start: usize) -> usize {
    let mut i = start;
    while let Some(&b) = bytes.get(i) {
        if !b.is_ascii_digit() {
            break;
        }
        i += 1;
    }
    i
}

#[cfg(test)]
mod tests {
    use super::{SortKey, compare, natural_cmp, sort, sort_with};
    use crate::case::Sensitivity;
    use crate::entry::{Entry, EntryKind};
    use std::cmp::Ordering;
    use std::ffi::{OsStr, OsString};
    use std::path::PathBuf;
    use std::time::{Duration, SystemTime};

    fn entry(name: &str, kind: EntryKind) -> Entry {
        Entry {
            name: OsString::from(name),
            path: PathBuf::from(name),
            kind,
            mode: 0,
            nlink: 0,
            uid: 0,
            gid: 0,
            size: 0,
            rdev: 0,
            mtime: SystemTime::UNIX_EPOCH,
            dev: 0,
            ino: 0,
            follow_chain: Vec::new(),
        }
    }

    fn file_with_size(name: &str, size: u64) -> Entry {
        Entry {
            size,
            ..entry(name, EntryKind::RegularFile)
        }
    }

    fn file_with_mtime(name: &str, secs_after_epoch: u64) -> Entry {
        Entry {
            mtime: SystemTime::UNIX_EPOCH + Duration::from_secs(secs_after_epoch),
            ..entry(name, EntryKind::RegularFile)
        }
    }

    fn names(v: &[Entry]) -> Vec<String> {
        v.iter()
            .map(|e| e.name.to_string_lossy().into_owned())
            .collect()
    }

    #[test]
    fn natural_order_treats_digit_runs_as_numbers() {
        let cmp = |x, y| natural_cmp(OsStr::new(x), OsStr::new(y), Sensitivity::Sensitive);
        assert_eq!(cmp("f2", "f10"), Ordering::Less);
        assert_eq!(cmp("f10", "f2"), Ordering::Greater);
        assert_eq!(cmp("f02", "f2"), Ordering::Greater);
        assert_eq!(cmp("f2", "f02"), Ordering::Less);
        assert_eq!(cmp("a", "a"), Ordering::Equal);
        assert_eq!(cmp("a", "ab"), Ordering::Less);
        assert_eq!(cmp("ab", "a"), Ordering::Greater);
    }

    #[test]
    fn natural_order_compares_non_digit_chars() {
        let cmp = |x, y| natural_cmp(OsStr::new(x), OsStr::new(y), Sensitivity::Sensitive);
        assert_eq!(cmp("a", "b"), Ordering::Less);
        assert_eq!(cmp("z", "a"), Ordering::Greater);
        assert_eq!(cmp("a1", "b1"), Ordering::Less);
        assert_eq!(cmp("Ab", "ab"), Ordering::Less);
    }

    #[test]
    fn natural_order_insensitive_folds_ascii() {
        let cmp = |x, y| natural_cmp(OsStr::new(x), OsStr::new(y), Sensitivity::Insensitive);
        // Case-fold makes "Ab" and "ab" compare equal naturally, but the
        // raw-bytes tie-break breaks the tie deterministically so sorting
        // stays total.
        assert_eq!(cmp("Ab", "ab"), Ordering::Less);
        assert_eq!(cmp("AB", "ac"), Ordering::Less);
    }

    #[test]
    fn natural_order_mixed_letter_then_digit() {
        let cmp = |x, y| natural_cmp(OsStr::new(x), OsStr::new(y), Sensitivity::Sensitive);
        assert_eq!(cmp("a1", "ab"), Ordering::Less);
        assert_eq!(cmp("ab", "a1"), Ordering::Greater);
    }

    #[test]
    fn directories_sort_before_files() {
        let d = entry("z", EntryKind::Directory);
        let f = entry("a", EntryKind::RegularFile);
        assert_eq!(compare(&d, &f, Sensitivity::Sensitive), Ordering::Less);
        assert_eq!(compare(&f, &d, Sensitivity::Sensitive), Ordering::Greater);
    }

    #[test]
    fn symlink_to_file_sorts_with_files() {
        let link = entry("a_link", EntryKind::Symlink);
        let real = entry("z_dir", EntryKind::Directory);
        assert_eq!(
            compare(&real, &link, Sensitivity::Sensitive),
            Ordering::Less
        );
        assert_eq!(
            compare(&link, &real, Sensitivity::Sensitive),
            Ordering::Greater
        );
    }

    #[test]
    fn broken_symlink_sorts_with_files() {
        let broken = entry("a_broken", EntryKind::Symlink);
        let file = entry("b_file", EntryKind::RegularFile);
        let dir = entry("z_dir", EntryKind::Directory);
        assert_eq!(
            compare(&broken, &file, Sensitivity::Sensitive),
            Ordering::Less
        );
        assert_eq!(
            compare(&dir, &broken, Sensitivity::Sensitive),
            Ordering::Less
        );
    }

    #[test]
    fn within_a_group_natural_order_applies() {
        let a = entry("file2", EntryKind::RegularFile);
        let b = entry("file10", EntryKind::RegularFile);
        assert_eq!(compare(&a, &b, Sensitivity::Sensitive), Ordering::Less);

        let da = entry("Dir2", EntryKind::Directory);
        let db = entry("dir10", EntryKind::Directory);
        assert_eq!(compare(&da, &db, Sensitivity::Insensitive), Ordering::Less);
    }

    #[test]
    fn sort_orders_directories_first_then_natural() {
        let mut v = vec![
            entry("file2", EntryKind::RegularFile),
            entry("dir10", EntryKind::Directory),
            entry("file10", EntryKind::RegularFile),
            entry("dir2", EntryKind::Directory),
        ];
        sort(&mut v, Sensitivity::Sensitive);
        let names: Vec<_> = v
            .iter()
            .map(|e| e.name.to_string_lossy().into_owned())
            .collect();
        assert_eq!(names, vec!["dir2", "dir10", "file2", "file10"]);
    }

    #[test]
    fn zero_padded_runs_compare_by_string_length_when_value_matches() {
        let cmp = |x, y| natural_cmp(OsStr::new(x), OsStr::new(y), Sensitivity::Sensitive);
        // Same numeric value (10) but different padding: the longer raw byte
        // form sorts later so the order is stable.
        assert_eq!(cmp("a010", "a0010"), Ordering::Less);
        assert_eq!(cmp("a0010", "a010"), Ordering::Greater);
    }

    #[test]
    fn unequal_tails_after_matching_digits_compare_by_remaining() {
        let cmp = |x, y| natural_cmp(OsStr::new(x), OsStr::new(y), Sensitivity::Sensitive);
        // Loop exits with one side's digit run exhausting the slice while the
        // other has non-digit bytes left; the side with leftover content is
        // longer and must sort after the exhausted one.
        assert_eq!(cmp("a0010", "a010a"), Ordering::Less);
        assert_eq!(cmp("a010a", "a0010"), Ordering::Greater);
    }

    #[test]
    fn enormous_digit_runs_do_not_panic() {
        let huge = "9".repeat(100);
        let other = "9".repeat(101);
        let result = natural_cmp(
            OsStr::new(&huge),
            OsStr::new(&other),
            Sensitivity::Sensitive,
        );
        assert_eq!(result, Ordering::Less);
    }

    #[test]
    fn size_key_sorts_files_smallest_first_with_name_tiebreak() {
        let mut v = vec![
            file_with_size("alpha", 100),
            file_with_size("zeta", 999),
            file_with_size("beta", 100),
        ];
        sort_with(&mut v, Sensitivity::Sensitive, SortKey::Size, false);
        assert_eq!(names(&v), vec!["alpha", "beta", "zeta"]);
    }

    #[test]
    fn time_key_sorts_files_oldest_first_with_name_tiebreak() {
        let mut v = vec![
            file_with_mtime("alpha", 100),
            file_with_mtime("zeta", 999),
            file_with_mtime("beta", 100),
        ];
        sort_with(&mut v, Sensitivity::Sensitive, SortKey::Time, false);
        assert_eq!(names(&v), vec!["alpha", "beta", "zeta"]);
    }

    #[test]
    fn directories_stay_grouped_first_under_size_key() {
        let mut v = vec![
            file_with_size("huge", 10_000_000),
            Entry {
                size: 4096,
                ..entry("tiny_dir", EntryKind::Directory)
            },
        ];
        sort_with(&mut v, Sensitivity::Sensitive, SortKey::Size, false);
        // Even though the file is larger, the directory still leads.
        assert_eq!(names(&v), vec!["tiny_dir", "huge"]);
    }

    #[test]
    fn directories_stay_grouped_first_under_time_key() {
        let mut v = vec![
            file_with_mtime("newer_file", 9_999),
            Entry {
                mtime: SystemTime::UNIX_EPOCH + Duration::from_secs(1),
                ..entry("older_dir", EntryKind::Directory)
            },
        ];
        sort_with(&mut v, Sensitivity::Sensitive, SortKey::Time, false);
        assert_eq!(names(&v), vec!["older_dir", "newer_file"]);
    }

    #[test]
    fn reverse_keeps_directories_first_but_reverses_within_groups() {
        let mut v = vec![
            entry("dir_a", EntryKind::Directory),
            entry("dir_b", EntryKind::Directory),
            entry("file_a", EntryKind::RegularFile),
            entry("file_b", EntryKind::RegularFile),
        ];
        sort_with(&mut v, Sensitivity::Sensitive, SortKey::Name, true);
        assert_eq!(names(&v), vec!["dir_b", "dir_a", "file_b", "file_a"]);
    }

    #[test]
    fn reverse_with_size_yields_descending_within_files() {
        let mut v = vec![
            file_with_size("small", 1),
            file_with_size("mid", 100),
            file_with_size("big", 9_999),
        ];
        sort_with(&mut v, Sensitivity::Sensitive, SortKey::Size, true);
        assert_eq!(names(&v), vec!["big", "mid", "small"]);
    }

    #[test]
    fn reverse_with_time_yields_newest_first_within_files() {
        let mut v = vec![
            file_with_mtime("old", 1),
            file_with_mtime("mid", 100),
            file_with_mtime("new", 9_999),
        ];
        sort_with(&mut v, Sensitivity::Sensitive, SortKey::Time, true);
        assert_eq!(names(&v), vec!["new", "mid", "old"]);
    }

    #[test]
    fn equal_size_files_break_tie_by_name_naturally() {
        let mut v = vec![
            file_with_size("file10", 42),
            file_with_size("file2", 42),
            file_with_size("file1", 42),
        ];
        sort_with(&mut v, Sensitivity::Sensitive, SortKey::Size, false);
        assert_eq!(names(&v), vec!["file1", "file2", "file10"]);
    }

    #[test]
    fn sort_wrapper_matches_sort_with_default_key() {
        let mut v1 = vec![
            entry("zeta", EntryKind::RegularFile),
            entry("alpha", EntryKind::RegularFile),
        ];
        let mut v2 = v1.clone();
        sort(&mut v1, Sensitivity::Sensitive);
        sort_with(&mut v2, Sensitivity::Sensitive, SortKey::Name, false);
        assert_eq!(names(&v1), names(&v2));
    }
}