forked from notwaldorf/tiny-care-terminal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcare.js
executable file
·188 lines (168 loc) · 5.72 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
#!/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 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) {
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', [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', ['-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;
for (var i = 0; i < lines.length; i++) {
// If it's a path
if (lines[i][0] === '/' || lines[i][0] === '\\') {
lines[i] = '\n' + chalk.red(lines[i]);
} else {
// 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');
}