-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (131 loc) · 3.28 KB
/
index.js
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
var vorpal = require('vorpal')();
const chalk = vorpal.chalk;
var jsonfile = require('jsonfile');
var _ = require('lodash');
var pad = require('pad-right');
var file = 'kufanya.json'
var data = { todos: [] };
jsonfile.readFile(file, function(err, obj) {
if(typeof(obj) !== 'undefined') {
data = obj;
}
})
function persist() {
jsonfile.writeFile(file, data, function (err) {
});
}
function addTodo(newTodo) {
data.todos.push({
text: newTodo,
date: new Date().getTime(),
done: false
});
persist();
}
function interactiveAdd(v, cb) {
v.prompt({
type: 'input',
name: 'newTodo',
message: 'New todo: ',
},
function(result){
if (result.newTodo) {
addTodo(result.newTodo);
interactiveAdd(v, cb);
}
else {
cb();
}
});
}
vorpal
.command('add', 'Bulk-add new todos')
.action(function(args, callback) {
this.log("Add new tasks. Press ⏎ with empty line to exit");
interactiveAdd(this, callback);
});
vorpal
.command('done', 'Mark as done\n')
.action(function (args, cb) {
var undone = _.filter(data.todos, function(o) { return !o.done });
var v = this;
if(undone.length > 0) {
this.prompt({
type: 'checkbox',
name: 'entry',
message: 'Choose entry to mark as done:',
choices: _.map(undone, function(it) {
return it.text;
})
},
function(entriesToMarkDone){
_.each(entriesToMarkDone.entry, function(labelToMarkDone) {
var ind = _.findIndex(data.todos, function(todoEntry) {
return todoEntry.text === labelToMarkDone;
});
data.todos[ind].done = true;
})
persist();
v.log(`Marked ${entriesToMarkDone.entry.length} entries as done.`);
cb();
})
}
else {
v.log('Nothing to mark as done.');
cb();
}
});
vorpal
.command('list', 'List todos')
.option('-a, --all')
.action(function(args, callback) {
if(data.todos.length == 0) {
this.log("No todos here\n");
}
else {
var midnight = new Date();
midnight.setHours(0,0,0,0);
var undone = _.filter(data.todos, function(o) { return !o.done });
var today = _.filter(data.todos, function(o) {
return o.date > midnight;
});
if(args.options.all) {
this.log("All-time\n");
reportTodos(undone, "Todo", false, this);
reportTodos(_.difference(data.todos, undone), "Done", true, this);
}
else {
this.log("Today\n");
var todayUndone = _.intersection(today, undone);
reportTodos(todayUndone, "Todo", false, this);
reportTodos(_.difference(today, todayUndone), "Done", true, this);
this.log(chalk.grey('Only showing today\'s entries, use -a to see all'));
}
this.log("");
}
callback();
});
function reportTodos(sublist, label, areDone, v) {
if(sublist.length === 0) {
return;
}
var chalkingFunction;
if(areDone) {
chalkingFunction = chalk.grey;
}
else {
chalkingFunction = chalk.cyan;
}
v.log(` ${chalkingFunction(label)}`);
var id = 0;
_.each(sublist, function(entry) {
id ++;
v.log(
chalkingFunction(` [${areDone ? 'x' : ' '}] ${entry.text}`)
);
})
v.log(chalkingFunction(''));
}
vorpal
.delimiter('>>')
.show();