summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 9727aad60ce1580381e986c44623adc4deb3f17c (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
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
#![cfg_attr(windows, feature(windows_by_handle))]
#![warn(
    clippy::all,
    clippy::cargo,
    clippy::complexity,
    clippy::correctness,
    clippy::nursery,
    clippy::pedantic,
    clippy::perf,
    clippy::style,
    clippy::suspicious
)]
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss,
    clippy::let_underscore_untyped,
    clippy::struct_excessive_bools,
    clippy::too_many_arguments
)]

use clap::CommandFactory;
use context::{layout, Context};
use progress::Message;
use render::{Engine, Flat, Inverted, Regular};
use std::{error::Error, io::stdout, process::ExitCode};
use tree::Tree;

/// Operations to wrangle ANSI escaped strings.
mod ansi;

/// CLI rules and definitions as well as context to be injected throughout the entire program.
mod context;

/// Operations relevant to the computation and presentation of disk usage.
mod disk_usage;

/// Filesystem operations.
mod fs;

/// All things related to icons on how to map certain files to the appropriate icons.
mod icons;

/// Concerned with displaying a progress indicator when stdout is a tty.
mod progress;

/// Concerned with taking an initialized [`Tree`] and its [`Node`]s and rendering the output.
///
/// [`Tree`]: tree::Tree
/// [`Node`]: tree::node::Node
mod render;

/// Global used throughout the program to paint the output.
mod styles;

/// Houses the primary data structures that are used to virtualize the filesystem, containing also
/// information on how the tree output should be ultimately rendered.
mod tree;

/// Utilities relating to interacting with tty properties.
mod tty;

/// Common utilities across all modules.
mod utils;

fn main() -> ExitCode {
    if let Err(e) = run() {
        eprintln!("{e}");
        return ExitCode::FAILURE;
    }
    ExitCode::SUCCESS
}

fn run() -> Result<(), Box<dyn Error>> {
    let ctx = Context::try_init()?;

    if let Some(shell) = ctx.completions {
        clap_complete::generate(shell, &mut Context::command(), "erd", &mut stdout());
        return Ok(());
    }

    context::color::no_color_env();
    styles::init(ctx.no_color());

    let indicator = (ctx.stdout_is_tty && !ctx.no_progress).then(progress::Indicator::measure);

    let (tree, ctx) =
        Tree::try_init_and_update_context(ctx, indicator.as_ref()).map_err(|err| {
            if let Some(ref progress) = indicator {
                progress.mailbox().send(Message::RenderReady).unwrap();
            }
            err
        })?;

    let output = match ctx.layout {
        layout::Type::Flat => {
            let render = Engine::<Flat>::new(tree, ctx);
            format!("{render}")
        }
        layout::Type::Inverted => {
            let render = Engine::<Inverted>::new(tree, ctx);
            format!("{render}")
        }
        layout::Type::Regular => {
            let render = Engine::<Regular>::new(tree, ctx);
            format!("{render}")
        }
    };

    if let Some(progress) = indicator {
        progress.mailbox().send(Message::RenderReady)?;
        progress.join_handle.join().unwrap()?;
    }

    println!("{output}");

    Ok(())
}