forked from notwaldorf/tiny-care-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
care.js
executable file
Β·240 lines (215 loc) Β· 7.03 KB
/
care.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
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
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env node
var config = require(__dirname + '/config.js');
var twitterbot = require(__dirname + '/twitterbot.js');
var spawn = require('child_process').spawn;
var blessed = require('blessed');
var contrib = require('blessed-contrib');
var chalk = require('chalk');
var parrotSay = require('parrotsay-api');
var bunnySay = require('sign-bunny');
var weather = require('weather-js');
var screen = blessed.screen(
{fullUnicode: true, // emoji or bust
smartCSR: true,
autoPadding: true,
title: 'β¨π tiny care terminal πβ¨'
});
// Quit on Escape, q, or Control-C.
screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
});
// Refresh on r, or Control-R.
screen.key(['r', 'C-r'], function(ch, key) {
tick();
});
var grid = new contrib.grid({rows: 12, cols: 12, screen: screen});
// grid.set(row, col, rowSpan, colSpan, obj, opts)
var weatherBox = grid.set(0, 8, 2, 4, blessed.box, makeScrollBox(' π€ '));
var todayBox = grid.set(0, 0, 6, 6, blessed.box, makeScrollBox(' π Today '));
var weekBox = grid.set(6, 0, 6, 6, blessed.box, makeScrollBox(' π Week '));
var commits = grid.set(0, 6, 6, 2, contrib.bar, makeGraphBox('Commits'));
var parrotBox = grid.set(6, 6, 6, 6, blessed.box, makeScrollBox(''));
var tweetBoxes = {}
tweetBoxes[config.twitter[1]] = grid.set(2, 8, 2, 4, blessed.box, makeBox(' π '));
tweetBoxes[config.twitter[2]] = grid.set(4, 8, 2, 4, blessed.box, makeBox(' π¬ '));
tick();
setInterval(tick, 1000 * 60 * config.updateInterval);
function tick() {
doTheWeather();
doTheTweets();
doTheCodes();
}
function doTheWeather() {
weather.find({search: config.weather, degreeType: config.celsius ? 'C' : 'F'}, function(err, result) {
if (result && result[0] && result[0].current) {
var json = result[0];
// TODO: add emoji for this thing.
var skytext = json.current.skytext.toLowerCase();
var currentDay = json.current.day;
var degreetype = json.location.degreetype;
var forecastString = '';
for (var i = 0; i < json.forecast.length; i++) {
var forecast = json.forecast[i];
if (forecast.day === currentDay) {
var skytextforecast = forecast.skytextday.toLowerCase();
forecastString = `Today, it will be ${skytextforecast} with a forecast high of ${forecast.high}Β°${degreetype} and a low of ${forecast.low}Β°${degreetype}.`;
}
}
weatherBox.content = `In ${json.location.name} it's ${json.current.temperature}Β°${degreetype} and ${skytext} right now. ${forecastString}`;
} else {
weatherBox.content = 'Having trouble fetching the weather for you :(';
}
});
}
function doTheTweets() {
for (var which in config.twitter) {
// Gigantor hack: first twitter account gets spoken by the party parrot.
if (which == 0) {
twitterbot.getTweet(config.twitter[which]).then(function(tweet) {
if (config.say === 'bunny') {
parrotBox.content = bunnySay(tweet.text);
screen.render();
} else if (config.say === 'llama') {
parrotBox.content = llamaSay(tweet.text);
screen.render();
} else if (config.say === 'cat') {
parrotBox.content = catSay(tweet.text);
screen.render();
} else {
parrotSay(tweet.text).then(function(text) {
parrotBox.content = text;
screen.render();
});
}
},function(error) {
// Just in case we don't have tweets.
parrotSay('Hi! You\'re doing great!!!').then(function(text) {
parrotBox.content = text;
screen.render();
});
});
} else {
twitterbot.getTweet(config.twitter[which]).then(function(tweet) {
tweetBoxes[tweet.bot.toLowerCase()].content = tweet.text;
screen.render();
},function(error) {
tweetBoxes[config.twitter[1]].content =
tweetBoxes[config.twitter[2]].content =
'Can\'t read Twitter without some API keys π°. Maybe try the scraping version instead?';
});
}
}
}
function doTheCodes() {
var todayCommits = 0;
var weekCommits = 0;
var today = spawn('sh ' + __dirname + '/standup-helper.sh', ['-m ' + config.depth, config.repos], {shell:true});
todayBox.content = '';
today.stdout.on('data', data => {
todayCommits = getCommits(`${data}`, todayBox);
updateCommitsGraph(todayCommits, weekCommits);
screen.render();
});
var week = spawn('sh ' + __dirname + '/standup-helper.sh', ['-m ' + config.depth + ' -d 7', config.repos], {shell:true});
weekBox.content = '';
week.stdout.on('data', data => {
weekCommits = getCommits(`${data}`, weekBox);
updateCommitsGraph(todayCommits, weekCommits);
screen.render();
});
}
function makeBox(label) {
return {
label: label,
tags: true,
// draggable: true,
border: {
type: 'line' // or bg
},
style: {
fg: 'white',
border: { fg: 'cyan' },
hover: { border: { fg: 'green' }, }
}
};
}
function makeScrollBox(label) {
var options = makeBox(label);
options.scrollable = true;
options.scrollbar = { ch:' ' };
options.style.scrollbar = { bg: 'green', fg: 'white' }
options.keys = true;
options.vi = true;
options.alwaysScroll = true;
options.mouse = true;
return options;
}
function makeGraphBox(label) {
var options = makeBox(label);
options.barWidth= 5;
options.xOffset= 4;
options.maxHeight= 10;
return options;
}
var commitRegex = /(.......) (- .*)/g;
function getCommits(data, box) {
var content = colorizeLog(data);
box.content += content;
return (box.content.match(commitRegex) || []).length;
}
function updateCommitsGraph(today, week) {
commits.setData({titles: ['today', 'week'], data: [today, week]})
}
function colorizeLog(text) {
var lines = text.split('\n');
var regex = /(.......) (- .*) (\(.*\)) (<.*>)/i;
var nothingRegex = /Seems like .* did nothing/i;
for (var i = 0; i < lines.length; i++) {
// If it's a path
if (lines[i][0] === '/') {
lines[i] = formatRepoName(lines[i], '/')
} else if(lines[i][0] === '\\') {
lines[i] = formatRepoName(lines[i], '\\')
} else {
// It may be a mean "seems like .. did nothing!" message. Skip it
var nothing = lines[i].match(nothingRegex);
if (nothing) {
lines[i] = '';
continue;
}
// It's a commit.
var matches = lines[i].match(regex);
if (matches) {
lines[i] = chalk.red(matches[1]) + ' ' + matches[2] + ' ' +
chalk.green(matches[3])
}
}
}
return lines.join('\n');
}
function formatRepoName(line, divider) {
var path = line.split(divider);
return '\n' + chalk.yellow(path[path.length - 1]);
}
function llamaSay(text) {
return `
${text}
β©β©
γοΌο½₯Οο½₯οΌ
γγβ β
γγβ ββββ
γ γ½γγγδΈΏ
γγ γβ₯οΏ£β₯`;
}
function catSay(text) {
return `
${text}
βͺ γ¬γ³γγ¬! βͺ
γ γγ β§οΌΏβ§ γγγ
γ γ ( γ»βγ» )γγγ
γγ οΌΌγγγοΌγγ
γγ γiβγ½ ο½
γγ γ (οΌΏ) γ
γγγγγ βͺ`
;
}