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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
use clap::{
Arg,
App,
ArgMatches,
crate_version,
crate_authors,
crate_description
};
use chrono::Duration;
use colored::*;
use budget::*;
fn main() {
let matches = get_cli_matches();
let no_color = matches.occurrences_of("plain") > 0;
let force_color = matches.occurrences_of("force-color") > 0;
let input = matches.value_of("INPUT").unwrap();
let account = match budget::parse_account(input) {
Ok(data) => data,
Err(error) => {
match error {
ParseError::IOError(kind) => {
println!("IO error while parsing: {:?}", kind);
},
ParseError::DeserializerError(_) => {
println!("Can't parse the file, invalid syntax");
},
}
::std::process::exit(1);
}
};
let maybe_calculated = budget::calculate(&account);
if no_color && !force_color {
colored::control::set_override(false);
} else if force_color {
colored::control::set_override(true);
}
output(account, maybe_calculated);
}
fn get_cli_matches() -> ArgMatches<'static> {
App::new("finbudg")
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.arg(Arg::with_name("plain")
.short("p")
.long("plain")
.help("Don't colorize the output. Can also be set \
with the NO_COLOR environment variable.")
.takes_value(false))
.arg(Arg::with_name("force-color")
.long("force-color")
.help("Forces colorized output even when piping. Takes \
precedence over --plain flag and NO_COLOR environment \
variable")
.takes_value(false))
.arg(Arg::with_name("INPUT")
.help("Expenses file in toml format to calculate from.")
.required(true)
.index(1))
.get_matches()
}
fn output(account: Account, maybe_calculated: Option<Calculated>) {
println!(
"{}",
format!(
"Your expenses for the period of {} - {}",
account.start_date.format("%Y-%m-%d"),
account.end_date.format("%Y-%m-%d"),
).cyan(),
);
let calculated = match maybe_calculated {
Some(data) => data,
None => {
println!();
println!("{}", "You have no expenses...".italic());
::std::process::exit(0);
}
};
let days_until_end = account.end_date - calculated.last_day;
println!(
"{}",
format!(
"Last day on entry: {}",
calculated.last_day.format("%Y-%m-%d"),
).cyan(),
);
println!(
"{}",
format!(
"Days until period end: {}",
days_until_end.num_days(),
).cyan(),
);
if days_until_end < Duration::zero() {
println!();
println!(
"{}",
"Your last day on entry is set after the last date of the period!"
.yellow(),
);
println!();
}
println!(
"{}",
format!(
"Budget: {:.2}",
account.budget,
).cyan(),
);
println!();
for (category, expenses) in calculated.categories_day_average.iter() {
println!(
"Average per day in {}: {:.2}",
category,
expenses,
);
}
println!(
"Average per day in essential expenses: {:.2}",
calculated.essential_day_average,
);
println!(
"Average per day: {:.2}",
calculated.all_day_average,
);
println!();
for (category, expenses) in calculated.categories_subtotal.iter() {
println!(
"Total in {}: {:.2}",
category,
expenses,
);
}
println!(
"Total in essential expenses: {:.2}",
calculated.essential_subtotal,
);
println!(
"Total: {:.2}",
calculated.total,
);
println!();
let balance_output = format!("{:.2}", calculated.balance);
let balance_output = if calculated.balance > 0.0 {
if account.budget / calculated.balance < 10.0 {
balance_output.green()
} else {
balance_output.yellow()
}
} else {
balance_output.red()
};
println!("Left on balance: {}", balance_output);
println!();
println!("Days until balance runs out:");
let days_left_output = format!(
"{:.2}",
calculated.days_left,
);
let days_left_essential_output = format!(
"{:.2}",
calculated.days_left_essential,
);
let mut all_are_healthy = true;
let mut essential_are_healthy = true;
let days_left_output =
if days_until_end.num_days() as f64 <= calculated.days_left {
days_left_output.green()
} else {
all_are_healthy = false;
days_left_output.red()
};
let days_left_essential_output =
if days_until_end.num_days() as f64 <= calculated.days_left_essential {
days_left_essential_output.green()
} else {
essential_are_healthy = false;
days_left_essential_output.red()
};
println!(
"...taking into account all expenses: {}",
days_left_output,
);
println!(
"...taking into account only essential expenses: {}",
days_left_essential_output,
);
println!();
if all_are_healthy {
println!(
"{}",
"Your expenses are healthy, they should last you from your last \
day on entry through your last day of the period.".green(),
);
} else {
println!(
"{}",
"You are spending more than you can afford with your current \
budget. Try minimizing your expenses".red(),
);
if essential_are_healthy {
println!(
"{}",
"On the other hand, if you only spend money on essentials, \
you should be able keep within your budget.".yellow(),
);
}
}
}
|