-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.ts
228 lines (202 loc) · 6.03 KB
/
ls.ts
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
import {Command, flags} from '@oclif/command'
import * as fs from "fs";
import { colorTask, filterByField, maxWidth, readTasks, urgency } from "../utils/task";
import { readConfig } from "../utils/config";
import { calculateWidth } from "../utils/table";
import { table, TableUserConfig } from "table";
import { Task } from "todo.json";
import chalk = require('chalk');
import wrapAnsi = require("wrap-ansi");
export default class List extends Command {
static description = 'List tasks'
static examples = [
`$ td ls`,
`$ td ls -p test`,
]
static flags = {
help: flags.help({char: 'h'}),
all: flags.boolean({
char: "a",
description: "list all tasks including done ones"
}),
done: flags.boolean({
char: "D",
description: "list only done tasks"
}),
priority: flags.string({
char: "P",
description: "priority (A-Z)"
}),
project: flags.string({
char: "p",
description: "filter tasks by specific projects",
multiple: true
}),
context: flags.string({
char: "c",
description: "filter tasks by specific contexts",
multiple: true
}),
"without-projects": flags.boolean({
description: "list tasks without projects",
default: false
}),
"without-contexts": flags.boolean({
description: "list tasks without contexts",
default: false
}),
"and-projects": flags.boolean({
description: "filter projects using AND operator instead of OR",
default: false
}),
"and-contexts": flags.boolean({
description: "filter contexts using AND operator instead of OR",
default: false
})
}
async run() {
const { flags } = this.parse(List);
const { todoPath, donePath } = readConfig();
const header = [
["ID", "P", "Text", "Projects", "Contexts", "Due"]
];
const stdoutColumns = process.stdout.columns ?? 80;
if (stdoutColumns < 50) {
this.error("Terminal width must be greater than 50.");
}
const tableOptions = {
drawHorizontalLine: index => index === 1,
border: {
bodyLeft: "",
bodyRight: "",
bodyJoin: "",
bottomLeft: "",
bottomRight: "",
bottomJoin: "",
bottomBody: "-",
joinLeft: "",
joinRight: "",
joinJoin: ""
}
} as TableUserConfig;
const wrapOptions = {
hard: true
};
let todoOutput = "";
let doneOutput = "";
if (!flags.done) {
let todoTasks = [] as Task[];
if (fs.existsSync(todoPath)) {
todoTasks = readTasks(todoPath);
}
const projectFilter = filterByField(
"projects",
flags.project,
flags["and-projects"],
flags["without-projects"]
);
const contextFilter = filterByField(
"contexts",
flags.context,
flags["and-contexts"],
flags["without-contexts"]
);
const todoData = todoTasks.map((task, index) => ({
index,
task
}))
.filter(({ task }) => projectFilter(task) && contextFilter(task))
.sort((a, b) => {
return urgency(b.task) - urgency(a.task);
});
const textWidth = maxWidth(todoData, "text");
const projWidth = maxWidth(todoData, "projects");
const ctxWidth = maxWidth(todoData, "contexts");
const dueWidth = maxWidth(todoData, "due");
const result = calculateWidth(stdoutColumns, {
idWidth: Math.max(2, todoData.length.toString().length),
priWidth: 1,
textWidth,
projWidth,
ctxWidth,
dueWidth
}, 2 * 6);
const output = todoData.map(({ index, task }) => {
const color = colorTask(task);
return [
(index + 1).toString(),
task.priority ?? "",
task.text,
task.projects?.join(" ") ?? "",
task.contexts?.join(" ") ?? "",
task.due ?? ""
].map((field, i) => {
let value = field;
if (result) {
if (i === 2)
value = wrapAnsi(field, result.textWidth, wrapOptions);
if (i === 3)
value = wrapAnsi(field, result.projWidth, wrapOptions);
if (i === 4)
value = wrapAnsi(field, result.ctxWidth, wrapOptions);
}
return color ? chalk[color].bold(value) : value;
});
});
todoOutput = table(header.concat(output), tableOptions);
}
if (flags.done || flags.all) {
let doneTasks = [] as Task[];
if (fs.existsSync(donePath)) {
doneTasks = readTasks(donePath);
}
const doneData = doneTasks.map((task, index) => ({
index,
task
}));
const textWidth = maxWidth(doneData, "text");
const projWidth = maxWidth(doneData, "projects");
const ctxWidth = maxWidth(doneData, "contexts");
const dueWidth = maxWidth(doneData, "due");
const result = calculateWidth(stdoutColumns, {
idWidth: Math.max(2, doneData.length.toString().length),
priWidth: 1,
textWidth,
projWidth,
ctxWidth,
dueWidth
}, 2 * 6);
const output = doneData.map(({ index, task }) => {
return [
(index + 1).toString(),
task.priority ?? "",
task.text,
task.projects?.join(" ") ?? "",
task.contexts?.join(" ") ?? "",
task.due ?? ""
].map((field, i) => {
let value = field;
if (result) {
if (i === 2)
value = wrapAnsi(field, result.textWidth, wrapOptions);
if (i === 3)
value = wrapAnsi(field, result.projWidth, wrapOptions);
if (i === 4)
value = wrapAnsi(field, result.ctxWidth, wrapOptions);
}
return value;
});
});
doneOutput = table(header.concat(output), tableOptions);
}
if (flags.all) {
this.log(`\nTodo:\n\n${todoOutput}\nDone:\n\n${doneOutput}`)
}
else if (flags.done) {
this.log(`\n${doneOutput}`);
}
else {
this.log(`\n${todoOutput}`);
}
}
}