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
use crate::{
commands::{Cli, Config, Error, Result, current_job, from_range_months},
entities::{DateTime, format_job_id},
jobber::JobberWork,
views::{CalendarView, create_calendar_table},
};
use clap::Parser;
use color_print::cformat;
/// Show work within a calendar.
#[derive(Parser, Debug)]
pub(crate) struct Calendar {
/// Time to display
///
/// Available formats:
///
/// | Example | Description |
/// | ------------|-----------------|
/// | `all` | all work time |
/// | `now` | current month |
/// | `5/2026` | month/year |
/// | `5` | month |
/// | `2026` | whole year |
#[clap(default_value = "all", verbatim_doc_comment)]
start: String,
/// Continue display until
///
/// Available formats:
///
/// | Example | Description |
/// | ------------|-----------------|
/// | `now` | current month |
/// | `5/2026` | month/year |
/// | `5` | month |
/// | `2026` | whole year |
#[clap(verbatim_doc_comment)]
end: Option<String>,
/// Show only work of this job
///
/// [default: current job]
#[clap(short, long, conflicts_with("all_jobs"))]
job_id: Option<usize>,
/// Show work of all jobs
///
/// [default: current job only]
#[clap(short, long, conflicts_with("job_id"))]
all_jobs: bool,
/// Show only deleted work and deleted jobs
#[clap(short, long)]
deleted: bool,
}
impl Calendar {
pub(crate) fn run(&self, cli: &Cli) -> Result<()> {
let config = Config::load()?;
let database = cli.open_database()?;
let (job_id, outro) = if self.all_jobs {
(None, cformat!("All jobs"))
} else {
let job_id = if let Some(job_id) = self.job_id {
job_id
} else {
current_job(&config, &database)?
};
(
Some(job_id),
cformat!(
"Job: {}",
format_job_id(job_id, &database, &config, cli.ansi())?
),
)
};
let (start, end) = if self.start == "all" {
let (start, end) = database.work_range(&job_id, self.deleted.into());
if start >= end {
return Err(Error::NoJobsFoundInRange(if let Some(end) = &self.end {
format!("{}..{}", self.start.clone(), end.clone())
} else {
self.start.clone()
}));
}
(start, end)
} else {
let (start, end) = from_range_months(&self.start, &self.end, DateTime::now())?;
if start >= end {
return Err(Error::InvalidRangeEx(if let Some(end) = &self.end {
format!("{}..{}", self.start.clone(), end.clone())
} else {
self.start.clone()
}));
}
(start, end)
};
let mut date = start;
while date < end {
let mut calendar = Vec::new();
let mut hours_per_month = 0.0;
let mut current_day = date.start_of_month();
let month_end = current_day.next_month();
let mut current_week = [None; 7];
let mut week_start_day;
while current_day < month_end {
let day_of_week = current_day.day_of_week() as usize;
current_week[day_of_week] =
Some(database.duration_by_day(job_id, current_day).num_minutes() as f32 / 60.0);
week_start_day = current_day.day() as i32 - day_of_week as i32;
if day_of_week == 6 || current_day.next_day() >= month_end {
let week_sum: f32 = current_week.iter().flatten().sum();
hours_per_month += week_sum;
calendar.push(CalendarView::new(
week_start_day,
current_week,
week_sum,
cli.ansi(),
));
current_week = [None; 7];
}
current_day = current_day.next_day();
}
calendar.push(CalendarView::new_sum(
date.start_of_month(),
hours_per_month,
cli.ansi(),
));
if hours_per_month > 0.0 {
database.duration_by_day(job_id, date);
let table = create_calendar_table(calendar, &config);
println!();
println!(
" {:02}/{}",
date.month(),
date.year()
);
println!("{table}");
} else {
println!(" .");
}
date = date.next_month();
}
eprintln!("{outro}");
Ok(())
}
}