aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/aft-ls.rs
blob: 3118c08ce019d37cbb7c36f502297f50e1c15363 (plain)
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

use std::{fs,env,path};
use std::error::Error;
use std::os::unix::fs::MetadataExt;

fn main() -> Result<(), Box<dyn Error>> {

    let args: Vec<String> = env::args().collect();
    let dir = match args.len() {
        0 | 1 => path::Path::new("."),
        _other => path::Path::new(&args[2]),
    };

    // header is basically a constant
    print!("\x01");
    println!("{}\x1D", ["file_name", "size", "mtime"].join("\x1E"));
    println!("{}\x1D", ["string", "integer+bytes", "datetime"].join("\x1E"));
    print!("\x02");

    for entry in fs::read_dir(dir)? {
            let entry = entry?;
            let metadata = entry.metadata()?;
            let filename: String = entry.file_name()
                .into_string()
                .or_else(|_| Err(format!("Invalid entry")))?;
            let mtime = chrono::DateTime::<chrono::Utc>::from_utc(
                chrono::NaiveDateTime::from_timestamp(metadata.mtime(), 0), chrono::Utc);
            println!("{}\x1D",
                [
                 filename,
                 metadata.size().to_string(),
                 mtime.to_rfc3339_opts(chrono::SecondsFormat::AutoSi, true),
                ].join("\x1E"));
    }
    Ok(())
}