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
pub mod sub;
pub use sub::Sub;
use crate::{
args::{Args, Quantity},
reporter::{ErrorOnlyReporter, ErrorReport, ProgressAndErrorReporter, ProgressReport},
runtime_error::RuntimeError,
size::{Bytes, Size},
size_getters::GET_APPARENT_SIZE,
visualizer::Direction,
};
use std::time::Duration;
use structopt_utilities::StructOptUtils;
#[cfg(unix)]
use crate::{
size::Blocks,
size_getters::{GET_BLOCK_COUNT, GET_BLOCK_SIZE},
};
pub struct App {
args: Args,
}
impl App {
pub fn from_env() -> Self {
App {
args: Args::strict_from_args(),
}
}
pub fn run(self) -> Result<(), RuntimeError> {
let column_width_distribution = self.args.column_width_distribution();
let report_error = if self.args.silent_errors {
ErrorReport::SILENT
} else {
ErrorReport::TEXT
};
fn error_only_reporter<Data: Size>(
report_error: fn(ErrorReport),
) -> ErrorOnlyReporter<fn(ErrorReport)> {
ErrorOnlyReporter::new(report_error)
}
fn progress_and_error_reporter<Data>(
report_error: fn(ErrorReport),
) -> ProgressAndErrorReporter<Data, fn(ErrorReport)>
where
Data: Size + Into<u64> + Send + Sync,
ProgressReport<Data>: Default + 'static,
{
ProgressAndErrorReporter::new(
ProgressReport::TEXT,
Duration::from_millis(100),
report_error,
)
}
macro_rules! sub {
(
$data:ty => $format:expr;
$quantity:ident => $get_data:ident;
$progress:literal => $create_reporter:ident;
) => {
if let Args {
quantity: Quantity::$quantity,
progress: $progress,
files,
bytes_format,
top_down,
max_depth,
min_ratio,
no_sort,
..
} = self.args
{
return Sub {
direction: Direction::from_top_down(top_down),
get_data: $get_data,
reporter: $create_reporter::<$data>(report_error),
bytes_format: $format(bytes_format),
files,
column_width_distribution,
max_depth,
min_ratio,
no_sort,
}
.run();
}
};
}
sub! {
Bytes => |x| x;
ApparentSize => GET_APPARENT_SIZE;
false => error_only_reporter;
}
sub! {
Bytes => |x| x;
ApparentSize => GET_APPARENT_SIZE;
true => progress_and_error_reporter;
}
#[cfg(unix)]
sub! {
Bytes => |x| x;
BlockSize => GET_BLOCK_SIZE;
false => error_only_reporter;
}
#[cfg(unix)]
sub! {
Bytes => |x| x;
BlockSize => GET_BLOCK_SIZE;
true => progress_and_error_reporter;
}
#[cfg(unix)]
sub! {
Blocks => |_| ();
BlockCount => GET_BLOCK_COUNT;
false => error_only_reporter;
}
#[cfg(unix)]
sub! {
Blocks => |_| ();
BlockCount => GET_BLOCK_COUNT;
true => progress_and_error_reporter;
}
dbg!(self.args);
panic!("Invalid combination of arguments")
}
}