Skip to content

Commit 105adf3

Browse files
committed
Fix bug with ordering in log subcommand.
- Fix padding with output - Update package
1 parent f72a81e commit 105adf3

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description = "A time tracking command-line utility"
44
repository = "https://github.com/chdsbd/doug"
55
documentation = "https://github.com/chdsbd/doug/blob/master/README.md"
66
license = "MIT"
7-
version = "1.2.2"
7+
version = "1.2.3"
88
authors = ["Christopher Dignam <chris@dignam.xyz>"]
99

1010
[dependencies]

src/main.rs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,6 @@ fn restart(periods: &[Period], save: fn(&[Period])) {
243243

244244
fn log(periods: &[Period]) {
245245
let mut days: HashMap<Date<chrono::Local>, Vec<Period>> = HashMap::new();
246-
let mut project_periods = Vec::new();
247-
let mut max_diff_len = 0;
248246

249247
// organize periods by day
250248
for period in periods {
@@ -253,8 +251,13 @@ fn log(periods: &[Period]) {
253251
period.clone(),
254252
);
255253
}
254+
255+
// order days
256+
let mut days: Vec<(Date<chrono::Local>, Vec<Period>)> = days.into_iter().collect();
257+
days.sort_by_key(|&(a, ref _b)| a);
258+
256259
// count the total time tracker per day
257-
for (date, day) in &days {
260+
for &(ref date, ref day) in &days {
258261
let d = day.into_iter().fold(Duration::zero(), |acc, x| {
259262
acc +
260263
(x.end_time.unwrap_or_else(Utc::now).signed_duration_since(
@@ -270,6 +273,7 @@ fn log(periods: &[Period]) {
270273
duration = format_duration(d).bold()
271274
);
272275
// find time tracker per period
276+
let mut project_periods = Vec::new();
273277
for period in day.iter() {
274278
// push periods onto vector so we can could there lengths and properly align them
275279
match period.end_time {
@@ -281,35 +285,24 @@ fn log(periods: &[Period]) {
281285
diff,
282286
period.project.clone(),
283287
));
284-
if format_duration(diff).len() > max_diff_len {
285-
max_diff_len = format_duration(diff).len()
286-
}
288+
println!(" {start} to {end} {diff:>width$} {project}",
289+
start = format_time(period.start_time),
290+
end = format_time(end_time),
291+
diff = format_duration(diff),
292+
project = period.project.clone().blue(),
293+
width = 11)
287294
}
288295
None => {
289296
let diff = Utc::now().signed_duration_since(period.start_time);
290-
project_periods.push((
291-
period.start_time,
292-
Utc::now(),
293-
diff,
294-
period.project.clone(),
295-
));
296-
if format_duration(diff).len() > max_diff_len {
297-
max_diff_len = format_duration(diff).len()
298-
}
297+
println!(" {start} to {end} {diff:>width$} {project}",
298+
start = format_time(period.start_time),
299+
end = format_time(Utc::now()),
300+
diff = format_duration(diff),
301+
project = period.project.clone().blue(),
302+
width = 11)
299303
}
300304
}
301305
}
302-
project_periods.sort();
303-
for &(start, end, diff, ref project) in &project_periods {
304-
println!(
305-
" {start} to {end} {diff:>width$} {project}",
306-
start = format_time(start),
307-
end = format_time(end),
308-
diff = format_duration(diff),
309-
project = project.blue(),
310-
width = max_diff_len
311-
);
312-
}
313306
}
314307
}
315308

@@ -438,20 +431,20 @@ fn format_duration(duration: Duration) -> String {
438431
format!("{}s", seconds)
439432
} else if hours == 0 {
440433
format!(
441-
"{minutes}m {seconds}s",
434+
"{minutes}m {seconds:>2}s",
442435
minutes = minutes,
443436
seconds = seconds
444437
)
445438
} else if days == 0 {
446439
format!(
447-
"{hours}h {minutes}m {seconds}s",
440+
"{hours}h {minutes:>2}m {seconds:>2}s",
448441
hours = hours,
449442
minutes = minutes,
450443
seconds = seconds
451444
)
452445
} else {
453446
format!(
454-
"{days}d {hours}h {minutes}m {seconds}s",
447+
"{days}d {hours:>2}h {minutes:>2}m {seconds:>2}s",
455448
days = days,
456449
hours = hours,
457450
minutes = minutes,

0 commit comments

Comments
 (0)