maram 0.2.0

A modern, high-performance alternative to the Unix tree command
Documentation
use std::fs::File;
use std::io::Write as _;

use maram::{
    collect, generate, generate_to_writer, render, render_to_writer, DistributionFormat,
    DistributionType, FilterOptions, MaramOptions, OutputFormat, SortBy,
};
use regex::Regex;

fn main() -> maram::Result<()> {
    let path = ".";

    // Build filter options
    let filter = FilterOptions {
        show_hidden: false,
        gitignore: true,
        max_depth: Some(3),
        max_dirs: Some(5),
        max_files: Some(10),
        sort_by: Some(SortBy::Name),
        reverse_sort: false,
        include: Some(Regex::new(r"\.(rs|toml|md)$").unwrap()),
        exclude: Some(Regex::new(r"(^|/)target(/|$)").unwrap()),
        ..Default::default()
    };

    // Build formatting and execution options
    let mut opts = MaramOptions { output: OutputFormat::Tree, filter, ..Default::default() };
    // tweak format/color preferences
    opts.format.unicode = true;
    opts.format.color = atty::is(atty::Stream::Stdout);
    opts.format.full_path = false;
    opts.format.show_size = true;
    // line counting, dir sizes, total summary
    opts = opts.with_line_counting(10 * 1024 * 1024).with_dir_sizes().with_total_size();
    // threads and distribution
    opts.threads = 0;
    opts.distribution = Some((DistributionType::Ext, 8, DistributionFormat::Table));

    // 1) One-shot generation as String
    let tree = generate(path, &opts)?;
    println!("{}", tree);

    // 2) Stream directly to a writer (e.g., a file)
    let mut tree_file = File::create("maram_tree.txt")?;
    generate_to_writer(path, &opts, &mut tree_file)?;
    writeln!(tree_file, "\n# Generated by maram")?;

    // 3) Collect structured entries, then render to different formats
    let entries = collect(path, &opts)?;

    // Render JSON as String
    let mut json_opts = opts.clone();
    json_opts.output = OutputFormat::Json;
    // distribution and totals are ignored for JSON/CSV
    let json = render(&entries, &json_opts)?;
    println!(
        "JSON output (truncated): {}",
        &json.chars().take(120).collect::<String>()
    );

    // Render CSV to a file
    let mut csv_opts = opts.clone();
    csv_opts.output = OutputFormat::Csv;
    let mut csv_file = File::create("maram_tree.csv")?;
    render_to_writer(&entries, &csv_opts, &mut csv_file)?;

    // 4) Plain list format
    let mut plain_opts = opts.clone();
    plain_opts.output = OutputFormat::Plain;
    let plain = render(&entries, &plain_opts)?;
    println!(
        "\nPlain list (truncated):\n{}",
        &plain.lines().take(10).collect::<Vec<_>>().join("\n")
    );

    Ok(())
}