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 = ".";
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()
};
let mut opts = MaramOptions { output: OutputFormat::Tree, filter, ..Default::default() };
opts.format.unicode = true;
opts.format.color = atty::is(atty::Stream::Stdout);
opts.format.full_path = false;
opts.format.show_size = true;
opts = opts.with_line_counting(10 * 1024 * 1024).with_dir_sizes().with_total_size();
opts.threads = 0;
opts.distribution = Some((DistributionType::Ext, 8, DistributionFormat::Table));
let tree = generate(path, &opts)?;
println!("{}", tree);
let mut tree_file = File::create("maram_tree.txt")?;
generate_to_writer(path, &opts, &mut tree_file)?;
writeln!(tree_file, "\n# Generated by maram")?;
let entries = collect(path, &opts)?;
let mut json_opts = opts.clone();
json_opts.output = OutputFormat::Json;
let json = render(&entries, &json_opts)?;
println!(
"JSON output (truncated): {}",
&json.chars().take(120).collect::<String>()
);
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)?;
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(())
}