-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
executable file
·70 lines (54 loc) · 1.74 KB
/
client.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
#!/usr/bin/env node
var fs = require('fs')
, path = require('path')
, notifier = require('node-notifier')
, say = require('say')
, logStream = (function() {
var dir = path.join(process.env.HOME, '.tomatotomato')
require('mkdirp').sync(dir)
return fs.createWriteStream(path.join(dir, 'LOG'), { flags: 'a' })
})()
, charm = require('charm')()
, connection = require('./connect')()
, format = require('./format')
, singleLineOutput = function (color, string) {
charm
.foreground(color)
.erase('line')
.write(string)
.left(string.length)
}
, normalizeHostname = function (hostString) {
return hostString.slice(0, hostString.indexOf('.')).toLowerCase()
}
, myHostname = normalizeHostname(require('os').hostname())
charm.pipe(process.stdout)
charm.cursor(false)
connection.on('data', function (obj) {
var isRemote = myHostname !== normalizeHostname(obj.host)
if (obj.type === 'work') {
singleLineOutput('red', format(obj.countdown))
if (obj.countdown <= 0) {
notifier.notify({ message: 'Let\'s relax for a while!' })
say.speak('Alex', 'Let\'s relax for a while!')
}
} else {
singleLineOutput('green', format(obj.countdown))
if (obj.countdown <= 0) {
notifier.notify({ message: 'Come on! Let\'s work!' })
say.speak('Agnes', 'Come on! Let\'s work!')
}
}
logStream.write(JSON.stringify({
port: obj.port
, host: obj.host
, type: obj.type
, countdown: obj.countdown
, timestamp: (new Date()).toJSON()
, remote: isRemote
}) + '\n')
})
connection.on('close', function () {
singleLineOutput('yellow', 'Catch up ketchup!')
})
singleLineOutput('yellow', 'I have not yet connected with my master')