-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
executable file
·118 lines (100 loc) · 2.6 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
#!/usr/bin/env node
const Tail = require('tail').Tail;
const chalk = require('chalk');
const keypress = require('keypress');
const getCenter = (total, inner) => Math.ceil((total - inner) / 2);
const title = name => `Are you still watching "${name}"?`;
const choices = ['Continue watching', 'Exit'];
const buttonSize = choices.sort((a, b) => b.length > a.length)[0].length + 2;
const wrappedChoices = choices.map(choice => {
const diff = getCenter(buttonSize, choice.length);
return [
...new Array(diff).fill(' ').join(''),
choice,
...new Array(diff).fill(' ').join(''),
].join('');
});
const center = str =>
new Array(getCenter(process.stdout.columns, str.length)).fill(' ').join('');
const draw = (active, filename) => {
const height = 2 + choices.length;
const paddingTop = getCenter(process.stdout.rows, height);
const paddingBottom = process.stdout.rows - height - paddingTop;
new Array(paddingTop).fill(' ').forEach(() => {
console.log('');
});
console.log(center(title(filename)) + title(filename));
console.log('');
wrappedChoices.map((choice, i) => {
i === active
? console.log(center(choice) + chalk.bgWhite.black(choice))
: console.log(center(choice) + choice);
});
new Array(paddingBottom).fill(' ').forEach(() => {
console.log('');
});
};
const prompt = filename =>
new Promise(yay => {
keypress(process.stdin);
let active = 0;
draw(active, filename);
process.stdin.on('keypress', function(ch, key) {
if (key && key.name === 'down') {
if (active >= choices.length - 1) {
active = 0;
} else {
active += 1;
}
draw(active, filename);
}
if (key && key.name === 'up') {
if (active <= 0) {
active = choices.length - 1;
} else {
active -= 1;
}
draw(active, filename);
}
if (key && key.name === 'return') {
process.stdin.setRawMode(false);
process.stdout.write('\033c');
if (active === 0) {
yay();
} else {
process.exit();
}
}
if (key && key.ctrl && key.name == 'c') {
process.stdin.setRawMode(false);
}
});
process.stdin.setRawMode(true);
process.stdin.resume();
});
const log = file => {
const tail = new Tail(file);
let count = 0;
let buffer = [];
tail.on('line', function(data) {
count++;
if (count === 4) {
buffer.push(data);
prompt(file).then(() => {
buffer.forEach(line => {
console.log(line);
});
buffer = [];
count = 0;
});
} else if (count >= 4) {
buffer.push(data);
} else {
console.log(data);
}
});
tail.on('error', function(error) {
console.log('ERROR: ', error);
});
};
log(process.argv[process.argv.length - 1]);