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
use camino::Utf8PathBuf;
use clap::{Args, Parser, Subcommand, ValueEnum};
use lib_tasker::todos::State;

/// A command-line application to manage your daily Tasks.
#[derive(Debug, Parser)]
#[command(
    name = "Tasker CLI",
    author,
    version,
    about,
    long_about = None,
    help_template = "\
{before-help}{name} {version}
{author-with-newline}{about-with-newline}
{usage-heading} {usage}

{all-args}{after-help}"
)]
pub struct Cli {
    /// Application subcommand
    #[command(subcommand)]
    pub command: Option<Command>,

    /// Path to a file in which to look for and save Tasks
    #[arg(long = "todo-file")]
    pub to_do_file: Option<Utf8PathBuf>,

    /// Path to an alternative configuration file. Takes precedence over `todo-file`
    #[arg(long)]
    pub config_file: Option<Utf8PathBuf>,
}

#[derive(Debug, Subcommand)]
#[command(help_template(
    "\
{name}
{about-with-newline}
{usage-heading} {usage}

{all-args}"
))]
pub enum Command {
    /// Add one Task
    #[command(arg_required_else_help = true, visible_alias = "a")]
    Add(AddToDo),

    /// Add multiple Tasks
    #[command(
        arg_required_else_help = true,
        name = "addm",
        visible_alias = "am"
    )]
    AddMultiple(AddMultipleToDo),

    /// Clean completed Tasks
    #[command(visible_alias = "c")]
    Clean,

    /// Delete Tasks
    #[command(arg_required_else_help = true, visible_alias = "d")]
    Delete(DeleteToDo),

    /// Edit a Task
    #[command(arg_required_else_help = true, visible_alias = "e")]
    Edit(EditToDo),

    /// List Tasks
    #[command(visible_alias = "l")]
    List(ListToDo),

    /// Print default paths for the application
    #[command(visible_alias = "p")]
    Paths,

    /// Change the state of a Task
    #[command(arg_required_else_help = true, visible_alias = "t")]
    Toggle(ToggleToDo),
}

#[derive(Args, Debug)]
#[command(help_template(
    "\
{name}
{about-with-newline}
{usage-heading} {usage}

{all-args}"
))]
pub struct AddToDo {
    /// Task to accomplish, wrap in quotes for multi-word tasks.
    pub description: String,

    /// Project the Task belongs to. Defaults to "Inbox"
    #[arg(short, long)]
    pub project: Option<String>,

    /// Tag to classify the Task. Can be called multiple times
    #[arg(short, long)]
    pub tag: Option<Vec<String>>,
}

#[derive(Args, Debug)]
#[command(help_template(
    "\
{name}
{about-with-newline}
{usage-heading} {usage}

{all-args}"
))]
pub struct AddMultipleToDo {
    /// Tasks to accomplish, wrap individual Tasks in quotes for multi-word
    /// Tasks.
    pub descriptions: Vec<String>,

    /// Project the Task's belongs to. Defaults to "Inbox"
    #[arg(short, long)]
    pub project: Option<String>,

    /// Tag to assign the Task's. Can be called multiple times
    #[arg(short, long)]
    pub tag: Option<Vec<String>>,
}

#[derive(Args, Debug)]
#[command(help_template(
    "\
{name}
{about-with-newline}
{usage-heading} {usage}

{all-args}"
))]
pub struct ToggleToDo {
    /// State to assign the Task
    #[arg(value_enum)]
    pub state: ToggleState,

    /// ID(s) of the Task(s) to toggle
    #[arg(name = "TO-DOS")]
    pub tasks: Vec<usize>,
}

#[derive(Debug, ValueEnum, Clone, Copy)]
pub enum ToggleState {
    /// This Task hasn't started
    #[value(name = "todo", alias = "t")]
    ToDo,

    /// This Task is in progress
    #[value(alias = "dg")]
    Doing,

    /// This Task is finished
    #[value(alias = "dn")]
    Done,

    /// This Task can't be accomplished due to external reasons
    #[value(name = "wait", alias = "w")]
    Waiting,
}

impl From<ToggleState> for State {
    fn from(value: ToggleState) -> Self {
        match value {
            ToggleState::ToDo => Self::ToDo,
            ToggleState::Doing => Self::Doing,
            ToggleState::Done => Self::Done,
            ToggleState::Waiting => Self::Waiting,
        }
    }
}

#[derive(Args, Debug)]
#[command(help_template(
    "\
{name}
{about-with-newline}
{usage-heading} {usage}

{all-args}"
))]
pub struct EditToDo {
    /// ID of the Task to edit
    #[arg(name = "TO-DO")]
    pub task: usize,

    /// Change Task description
    #[arg(short, long)]
    pub description: Option<String>,

    /// Change Task progress
    #[arg(short, long, value_enum)]
    pub state: Option<ToggleState>,

    /// Change Task project
    #[arg(short, long)]
    pub project: Option<String>,

    /// Replace Task tags. Can be called multiple times
    #[arg(short, long)]
    pub tags: Option<Vec<String>>,
}

#[derive(Args, Debug)]
#[command(help_template(
    "\
{name}
{about-with-newline}
{usage-heading} {usage}

{all-args}"
))]
pub struct DeleteToDo {
    /// Id's of Task(s) to delete
    #[arg(name = "TASKS")]
    pub tasks: Vec<usize>,
}

#[derive(Args, Debug)]
#[command(help_template(
    "\
{name}
{about-with-newline}
{usage-heading} {usage}

{all-args}"
))]
pub struct ListToDo {
    /// Sort Tasks by this field
    #[arg(short = 'S', long, value_enum)]
    pub sort_by: Option<SortToDo>,

    /// Only show Tasks containing this text within their descriptions
    #[arg(short, long)]
    pub description: Option<String>,

    /// Only show Tasks within this state of progress
    #[arg(short, long)]
    pub state: Option<ToggleState>,

    /// Only show Tasks containing this tag. Can be called multiple times
    #[arg(short, long)]
    pub tag: Option<Vec<String>>,

    /// Only show Tasks belonging to this project
    #[arg(short, long)]
    pub project: Option<String>,
}

#[derive(Debug, ValueEnum, Clone, Copy)]
pub enum SortToDo {
    /// Sort by description [aliases: desc, d]
    #[value(alias = "desc", alias = "d")]
    Description,

    /// Sort by project [aliases: pro, p]
    #[value(alias = "pro", alias = "p")]
    Project,

    /// Sort by state [aliases: s]
    #[value(alias = "s")]
    State,

    /// Sort by ID [aliases: i]
    #[value(alias = "i")]
    ID,
}