Skip to content

Commit 2a0c926

Browse files
committed
Adapt stat graph to the real terminal width.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 029bec1 commit 2a0c926

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/cli.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ cli.run = function() {
6969
});
7070

7171
var yargs = require('yargs');
72+
h.width = yargs.terminalWidth();
7273
yargs.commandDir('commands')
7374
.completion()
7475
.help('h')
7576
.alias('h', 'help')
7677
.version(false)
7778
.epilog('Seek more help at https://skygragon.github.io/leetcode-cli/commands')
78-
.wrap(Math.min(yargs.terminalWidth(), 120))
79+
.wrap(Math.min(h.width, 120))
7980
.argv;
8081
};
8182

lib/commands/stat.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,34 @@ function showGraph(problems) {
9494
none: chalk.gray('⬚ ')
9595
};
9696

97+
// row header is 4 bytes
98+
// each question takes 2 bytes
99+
// each group has 10 questions, which takes (2*10=20) + 3 paddings
100+
var groups = Math.floor((h.width - 4) / (3 + 2 * 10));
101+
if (groups < 1) groups = 1;
102+
if (groups > 5) groups = 5;
103+
104+
var header = _.range(groups)
105+
.map(function(x) { return sprintf('%5d%18d', x * 10 + 1, x * 10 + 10); })
106+
.join('');
107+
log.info(' ' + header);
108+
97109
var graph = [];
98110
_.each(problems, function(problem) {
99111
graph[problem.id] = icons[problem.state] || icons.none;
100112
});
101113

102-
log.printf('%8d%19d%5d%18d%5d%18d%5d%18d%5d%18d', 1, 10, 11, 20, 21, 30, 31, 40, 41, 50);
103-
104-
var line = [sprintf(' %03d ', 0)];
114+
var line = [sprintf(' %03d', 0)];
105115
for (var i = 1, n = graph.length; i <= n; ++i) {
116+
// padding before group
117+
if (i % 10 === 1) line.push(' ');
118+
106119
line.push(graph[i] || ' ');
107-
if (i % 10 === 0) line.push(' ');
108-
if (i % 50 === 0 || i === n) {
120+
121+
// time to start new row
122+
if (i % (10 * groups) === 0 || i === n) {
109123
log.info(line.join(''));
110-
line = [sprintf(' %03d ', i)];
124+
line = [sprintf(' %03d', i)];
111125
}
112126
}
113127

0 commit comments

Comments
 (0)