-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodol.cpp
504 lines (428 loc) · 13.7 KB
/
todol.cpp
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <unistd.h>
#include <fstream>
#include <sys/ioctl.h>
using namespace std;
#define BOX_OPEN "[ ]"
#define BOX_TICKED "[X]"
#define SAVE_FILE "todol_lists.txt"
#define HEADER "ToDoL v1.0"
struct todoitem {
string name;
bool ticked;
};
struct todolist {
string name;
vector<struct todoitem> list;
};
struct winsize size;
// the global list of todolists
vector<struct todolist> lists;
// setANSI: used mostly as a helper function for printMessage, sets the ANSI code
void setANSI(string code) {
cout << "\e" << code;
}
void clearLine();
// printMessage: an easier way for me to print colored text
void printMessage(string msg, string textColor, string bgColor) {
clearLine();
setANSI(textColor);
setANSI(bgColor);
cout << msg;
setANSI("[0m");
cout << endl;
}
void clearLine() {
cout << "\e[K";
}
void clearScreen(bool showHeader) {
cout << "\e[0;0H";
for (int i = 0; i < size.ws_row-1; i++) {
cout << "\e[K" << endl;
}
cout << "\e[0;0H";
if (showHeader)
printMessage(HEADER, "[30m", "[45m");
}
// tokenize: returns the input, but broken into a vector of strings
vector<string> tokenize(string input) {
vector<string> tokens;
stringstream stream(input);
string token;
while (stream >> token) {
tokens.push_back(token);
}
return tokens;
}
// untokenizeFromCommand: turns a vector back into a string, used for some commands
string untokenizeFromCommand(vector<string> args) {
string str = "";
for (int i = 1; i < args.size()-1; i++) {
str += args[i] + " ";
}
str += args[args.size()-1];
return str;
}
string untokenizeFromCommand(vector<string> args, int start) {
string str = "";
for (int i = start; i < args.size()-1; i++) {
str += args[i] + " ";
}
str += args[args.size()-1];
return str;
}
// printList: prints the specified todo list and it's contents
void printList(string listName) {
int i = -1;
for (int n = 0; n < lists.size(); n++) {
if (lists[n].name.compare(listName) == 0) {
i = n;
break;
}
}
if (i == -1) {
printMessage("list \'" + listName + "\' does not exist.", "[31m", "[49m");
return;
}
printMessage(lists[i].name, "[30m", "[46m");// << endl;
setANSI("[36m");
for(int j = 0; j < lists[i].list.size(); j++) {
if (lists[i].list[j].ticked)
cout << BOX_TICKED << " ";
else
cout << BOX_OPEN << " ";
cout << lists[i].list[j].name << endl;
}
setANSI("[0m");
}
// printLists: prints all todo lists and their contents
void printLists() {
int curLine = 0;
int maxLine = size.ws_row-1;
for (int i = curLine; i < lists.size(); i++) {
//lists[i].list.size();
printList(lists[i].name);
cout << endl;
}
}
// makeList: creates a todo list with the name specified
void makeList(vector<string> args) {
struct todolist l;// = new struct todolist;//;new struct todolist l;
string n = args[1];
// Check if the list exists already, let the user know if it does, and don't create a new list
for (int i = 0; i < lists.size(); i++) {
if (lists[i].name.compare(n) == 0) {
printMessage("list \'" + n + "\' exists already.", "[31m", "[49m");
return;
}
}
l.name = n;
lists.push_back(l);
// cout << "created list \'" << n << "\'" << endl;
printMessage("created list \'" + n + "\'", "[32m", "[49m");
}
// removeList: removes the todo list with the name specified
void removeList(vector<string> args) {
string n = args[1];
// remove all lists if listname is "*"
if (n.compare("*") == 0) {
for (int i = 0; i < lists.size(); i++) {
lists[i].list.clear();
}
lists.clear();
printMessage("removed all lists", "[32m", "[49m");
return;
}
for (int i = 0; i < lists.size(); i++) {
if (lists[i].name.compare(n) == 0) {
lists[i].list.clear();
lists.erase(lists.begin() + i);
printMessage("removed list \'" + n + "\'", "[32m", "[49m");
return;
}
}
printMessage("list \'" + n + "\' does not exist.", "[31m", "[49m");
}
// renameList: replaces the specified list's name with the new name
void renameList(vector<string> args) {
string n = args[1];
string n2 = args[2];
for (int i = 0; i < lists.size(); i++) {
if (lists[i].name.compare(n) == 0) {
lists[i].name = n2;
printMessage("renamed list \'" + n + "\' to \'" + n2 + "\'", "[32m", "[49m");
return;
}
}
printMessage("list \'" + n + "\' does not exist.", "[31m", "[49m");
}
// addToList: adds items to todo lists
void addToList(vector<string> args) {
string listName = args[1];
string itemName = untokenizeFromCommand(args, 3);
struct todoitem item;
item.name = itemName;
item.ticked = false;
for (int i = 0; i < lists.size(); i++) {
if (lists[i].name.compare(listName) == 0) {
lists[i].list.push_back(item);
printMessage("added \'" + itemName + "\' to " + listName, "[32m", "[49m");
return;
}
}
printMessage("list \'" + listName + "\' does not exist.", "[31m", "[49m");
}
// removeFromList: removes items from todo lists
void removeFromList(vector<string> args) {
string listName = args[1];
string itemName = untokenizeFromCommand(args, 3);
// find the list
for (int i = 0; i < lists.size(); i++) {
if (lists[i].name.compare(listName) == 0) {
if (itemName.compare("*") == 0) { // if '*' is itemName, remove EVERYTHING from list
lists[i].list.clear();
printMessage("removed all items from \'" + listName + "\'", "[32m", "[49m");
return;
}
//find the item
for(int j = 0; j < lists[i].list.size(); j++) {
if (lists[i].list[j].name.compare(itemName) == 0) {
lists[i].list.erase(lists[i].list.begin() + j);
printMessage("removed \'" + itemName + "\' from " + listName, "[32m", "[49m");
return;
}
}
printMessage("item \'" + itemName + "\' does not exist in list \'" + listName + "\'", "[31m", "[49m");
return;
}
}
printMessage("list \'" + listName + "\' does not exist.", "[31m", "[49m");
}
// removeFromList: removes items from todo lists
void setMark(vector<string> args, bool mark) {
string listName = args[1];
string itemName = untokenizeFromCommand(args, 3);
// find the list
for (int i = 0; i < lists.size(); i++) {
if (lists[i].name.compare(listName) == 0) {
//find the item
for(int j = 0; j < lists[i].list.size(); j++) {
if (itemName.compare("*") == 0) { // if '*' is itemName, remove EVERYTHING from list
lists[i].list[j].ticked = mark;
if (mark)
printMessage("marked all items from \'" + listName + "\'", "[32m", "[49m");
else
printMessage("unmarked all items from \'" + listName + "\'", "[32m", "[49m");
}
else if (lists[i].list[j].name.compare(itemName) == 0) {
lists[i].list[j].ticked = mark;
if (mark)
printMessage("marked \'" + itemName + "\'", "[32m", "[49m");
else
printMessage("unmarked \'" + itemName + "\'", "[32m", "[49m");
return;
}
}
printMessage("item \'" + itemName + "\' does not exist in list \'" + listName + "\'", "[31m", "[49m");
return;
}
}
printMessage("list \'" + listName + "\' does not exist.", "[31m", "[49m");
}
// saveLists: saves the lists you've created to a file
void saveLists() {
ofstream saveFile(SAVE_FILE);
for (int i = 0; i < lists.size(); i++) {
saveFile << "=" << lists[i].name << endl;
for (int j = 0; j < lists[i].list.size(); j++) {
if (lists[i].list[j].ticked == true)
saveFile << 1;
else
saveFile << 0;
saveFile << lists[i].list[j].name << endl;
}
}
printMessage("Saved!", "[32m", "[49m");
saveFile.close();
}
// loadLists: loads the lists you've saved to a file
void loadLists() {
ifstream saveFile(SAVE_FILE);
string line;
int whichList = -1;
if (saveFile.is_open())
{
vector<string> fakeCmd;
fakeCmd = {"rm", "*"};
removeList(fakeCmd);
fakeCmd.clear();
while (getline (saveFile,line) )
{
// if it's a list title
if (line[0] == '=') {
fakeCmd = {"add", line.substr(1,line.length()-1)};
makeList(fakeCmd);
whichList++;
}
else if (whichList >= 0) {
// if item is unticked
if (line[0] == '0') {
struct todoitem newitem;
newitem.name = line.substr(1);
newitem.ticked = false;
lists[whichList].list.push_back(newitem);
}
// if item is ticked
else if (line[0] == '1') {
struct todoitem newitem;
newitem.name = line.substr(1);
newitem.ticked = true;
lists[whichList].list.push_back(newitem);
}
}
else {
printMessage("Corrupted Save File!", "[31m", "[49m");
saveFile.close();
break;
}
}
saveFile.close();
}
else {
printMessage("No Save File to load from!", "[31m", "[49m");
}
}
// main: takes input and processes commands
int main() {
string input;
vector<string> command;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
cout << "\e[2J" << "\e[0;0H";
loadLists();
clearScreen(true);
while(1) {
clearLine();
cout << ">>";
cout << "\e[s" << "\e[0;0H";
printMessage(HEADER, "[30m", "[45m");
cout << "\e[u";
getline(cin, input);
command = tokenize(input);
if (command.size() == 0) {
continue;
}
// HERE ARE THE COMMANDS
if (command[0] == "add" || command[0] == "a") { // makes a list (CHANGE)
if (command.size() > 1) {
makeList(command);
}
else {
printMessage("add [list name]", "[31m", "[49m");
}
}
else if (command[0] == "remove" || command[0] == "rm") { // removes a list (CHANGE)
if (command.size() > 1) {
removeList(command);
}
else {
printMessage("remove [list name]", "[31m", "[49m");
}
}
else if (command[0] == "rename" || command[0] == "rn") {
if (command.size() != 3) {
printMessage("rename [list name] [new name]", "[31m", "[49m");
}
else {
renameList(command);
}
}
else if (command[0] == "list" || command[0] == "ls") { // actions on a list
if (command.size() < 4) {
printMessage("list [list name] add [item name]", "[31m", "[49m");
printMessage("list [list name] remove [item name]", "[31m", "[49m");
printMessage("list [list name] mark [item name]", "[31m", "[49m");
printMessage("list [list name] unmark [item name]", "[31m", "[49m");
continue;
}
if (command[2] == "add" || command[2] == "a") { // adds item to list
addToList(command);
}
else if (command[2] == "remove" || command[2] == "rm") { // removes item from list
removeFromList(command);
}
else if (command[2] == "mark" || command[2] == "m") { // removes item from list
setMark(command, true);
}
else if (command[2] == "unmark" || command[2] == "um") { // removes item from list
setMark(command, false);
}
else {
printMessage("list [list name] add [item name]", "[31m", "[49m");
printMessage("list [list name] remove [item name]", "[31m", "[49m");
printMessage("list [list name] mark [item name]", "[31m", "[49m");
printMessage("list [list name] unmark [item name]", "[31m", "[49m");
}
}
else if (command[0] == "todo" || command[0] == "td" || command[0] == "t") { // prints all of your todo lists
if (lists.size() == 0) {
printMessage("No lists to print", "[31m", "[49m");
continue;
}
clearScreen(true);
if (command.size() == 2) {
printList(command[1]);
}
else {
printLists();
}
}
else if (command[0] == "help") {
clearScreen(true);
printMessage("add [list name]"
"\nremove [list name]"
"\nrename [list name] [new name]"
"\nlist [list name] add [item name]"
"\nlist [list name] remove [item name]"
"\nlist [list name] mark [item name]"
"\nlist [list name] unmark [item name]"
"\ntodo"
"\ntodo [list name]"
"\nhelp"
"\nclear"
"\nexit [-f]",
"[35m", "[49m");
}
else if (command[0] == "clear" || command[0] == "cls") { // clears the screen (TODO: REMOVE)
clearScreen(true);
}
else if (command[0] == "exit") {
clearScreen(true);
if (command.size() > 1) {
if (command[1] != "-f") {
saveLists();
}
}
else {
saveLists();
}
printMessage("Exiting...", "[35m", "[49m");
usleep(250000);
break;
}
else {
printMessage("Unknown command \'" + command[0] + "\'", "[31m", "[49m");
}
}
//TODO: ASSIGN ID NUBMERS TO EACH LIST FOR EASIER COMMAND EDITING
//TODO: SCROLLING TODOLISTS IF TOO LONG
//TODO: SPLIT INTO MULTIPLE FILES FOR CRYING OUT LOUD
//MAYBETODO:
//CURSES: display todolists on side instead
//Keyboard shortcuts & control
//'Edit' command to graphically edit the list
clearScreen(false);
return 0;
}