-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.cpp
616 lines (540 loc) · 14.4 KB
/
cli.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* Commandline interface for tasker.
*
* Copyright (C) 2017 Aleksi Salmela
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <cctype>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
#include <exception>
#include <unistd.h>
#ifndef _NO_READLINE_
# include <readline/readline.h>
# include <readline/history.h>
#endif
#include "backend.h"
#include "cli.h"
int main(int argc, char **argv)
{
Tasker::Cli::Main cli;
if(!cli.init(argc, argv)) {
return 1;
}
return (int)!cli.mainLoop();
}
namespace Tasker {
namespace Cli {
class CliException : public std::exception {
public:
CliException(std::string message) {
mMessage = message.c_str();
}
CliException(const char *message) {
mMessage = message;
}
const char *what() const throw() {
return mMessage;
}
private:
const char *mMessage;
};
static std::string openEditor(std::string text) {
const char *c_editor = getenv("EDITOR");
std::string editor;
if(!c_editor || !*c_editor) {
editor = "sensible-editor";
} else {
editor = c_editor;
}
char filename[] = "/tmp/tasker.XXXXXX";
if(mkstemp(filename) == -1) {
std::cerr << "Unable to create tmp file.\n";
return text;
}
std::ofstream stream(filename);
stream << text;
stream.close();
system((editor + " " + filename).c_str());
std::ifstream istream(filename);
std::string content((std::istreambuf_iterator<char>(istream)),
(std::istreambuf_iterator<char>()));
istream.close();
unlink(filename);
return content;
}
// From https://stackoverflow.com/questions/ TODO find it
static std::string trim(const std::string &str)
{
auto wsFront = std::find_if_not(str.begin(), str.end(),
(int(&)(int))std::isspace);
auto wsBack = std::find_if_not(str.rbegin(), str.rend(),
(int(&)(int))std::isspace);
return (wsBack.base() <= wsFront ?
std::string() :
std::string(wsFront, wsBack.base()));
}
Main::Main()
:mListView()
{
#ifdef _READLINE_H_
rl_initialize();
#endif
mColors = false;
if(getenv("LS_COLORS")) {
mColors = true;
NORMAL = "\e[m";
BOLD = "\e[1m";
UNDERLINE = "\e[4m";
INVERT = "\e[7m";
OVERLINE = "\e[9m";
RED = "\e[31m";
GREEN = "\e[32m";
BLUE = "\e[34m";
CYAN = "\e[36m";
} else {
NORMAL = BOLD = UNDERLINE = OVERLINE = "";
RED = GREEN = BLUE = CYAN = "";
}
newView(&mListView);
}
Main::~Main()
{
quit();
delete mProject;
}
bool Main::init(int argc, char **argv)
{
char *cwd_tmp = get_current_dir_name();
std::string cwd = cwd_tmp;
free(cwd_tmp);
std::string dir = Backend::Config::getTaskerData(cwd, NULL);
bool knownToExist = !dir.empty();
if(!knownToExist) {
dir = cwd;
}
if(argc >= 2) {
dir = argv[1];
knownToExist = false;
}
mProject = Backend::Project::open(dir);
if(mProject) {
return true;
}
if(knownToExist) {
std::cerr << "The config file contains non-existing project " << dir << "\n";
return false;
}
std::cerr << "Tasker data not found.\n";
while(true) {
std::string prompt = "Create a new tasker repository? [y/n]";
std::string line = Main::getLine(prompt);
if(line == "y") {
break;
} else if(line == "n") {
return false;
}
}
dir = Main::getLine("Where do you want the issues?");
// Ugly way to turn relative path to absolute path
if(dir[0] != '/') {
dir = cwd + "/" + dir;
}
mProject = Backend::Project::create(dir);
Backend::Config::setTaskerData(cwd, dir);
return true;
}
std::string Main::getLine(std::string cmd)
{
std::string line;
#ifdef _READLINE_H_
HISTORY_STATE *state = history_get_history_state();
HISTORY_STATE tmp_state = {0};
history_set_history_state(&tmp_state);
char *buf = ::readline((cmd + " ").c_str());
line = std::string(buf);
free(buf);
//clear_history();
history_set_history_state(state);
#else
std::cout << cmd << " ";
std::getline(std::cin, line);
#endif
return line;
}
void Main::readline(std::string cmd, std::string &command, std::vector<std::string> &args)
{
std::string line;
#ifdef _READLINE_H_
char *buf = ::readline((cmd + " ").c_str());
line = std::string(buf);
free(buf);
if(!line.empty()) {
add_history(line.c_str());
}
#else
std::cout << cmd << " ";
std::getline(std::cin, line);
#endif
std::istringstream iss(line);
iss >> command;
args.clear();
std::string arg;
while(iss >> arg) {
args.push_back(arg);
}
}
std::string Main::getText(TextClass klass, std::string text)
{
std::ostringstream stream;
switch(klass) {
case TASK_ID:
stream << INVERT << BOLD << " " << text << " " << NORMAL;
break;
case TASK_NAME:
stream << UNDERLINE << BOLD << text << NORMAL;
break;
case TASK_STATE:
case TASK_STATE_CLOSED:
stream << ((klass == TASK_STATE) ? BLUE : GREEN);
stream << BOLD << "[" << text << "]\n" << NORMAL;
break;
case TASK_LIST_HEADER:
stream << BOLD << text << NORMAL;
break;
case SUB_TASK_HEADER:
case EVENT_HEADER:
stream << RED << text << NORMAL;
break;
default:
stream << text;
}
return stream.str();
}
void Main::newView(View *view)
{
mViewStack.push_back(view);
}
void Main::deleteView(View *view)
{
if (mViewStack.back() != view) {
throw CliException("You can only delete the top item");
}
mViewStack.pop_back();
if(view != &mListView) {
delete view;
}
}
bool Main::mainLoop()
{
while(!mViewStack.empty()) {
View *activeView = getActiveView();
activeView->render(this);
}
return true;
}
View *Main::getActiveView()
{
return mViewStack.back();
}
Backend::Project *Main::getProject()
{
return mProject;
}
void Main::quit()
{
for(View *view : mViewStack) {
if(&mListView != view) {
delete view;
}
}
mViewStack.clear();
}
/// TaskListView
TaskListView::TaskListView()
:mFilter(Backend::TaskFilter::isOpen(true))
{
mShowView = true;
}
TaskListView::~TaskListView()
{
delete mFilter;
}
void TaskListView::setFilter(Backend::TaskFilter *filter)
{
delete mFilter;
mFilter = filter;
}
void TaskListView::view(CliInterface *parent)
{
auto *mList = parent->getProject()->getTaskList();
std::cout << parent->getText(TASK_LIST_HEADER, "Task list:\n");
if (mList->getSize() == 0) {
std::cout << " [Empty]\n";
}
auto sort = [](const Backend::Task *first, const Backend::Task *second) {
Backend::Date firstDate = first->getCreationDate();
Backend::Date secondDate = second->getCreationDate();
if(!first->getEvents().empty())
firstDate = first->getEvents().back()->getCreationDate();
if(!second->getEvents().empty())
secondDate = second->getEvents().back()->getCreationDate();
return firstDate > secondDate;
};
auto taskList = mList->getFiltered(mFilter);
std::sort(taskList.begin(), taskList.end(), sort);
for (Backend::Task *t : taskList) {
std::string id = std::string(" #") + std::to_string(t->getId());
std::cout << std::setw(4) << id << " " << t->getName() << "\n";
}
}
void TaskListView::render(CliInterface *parent)
{
auto *mList = parent->getProject()->getTaskList();
std::string command;
std::vector<std::string> args;
if(mShowView) {
view(parent);
mShowView = false;
}
Main::readline("TaskList>", command, args);
if (command == "o" || command == "open") {
int taskIndex;
std::istringstream iss(args[0]);
iss >> taskIndex;
Backend::Task *task = mList->getTask(taskIndex);
if (!task) {
std::cerr << "Task index is out-of-bounds.\n";
return;
}
parent->newView(new TaskView(task));
mShowView = true;
} else if (command == "n" || command == "new") {
parent->newView(new CreateTaskView());
} else if (command == "ls" || command == "list") {
mShowView = true;
} else if (command == "s" || command == "search") {
std::ostringstream oss;
std::string separator = "";
for(std::string str : args) {
oss << separator << str;
separator = " ";
}
try {
auto *search = Backend::Search::create(oss.str());
setFilter(search);
mShowView = true;
} catch(Backend::SearchException e) {
std::cout << "Syntax error in search string\n";
}
} else if (command == "t" || command == "type") {
if (args.size() != 1) {
std::cout << "USAGE: type NAME\n";
return;
}
parent->newView(new ModifyTaskTypeView(args[0]));
} else if (command == "w" || command == "write") {
parent->getProject()->write();
} else if (command == "q" || command == "quit") {
parent->deleteView(this);
} else {
std::cerr << "Unknown command '" << command << "'.\n";
}
}
/// ModifyTaskTypeView
ModifyTaskTypeView::ModifyTaskTypeView(std::string name)
{
mName = name;
}
void ModifyTaskTypeView::render(CliInterface *parent)
{
auto *type = parent->getProject()->getType(mName);
if(!type) {
std::cout << "Creating new type '" << mName << "'\n";
type = new Backend::TaskType(parent->getProject(), mName);
auto state = Backend::TaskState::create(type, "not-started");
type->setStartState(state);
auto endState = Backend::TaskState::create(type, "done");
type->setEndStates({endState});
type->setTransition(state, endState);
} else {
std::cout << "Opening existing type '" << mName << "'\n";
}
std::cout << "\nTODO: The task types can't be modified from cli"
<< ", but you can manually edit the json file.\n\n";
parent->deleteView(this);
}
/// CreateListView
CreateTaskView::CreateTaskView(Backend::Task *parent)
{
mParent = parent;
}
void CreateTaskView::render(CliInterface *parent)
{
std::string name, type;
name = Main::getLine("Name:");
type = Main::getLine("Type:");
std::cout << "\n";
auto *prj = parent->getProject();
auto *task_type = prj->getType(type);
if(!task_type) {
// TODO implement recomendations
std::cerr << "Type is unknown.\n";
parent->deleteView(this);
return;
}
auto *task = new Backend::Task(prj, name);
task->setType(task_type);
if(mParent) {
mParent->addSubTask(task);
} else {
parent->getProject()->getTaskList()->addTask(task);
}
parent->deleteView(this);
parent->newView(new TaskView(task));
}
//TaskView.cpp
TaskView::TaskView(Backend::Task *task)
{
mTask = task;
mShowView = true;
}
void TaskView::view(CliInterface *parent)
{
std::ostringstream stream;
if(mTask->getParentTask()) {
stream << "#" << mTask->getParentTask()->getId() << "." << mTask->getId()
<< " ";
} else {
stream << "#" << mTask->getId();
}
if(parent->hasColor()) {
auto klass = mTask->isClosed() ? TASK_STATE_CLOSED : TASK_STATE;
std::cout << parent->getText(TASK_ID, stream.str());
std::cout << " " << parent->getText(TASK_NAME, mTask->getName());
std::cout << " " << parent->getText(klass, mTask->getState()->getName());
} else {
stream << " " << mTask->getName() << " [" << mTask->getState()->getName() + "]";
std::string name = stream.str();
std::cout << name << "\n";
for(unsigned int i = 0; i < name.size(); i++) {
std::cout << "=";
}
std::cout << "\n";
}
std::cout << "Created at "
<< mTask->getCreationDate().getFormattedTime("%d.%m.%Y %H:%M:%S") << "\n";
if(mTask->getAssigned()) {
std::cout << "assigned to: " << mTask->getAssigned()->getName() << "\n";
}
std::cout << "\n" << mTask->getDescription();
auto subTasks = mTask->getSubTasks();
if(!subTasks.empty()) {
std::cout << parent->getText(SUB_TASK_HEADER, "Sub-tasks:\n");
unsigned int index = 1;
for(auto *task : subTasks) {
std::cout << " " << index++ << ". ";
std::cout << task->getName() << "\n";
}
std::cout << "\n";
}
for(auto *event : mTask->getEvents()) {
auto *comment = dynamic_cast<Backend::CommentEvent*>(event);
auto *stateChange = dynamic_cast<Backend::StateChangeEvent*>(event);
std::ostringstream header;
header << event->getCreationDate().getFormattedTime("%d.%m.%Y")
<< " by " << event->getUser()->getName() << "\n";
std::cout << parent->getText(EVENT_HEADER, header.str());
if(comment) {
std::cout << comment->getContent();
} else if(stateChange) {
std::cout << "State changed from " << stateChange->from()->getName()
<< " to " << stateChange->to()->getName() << "\n";
} else {
std::cout << "Unknown event\n";
}
std::cout << "\n";
}
}
void TaskView::render(CliInterface *parent)
{
if(mShowView) {
view(parent);
mShowView = false;
}
std::string command;
std::vector<std::string> args;
Main::readline("Task>", command, args);
if(command == "e" || command == "exit") {
parent->deleteView(this);
} else if(command == "q" || command == "quit") {
parent->quit();
} else if(command == "n" || command == "new") {
parent->newView(new CreateTaskView(mTask));
} else if (command == "ls" || command == "list") {
mShowView = true;
} else if(command == "o" || command == "open") {
int taskIndex;
std::istringstream iss(args[0]);
iss >> taskIndex;
taskIndex--;
if (taskIndex < 0 || (unsigned)taskIndex >= mTask->getSubTasks().size()) {
std::cerr << "Task index is out-of-bounds.\n";
return;
}
Backend::Task *task = mTask->getSubTasks()[taskIndex];
parent->newView(new TaskView(task));
mShowView = true;
} else if(command == "a" || command == "assign") {
mTask->setAssigned(parent->getProject()->getDefaultUser());
} else if(command == "c" || command == "comment") {
auto *comment = new Backend::CommentEvent(mTask, trim(openEditor("")));
mTask->addEvent(comment);
} else if(command == "s" || command == "state") {
//Change the state
if(args.size() != 1) {
std::cout << "Usage: state NEW_STATE\n";
return;
}
auto possibles = mTask->getType()->possibleChanges(mTask->getState());
for(auto *state : possibles) {
if(state->getName() == args[0]) {
mTask->setState(state);
return;
}
}
std::cerr << "No possible state " << args[0] << "\n";
std::cout << "Allowed next states are:\n";
for(auto *state : possibles) {
std::cout << " " << state->getName() << "\n";
}
} else if(command == "r" || command == "rename") {
mTask->setName(trim(openEditor(mTask->getName())));
//Rename the task
} else if(command == "d" || command == "description") {
//Change description
std::string text = openEditor(mTask->getDescription());
mTask->setDescription(trim(text));
} else {
std::cerr << "Unknown command '" << command << "'.\n";
}
}
};
};