-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatters.rs
More file actions
382 lines (350 loc) Β· 12.6 KB
/
Copy pathformatters.rs
File metadata and controls
382 lines (350 loc) Β· 12.6 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use std::collections::HashMap;
use lazy_static::lazy_static;
use ansi_term::Colour;
use crate::parsers::LBS_PER_KG;
//use crate::models::{JDay, Set, Exercise, EBlock, User};
use crate::models::{JDay, Set, Exercise, EBlock};
use crate::workouts::read_cached_user_wants_kg_or;
use crate::table::matches_any_filter;
#[derive(Clone)]
pub struct FormatOptions {
pub user_wants_kg: bool,
pub bw_precision: usize,
pub color_enabled: bool,
pub show_unit_name: bool,
}
impl FormatOptions {
pub fn for_display(user_wants_kg: bool) -> Self {
Self {
user_wants_kg,
bw_precision: 0,
color_enabled: *COLOR_ENABLED,
show_unit_name: false,
}
}
pub fn for_cache() -> Self {
Self {
user_wants_kg: read_cached_user_wants_kg_or(true),
bw_precision: 4,
color_enabled: false,
show_unit_name: true,
}
}
pub fn no_color(user_wants_kg: bool) -> Self {
Self {
user_wants_kg,
bw_precision: 0,
color_enabled: false,
show_unit_name: false,
}
}
}
lazy_static! {
pub static ref COLOR_ENABLED: bool = {
let color_arg = std::env::var("WXRUST_COLOR").unwrap_or("auto".to_string());
match color_arg.as_str() {
"always" => true,
"never" => false,
"auto" => atty::is(atty::Stream::Stdout),
_ => atty::is(atty::Stream::Stdout),
}
};
pub static ref STDERR_COLOR_ENABLED: bool = {
let color_arg = std::env::var("WXRUST_COLOR").unwrap_or("auto".to_string());
match color_arg.as_str() {
"always" => true,
"never" => false,
"auto" => atty::is(atty::Stream::Stderr),
_ => atty::is(atty::Stream::Stderr),
}
};
}
pub fn color_date(s: &str) -> String {
color_date_internal(s, &FormatOptions::for_display(true))
}
fn color_date_internal(s: &str, options: &FormatOptions) -> String {
if options.color_enabled {
Colour::RGB(157, 78, 221).paint(s).to_string()
} else {
s.to_string()
}
}
#[allow(dead_code)]
pub fn color_bw(s: &str) -> String {
color_bw_internal(s, &FormatOptions::for_display(true))
}
fn color_bw_internal(s: &str, options: &FormatOptions) -> String {
if options.color_enabled {
Colour::RGB(58, 134, 255).paint(s).to_string()
} else {
s.to_string()
}
}
fn color_exercise_internal(s: &str, options: &FormatOptions) -> String {
if options.color_enabled {
Colour::RGB(0, 150, 255).paint(s).to_string()
} else {
s.to_string()
}
}
fn color_weight_internal(s: &str, options: &FormatOptions) -> String {
if options.color_enabled {
Colour::RGB(255, 121, 0).paint(s).to_string()
} else {
s.to_string()
}
}
fn color_reps_internal(s: &str, options: &FormatOptions) -> String {
if options.color_enabled {
Colour::RGB(0, 187, 249).paint(s).to_string()
} else {
s.to_string()
}
}
fn color_sets_internal(s: &str, options: &FormatOptions) -> String {
if options.color_enabled {
Colour::RGB(241, 91, 181).paint(s).to_string()
} else {
s.to_string()
}
}
pub fn format_weight(w: f32, w_in_lbs: bool, options: &FormatOptions) -> String {
let display_in_lbs = !options.user_wants_kg;
let num = if w_in_lbs && display_in_lbs {
w
} else if w_in_lbs && !display_in_lbs {
w / LBS_PER_KG
} else if !w_in_lbs && display_in_lbs {
w * LBS_PER_KG
} else {
w
};
//eprintln!("formatter: w_in_lbs = {}, display_in_lbs = {}, w = {}, num = {}",
// w_in_lbs, display_in_lbs, w, num);
let unit_str = if display_in_lbs { "lbs" } else { "kg" };
if options.show_unit_name {
format!("{:.0} {}", num, unit_str)
} else {
format!("{:.0}", num)
}
}
pub fn format_weight_with_bw(w: f32, w_in_lbs: bool, usebw: i32, options: &FormatOptions) -> String {
if usebw != 0 {
if w > 0.0 {
if usebw > 0 {
format!("BW+{}", format_weight(w, w_in_lbs, options))
} else {
format!("BW-{}", format_weight(w, w_in_lbs, options))
}
} else {
"BW".to_string()
}
} else {
format_weight(w, w_in_lbs, options)
}
}
#[allow(dead_code)]
pub fn format_set(set: &Set) -> String {
format_set_internal(set, &FormatOptions::for_display(true))
}
#[allow(dead_code)]
pub fn format_failed_set(w: f32, w_in_lbs: bool, usebw: i32, r: u32, s: u32, options: &FormatOptions) -> String {
let wbw = format_weight_with_bw(w, w_in_lbs, usebw, options);
let rxs = format!("{} x {} x {}", wbw, r, s);
if options.color_enabled {
Colour::RGB(255, 0, 0).paint(rxs).to_string()
} else {
rxs
}
}
#[allow(dead_code)]
fn format_set_internal(set: &Set, options: &FormatOptions) -> String {
let w = set.w.unwrap_or(0.0);
let r = set.r.unwrap_or(0);
let s = set.s.unwrap_or(1);
let rpe = set.rpe.unwrap_or(0.0);
let w_in_lbs = false; //set.lb.unwrap_or(0.0) == 1.0;
let usebw = set.usebw.unwrap_or(0);
let line = format_weight_with_bw(w, w_in_lbs, usebw, options);
let w_str = color_weight_internal(&line, options);
let mut line = w_str;
if r > 0 {
line += " x ";
line += &color_reps_internal(&r.to_string(), options);
if s > 1 {
line += " x ";
line += &color_sets_internal(&s.to_string(), options);
}
} else {
line = format_failed_set(w, w_in_lbs, usebw, r, s, options);
}
if rpe > 0.0 {
line += &format!(" @{}", rpe);
}
if let Some(c) = &set.c
&& !c.is_empty() {
line += &format!(" {}", c);
}
line
}
#[allow(dead_code)]
pub fn compress_sets(sets: &[Set]) -> Vec<String> {
compress_sets_internal(sets, &FormatOptions::for_display(true))
}
fn compress_sets_internal(sets: &[Set], options: &FormatOptions) -> Vec<String> {
let mut compressed = Vec::new();
let mut i = 0;
while i < sets.len() {
let set = &sets[i];
if set.set_type.unwrap_or(0) != 0 {
compressed.push(format_set_internal(set, options));
i += 1;
continue;
}
let w = set.w.unwrap_or(0.0);
let r = set.r.unwrap_or(0);
let _s = set.s.unwrap_or(1);
let rpe = set.rpe.unwrap_or(0.0);
let w_in_lbs = false; //set.lb.unwrap_or(0.0) == 1.0;
let usebw = set.usebw.unwrap_or(0);
// check for same weight consecutive
let mut same_weight = vec![r];
let mut j = i + 1;
while j < sets.len() {
let next = &sets[j];
if next.set_type.unwrap_or(0) != 0 || next.w != set.w || next.rpe != set.rpe || next.lb != set.lb || next.s != set.s || next.usebw != set.usebw {
break;
}
same_weight.push(next.r.unwrap_or(0));
j += 1;
}
if same_weight.len() > 1 {
let line = format_weight_with_bw(w, w_in_lbs, usebw, options);
let w_str = color_weight_internal(&line, options);
let r_str = same_weight.iter().map(|&r| color_reps_internal(&r.to_string(), options)).collect::<Vec<_>>().join(", ");
let mut line = format!("{} x {}", w_str, r_str);
if rpe > 0.0 {
line += &format!(" @{}", rpe);
}
compressed.push(line);
i = j;
} else {
// check for same rep
let mut same_rep = vec![w];
let mut j = i + 1;
while j < sets.len() {
let next = &sets[j];
if next.set_type.unwrap_or(0) != 0 || next.r != set.r || next.rpe != set.rpe || next.lb != set.lb || next.s != set.s || next.usebw != set.usebw {
break;
}
same_rep.push(next.w.unwrap_or(0.0));
j += 1;
}
if same_rep.len() > 1 {
let w_str = same_rep.iter().map(|&w| {
let line = format_weight_with_bw(w, w_in_lbs, usebw, options);
color_weight_internal(&line, options)
}).collect::<Vec<_>>().join(", ");
let r_str = color_reps_internal(&r.to_string(), options);
let mut line = format!("{} x {}", w_str, r_str);
if rpe > 0.0 {
line += &format!(" @{}", rpe);
}
compressed.push(line);
i = j;
} else {
compressed.push(format_set_internal(set, options));
i += 1;
}
}
}
compressed
}
#[allow(dead_code)]
pub fn format_single_eblock(jday: &JDay, eblock: &EBlock) -> String {
format_single_eblock_internal(jday, eblock, &FormatOptions::for_display(true))
}
fn format_single_eblock_internal(jday: &JDay, eblock: &EBlock, options: &FormatOptions) -> String {
let mut ex_map: HashMap<String, &Exercise> = HashMap::new();
for ex_wrap in &jday.exercises {
ex_map.insert(ex_wrap.exercise.id.clone(), &ex_wrap.exercise);
}
let mut lines = Vec::new();
if let Some(ex) = ex_map.get(&eblock.eid) {
lines.push("#".to_string() + &color_exercise_internal(&ex.name, options));
lines.extend(compress_sets_internal(&eblock.sets, options));
}
lines.join("\n")
}
pub fn summarize_workout(jday: &JDay, user_wants_kg: bool, filters: &[String]) -> String {
summarize_workout_internal(jday, &FormatOptions::for_display(user_wants_kg), filters)
}
fn summarize_workout_internal(jday: &JDay, options: &FormatOptions, filters: &[String]) -> String {
let mut ex_map: HashMap<String, &Exercise> = HashMap::new();
for ex_wrap in &jday.exercises {
ex_map.insert(ex_wrap.exercise.id.clone(), &ex_wrap.exercise);
}
let mut summaries = Vec::new();
for eblock in &jday.eblocks {
if let Some(ex) = ex_map.get(&eblock.eid) {
if !matches_any_filter(&ex.name, filters) {
continue;
}
// Find the heaviest set: max weight, then max reps
// Skip failed sets (reps == 0 or sets == 0)
let mut max_weight = 0.0;
let mut max_reps = 0;
for set in &eblock.sets {
let w = set.w.unwrap_or(0.0);
let r = set.r.unwrap_or(0);
let s = set.s.unwrap_or(1);
if r > 0 && s > 0 && (w > max_weight || (w == max_weight && r > max_reps)) {
max_weight = w;
max_reps = r;
}
}
if max_weight > 0.0 {
let w_in_lbs = false; //eblock.sets.iter().any(|s| s.lb.unwrap_or(0.0) == 1.0);
let w_str = color_weight_internal(&format_weight(max_weight, w_in_lbs, options), options);
let r_str = color_reps_internal(&max_reps.to_string(), options);
summaries.push(format!("#{} {}x{}", color_exercise_internal(&ex.name, options), w_str, r_str));
}
}
}
summaries.join("; ")
}
pub fn format_workout(date: &str, jday: &JDay, user_wants_kg: bool) -> String {
let options = FormatOptions::for_display(user_wants_kg);
format_workout_internal(date, jday, &options)
}
fn format_workout_internal(date: &str, jday: &JDay, options: &FormatOptions) -> String {
let mut result = jday.log.clone();
for eblock in &jday.eblocks {
let formatted = format_single_eblock_internal(jday, eblock, options);
let placeholder = format!("EBLOCK:{}", eblock.eid);
result = result.replace(&placeholder, &formatted);
}
let mut output = vec![color_date_internal(date, options)];
if let Some(bw) = jday.bw
&& bw > 0.0 {
let num = if options.user_wants_kg { bw } else { bw * LBS_PER_KG };
let unit_str = if options.user_wants_kg { "kg" } else { "lbs" };
let bwtxt = if options.show_unit_name {
format!("{:.*} {}", options.bw_precision, num, unit_str)
} else {
format!("{:.*}", options.bw_precision, num)
};
output.push(format!("@ {} bw", color_bw_internal(&bwtxt, options)));
}
output.push(result);
output.join("\n")
}
#[allow(dead_code)]
pub fn format_workout_no_color(date: &str, jday: &JDay, user_wants_kg: bool) -> String {
let options = FormatOptions::no_color(user_wants_kg);
format_workout_internal(date, jday, &options)
}
pub fn format_workout_for_cache(date: &str, jday: &JDay) -> String {
let options = FormatOptions::for_cache();
format_workout_internal(date, jday, &options)
}