-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhnDisplay.js
More file actions
130 lines (104 loc) · 3 KB
/
hnDisplay.js
File metadata and controls
130 lines (104 loc) · 3 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
'use strict';
var xcolor = require('xcolor');
var readline = require('readline');
var exec = require('child_process').exec;
var terminalWidth = process.stdout.getWindowSize()[0];
var logo = '[Y]';
var title = ' Hacker News';
var menu = ' [F]ront Page [N]ewest [A]sk [J]obs [#] Open News in Browser [Q]uit';
var rowWidth = function(item){
return terminalWidth
- (item.id.length)
- (item.title.length)
- (item.points.length + 2 );
};
var rowSpace = function(size){
var s = '';
for (var i=0; i<size; i++)
s += ' ';
return s;
};
var displayHeader = function(title){
var spaceLen = terminalWidth - title.length - menu.length - logo.length;
xcolor.log('{{bg #ff6600}}{{#FFFFFF}}{{bold}}' + logo + '{{#000000}}' + title + '{{/bold}}' + menu + rowSpace(spaceLen));
};
var displayRow = function(item){
xcolor.log('{{bg #f6f6ef}}{{#000000}}' + item.id + item.title + rowSpace(rowWidth(item)) + '(' + item.points + ')' );
};
var openUrl = function(url){
var cmd = 'xdg-open';
if (!!process.platform.match(/^win/))
cmd = 'start';
else if(!!process.platform.match(/^dar/))
cmd = 'open';
exec(cmd + ' ' + url, function (error, stdout, stderr){
//console.log(stdout)
if (error !== null) {
console.log('exec error: ' + error);
process.exit();
}
});
};
var getPageUri = function(cmd){
switch(cmd.toUpperCase()){
case 'N':
return 'newest';
case 'A':
return 'ask';
case 'J':
return 'jobs';
case 'Y':
return 'news'; //this should return current uri.
case 'Q':
process.exit();
break;
default:
return 'news';
}
};
exports.display = function(items){
if (items.length === 0) process.exit();
displayHeader(title);
items.forEach(function(item){
displayRow(item);
});
};
exports.clearScreen = function(callback){
var cmd = 'clear';
if (process.platform === 'win32')
cmd = 'cls';
exec(cmd, function (error, stdout, stderr) {
console.log(stdout);
callback();
if (error) {
callback('exec error: ' + error);
process.exit(1);
}
});
};
exports.getInput = function(items, callback){
var uri;
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('HN> ', function(cmd) {
if (isNaN(cmd)){
uri = getPageUri(cmd);
rl.close();
}else{
var timeout = 0;
if (cmd > 0 && cmd <= items.length){
console.log(items[cmd - 1]);
openUrl(items[cmd - 1].href);
}else{
timeout = 1000;
console.log('Invalid Command');
}
}
setTimeout(function(){
callback(uri);
rl.close();
}, timeout);
});
};