forked from klaudiosinani/taskbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
343 lines (270 loc) · 9.07 KB
/
Copy pathrender.js
File metadata and controls
343 lines (270 loc) · 9.07 KB
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
'use strict';
const chalk = require('chalk');
const signale = require('signale');
const config = require('./config');
signale.config({displayLabel: false});
const {await: wait, error, log, note, pending, success} = signale;
const {blue, green, grey, magenta, red, underline, yellow} = chalk;
const priorities = {2: 'yellow', 3: 'red'};
class Render {
get _configuration() {
return config.get();
}
_colorBoards(boards) {
return boards.map(x => grey(x)).join(' ');
}
_isBoardComplete(items) {
const {tasks, complete, notes} = this._getItemStats(items);
return tasks === complete && notes === 0;
}
_getAge(birthday) {
const daytime = 24 * 60 * 60 * 1000;
const age = Math.round(Math.abs((birthday - Date.now()) / daytime));
return (age === 0) ? '' : grey(`${age}d`);
}
_getCorrelation(items) {
const {tasks, complete} = this._getItemStats(items);
return grey(`[${complete}/${tasks}]`);
}
_getItemStats(items) {
let [tasks, complete, notes] = [0, 0, 0];
items.forEach(item => {
if (item._isTask) {
tasks++;
if (item.isComplete) {
return complete++;
}
}
return notes++;
});
return {tasks, complete, notes};
}
_getStar(item) {
return item.isStarred ? yellow('★') : '';
}
_buildTitle(key, items) {
const title = (key === new Date().toDateString()) ? `${underline(key)} ${grey('[Today]')}` : underline(key);
const correlation = this._getCorrelation(items);
return {title, correlation};
}
_buildPrefix(item) {
const prefix = [];
const {_id} = item;
prefix.push(' '.repeat(4 - String(_id).length));
prefix.push(grey(`${_id}.`));
return prefix.join(' ');
}
_buildMessage(item) {
const message = [];
const {isComplete, description} = item;
const priority = parseInt(item.priority, 10);
if (!isComplete && priority > 1) {
message.push(underline[priorities[priority]](description));
} else {
message.push(isComplete ? grey(description) : description);
}
if (!isComplete && priority > 1) {
message.push(priority === 2 ? yellow('(!)') : red('(!!)'));
}
return message.join(' ');
}
_displayTitle(board, items) {
const {title: message, correlation: suffix} = this._buildTitle(board, items);
const titleObj = {prefix: '\n ', message, suffix};
return log(titleObj);
}
_displayItemByBoard(item) {
const {_isTask, isComplete, inProgress} = item;
const age = this._getAge(item._timestamp);
const star = this._getStar(item);
const prefix = this._buildPrefix(item);
const message = this._buildMessage(item);
const suffix = (age.length === 0) ? star : `${age} ${star}`;
const msgObj = {prefix, message, suffix};
if (_isTask) {
return isComplete ? success(msgObj) : inProgress ? wait(msgObj) : pending(msgObj);
}
return note(msgObj);
}
_displayItemByDate(item) {
const {_isTask, isComplete, inProgress} = item;
const boards = item.boards.filter(x => x !== 'My Board');
const star = this._getStar(item);
const prefix = this._buildPrefix(item);
const message = this._buildMessage(item);
const suffix = `${this._colorBoards(boards)} ${star}`;
const msgObj = {prefix, message, suffix};
if (_isTask) {
return isComplete ? success(msgObj) : inProgress ? wait(msgObj) : pending(msgObj);
}
return note(msgObj);
}
displayByBoard(data) {
Object.keys(data).forEach(board => {
if (this._isBoardComplete(data[board]) && !this._configuration.displayCompleteTasks) {
return;
}
this._displayTitle(board, data[board]);
data[board].forEach(item => {
if (item._isTask && item.isComplete && !this._configuration.displayCompleteTasks) {
return;
}
this._displayItemByBoard(item);
});
});
}
displayByDate(data) {
Object.keys(data).forEach(date => {
if (this._isBoardComplete(data[date]) && !this._configuration.displayCompleteTasks) {
return;
}
this._displayTitle(date, data[date]);
data[date].forEach(item => {
if (item._isTask && item.isComplete && !this._configuration.displayCompleteTasks) {
return;
}
this._displayItemByDate(item);
});
});
}
displayStats({percent, complete, inProgress, pending, notes}) {
if (!this._configuration.displayProgressOverview) {
return;
}
percent = percent >= 75 ? green(`${percent}%`) : percent >= 50 ? yellow(`${percent}%`) : `${percent}%`;
const status = [
`${green(complete)} ${grey('done')}`,
`${blue(inProgress)} ${grey('in-progress')}`,
`${magenta(pending)} ${grey('pending')}`,
`${blue(notes)} ${grey(notes === 1 ? 'note' : 'notes')}`
];
if (complete !== 0 && inProgress === 0 && pending === 0 && notes === 0) {
log({prefix: '\n ', message: 'All done!', suffix: yellow('★')});
}
if (pending + inProgress + complete + notes === 0) {
log({prefix: '\n ', message: 'Type `tb --help` to get started!', suffix: yellow('★')});
}
log({prefix: '\n ', message: grey(`${percent} of all tasks complete.`)});
log({prefix: ' ', message: status.join(grey(' · ')), suffix: '\n'});
}
invalidCustomAppDir(path) {
const [prefix, suffix] = ['\n', red(path)];
const message = 'Custom app directory was not found on your system:';
error({prefix, message, suffix});
}
invalidID(id) {
const [prefix, suffix] = ['\n', grey(id)];
const message = 'Unable to find item with id:';
error({prefix, message, suffix});
}
invalidIDsNumber() {
const prefix = '\n';
const message = 'More than one ids were given as input';
error({prefix, message});
}
invalidPriority() {
const prefix = '\n';
const message = 'Priority can only be 1, 2 or 3';
error({prefix, message});
}
markComplete(ids) {
if (ids.length === 0) {
return;
}
const [prefix, suffix] = ['\n', grey(ids.join(', '))];
const message = `Checked ${ids.length > 1 ? 'tasks' : 'task'}:`;
success({prefix, message, suffix});
}
markIncomplete(ids) {
if (ids.length === 0) {
return;
}
const [prefix, suffix] = ['\n', grey(ids.join(', '))];
const message = `Unchecked ${ids.length > 1 ? 'tasks' : 'task'}:`;
success({prefix, message, suffix});
}
markStarted(ids) {
if (ids.length === 0) {
return;
}
const [prefix, suffix] = ['\n', grey(ids.join(', '))];
const message = `Started ${ids.length > 1 ? 'tasks' : 'task'}:`;
success({prefix, message, suffix});
}
markPaused(ids) {
if (ids.length === 0) {
return;
}
const [prefix, suffix] = ['\n', grey(ids.join(', '))];
const message = `Paused ${ids.length > 1 ? 'tasks' : 'task'}:`;
success({prefix, message, suffix});
}
markStarred(ids) {
if (ids.length === 0) {
return;
}
const [prefix, suffix] = ['\n', grey(ids.join(', '))];
const message = `Starred ${ids.length > 1 ? 'items' : 'item'}:`;
success({prefix, message, suffix});
}
markUnstarred(ids) {
if (ids.length === 0) {
return;
}
const [prefix, suffix] = ['\n', grey(ids.join(', '))];
const message = `Unstarred ${ids.length > 1 ? 'items' : 'item'}:`;
success({prefix, message, suffix});
}
missingBoards() {
const prefix = '\n';
const message = 'No boards were given as input';
error({prefix, message});
}
missingDesc() {
const prefix = '\n';
const message = 'No description was given as input';
error({prefix, message});
}
missingID() {
const prefix = '\n';
const message = 'No id was given as input';
error({prefix, message});
}
successCreate({_id, _isTask}) {
const [prefix, suffix] = ['\n', grey(_id)];
const message = `Created ${_isTask ? 'task:' : 'note:'}`;
success({prefix, message, suffix});
}
successEdit(id) {
const [prefix, suffix] = ['\n', grey(id)];
const message = 'Updated description of item:';
success({prefix, message, suffix});
}
successDelete(ids) {
const [prefix, suffix] = ['\n', grey(ids.join(', '))];
const message = `Deleted ${ids.length > 1 ? 'items' : 'item'}:`;
success({prefix, message, suffix});
}
successMove(id, boards) {
const [prefix, suffix] = ['\n', grey(boards.join(', '))];
const message = `Move item: ${grey(id)} to`;
success({prefix, message, suffix});
}
successPriority(id, level) {
const prefix = '\n';
const message = `Updated priority of task: ${grey(id)} to`;
const suffix = level === '3' ? red('high') : (level === '2' ? yellow('medium') : green('normal'));
success({prefix, message, suffix});
}
successRestore(ids) {
const [prefix, suffix] = ['\n', grey(ids.join(', '))];
const message = `Restored ${ids.length > 1 ? 'items' : 'item'}:`;
success({prefix, message, suffix});
}
successCopyToClipboard(ids) {
const [prefix, suffix] = ['\n', grey(ids.join(', '))];
const message = `Copied the ${ids.length > 1 ? 'descriptions of items' : 'description of item'}:`;
success({prefix, message, suffix});
}
}
module.exports = new Render();