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
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "calendar")]
#[command(author, version, about = "Calendar-rs is a small cli to handle your calendars from the terminal")]
pub struct Cli {
#[command(subcommand)]
pub command: Option<Command>,
// Global options for when no subcommand is given (default view mode)
#[arg(short, long, help = "View mode: day, week, month")]
mode: Option<String>,
#[arg(short, long, help = "Specify the calendar to view")]
calendar: Option<String>,
#[arg(short, long, help = "Show n times")]
number: Option<u32>,
}
#[derive(Subcommand)]
pub enum Command {
/// List events from all or specific calendars
List {
/// Query terms in the fzf search
query: Vec<String>,
/// Specify the calendar to list (default: all)
#[arg(short, long)]
calendar: Option<String>,
/// Start date for listing (default: today)
#[arg(short, long)]
from: Option<String>,
/// End date for listing (default: 1 month from today)
#[arg(short, long)]
to: Option<String>,
/// Limit the number of events shown
#[arg(short, long)]
limit: Option<usize>,
/// Show the uuid of the tasks for future modification
#[arg(short, long)]
id: bool,
},
/// Add a new event to a calendar
Add {
/// Name of the event
name: Vec<String>,
/// Event start time (eg. tom@21 14-jul@12:30 2024/08/06@08:00)
#[arg(short, long)]
at: String,
/// Event end time (default: 1 hour after start)
#[arg(short, long)]
to: Option<String>,
/// The calendar to add the event to (default: personal)
#[arg(short, long)]
calendar: Option<String>,
/// Event location
#[arg(short, long)]
loc: Option<String>,
/// Event description
#[arg(short, long)]
desc: Option<String>,
/// Repeat frequency (daily, weekly, monthly, yearly)
#[arg(short, long)]
repeat: Option<String>,
/// Repeat every N days/weeks/months/years
#[arg(short, long)]
every: Option<u32>,
/// Repeat until this date
#[arg(short, long)]
until: Option<String>,
},
/// Display calendar in various formats (daily, weekly, monthly)
View {
/// Specify the date for which the calendar will be run
date: Option<String>,
/// View mode: day, week, month
#[arg(short, long, default_value = "month")]
mode: String,
/// Specify the calendar to view
#[arg(short, long)]
calendar: Option<String>,
/// Show n times
#[arg(short, long)]
number: Option<u32>,
},
/// Edit an existing event
Edit {
/// Event ID to edit
event_id: String,
/// The calendar to edit the event from (default: personal)
#[arg(short, long)]
calendar: Option<String>,
/// Name of the event
#[arg(short, long)]
name: Option<String>,
/// New event start time
#[arg(short, long)]
at: Option<String>,
/// New event end time
#[arg(short, long)]
to: Option<String>,
/// New event location
#[arg(short, long)]
loc: Option<String>,
/// New event description
#[arg(short, long)]
desc: Option<String>,
},
/// Delete an event
Delete {
/// Event ID to delete
event_id: String,
/// Specify the calendar
#[arg(short, long)]
calendar: Option<String>,
/// Delete without confirmation
#[arg(short, long)]
force: bool,
},
/// Show details of a specific event
Show {
/// Event ID to show
event_id: String,
/// Specify the calendar to show from
#[arg(short, long)]
calendar: Option<String>,
},
/// Synchronize calendars using vdirsyncer
Sync {
/// Specify the calendar to sync
#[arg(long)]
calendar: Option<String>,
},
}
#[derive(Clone, Debug)]
pub enum ViewMode {
Day,
Week,
Month,
}
impl std::str::FromStr for ViewMode {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"day" | "d" => Ok(ViewMode::Day),
"week" | "w" => Ok(ViewMode::Week),
"month" | "m" => Ok(ViewMode::Month),
_ => Err(format!("Invalid view mode: {}. Use day, week, or month", s)),
}
}
}
impl Default for ViewMode {
fn default() -> Self {
ViewMode::Month
}
}
/// Parse command line arguments
pub fn parse() -> Cli {
Cli::parse()
}
/// Get the command to run, defaulting to View if none specified
pub fn get_command(cli: Cli) -> Command {
cli.command.unwrap_or(Command::View {
date: None,
mode: cli.mode.unwrap_or("month".to_string()),
calendar: cli.calendar,
number: cli.number,
})
}