This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.js
121 lines (99 loc) · 3.42 KB
/
table.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
const Sep = require("inquirer").Separator;
const chalk = require("chalk").default;
const WIDTH = require("terminal-kit").terminal.width;
const TABLE_COLOR = "cyanBright";
const colorSep = m => new Sep(chalk[TABLE_COLOR](m));
class Table {
/** @param {import("./assignment")} hw */
static formatAssignment(hw) {
let dueStr = hw.dueString;
let color = chalk[UrgencyToColor[dueStr.type]];
return toRow(fillString(hw.name||"", 12),
fillString(hw.score||hw.status||"", 14),
fillString(color(dueStr.message), 40));
}
static top(...args) {
return colorSep("┏" + args.map(n => "━".repeat(n)).join("┳") + "┓");
}
static middle(...args) {
return colorSep("┣" + args.map(n => "━".repeat(n)).join("╋") + "┫");
}
static bottom(...args) {
return colorSep("┗" + args.map(n => "━".repeat(n)).join("┻") + "┛");
}
/**
* @param {Number} width
* @param {Function} color color function from chalk
*/
static splitRows(string, width, color) {
return splitStirng(string, width - 2).map(r => {
return toRow(color(fillString(r, width)));
}).join("\n ");
}
/**
* @param {import("./assignment")[]} hwList
*/
static chooseHw(hwList) {
return [Table.top(12, 14, 30),
...hwList.map(h => {
return {
name: Table.formatAssignment(h),
value: h,
};
})
.reduce((prev, curr) => {
if (prev.length) {
prev.push(Table.middle(12, 14, 30));
}
prev.push(curr);
return prev;
}, []),
Table.bottom(12, 14, 30), "Refresh", "Back", "Quit"];
}
/**
* @param {String[][]} typeQuestions
* @param {String[]} colors
*/
static chooseQuestion(typeQuestions, colors) {
colors = Array.isArray(colors) ? colors : [];
let w = WIDTH - 6;
let list = [Table.top(w)];
typeQuestions.forEach((questions, index) => {
let color = colors[index] || chalk.yellowBright;
questions.forEach(question => {
if (list.length > 1) list.push(Table.middle(w));
list.push(Table.splitRows(question, w, color));
});
});
list.push(Table.bottom(w), "Refresh", "Back", "Quit");
return list;
}
}
const UrgencyToColor = {
"normal": "greenBright",
"warn": "yellowBright",
"urgent": "yellow",
"danger": "redBright",
"dead": "red"
};
/**
* @param {String} s
* @param {Number} l
*/
const fillString = (s, l) => (s.length <= l) ? (s + " ".repeat(l - s.length))
: s.slice(0, l - 3) + "...";
/**
* @param {...String} args
*/
const toRow = (...args) => {
let line = chalk[TABLE_COLOR]("┃");
return line + args.join(line) + line;
}
/**
* @param {String} string
* @param {Number} size
* @return {String[]}
*/
const splitStirng = (string, size) => string.replace(/\n/g, "")
.match(new RegExp(`.{1,${size}}`, "g"));
module.exports = Table;