-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui.js
284 lines (246 loc) · 6.16 KB
/
ui.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
'use strict'
const chalk = require('chalk')
const Table = require('cli-table2')
const ms = require('ms')
const esc = require('ansi-escapes')
const wrap = require('prompt-skeleton')
const figures = require('figures')
const childProcess = require('child_process')
const tmp = require('tmp')
const fs = require('fs')
const trim = require('trim-newlines')
const { width, height } = require('window-size')
const {
table, newline, spawn, tmpFile, readFile, writeFile
} = require('./util')
const UI = {
moveCursor: function (n) {
this.cursor = n
if (this.message) this.message = this.messages[this.cursor]
},
abort: function () {
if (this.message) {
this.message = null
this.render()
} else {
this.out.write('\n')
this.close()
}
},
submit: function () {
if (this.message) return this.bell()
this.message = this.messages[this.cursor]
this.render()
},
first: function () {
this.moveCursor(0)
this.render()
},
last: function () {
this.moveCursor(this.messages.length - 1)
this.render()
},
up: function () {
if (this.cursor === 0) return this.bell()
this.moveCursor(this.cursor - 1)
this.render()
},
down: function () {
if (this.cursor === (this.messages.length - 1)) return this.bell()
this.moveCursor(this.cursor + 1)
this.render()
},
query: function (message) {
const self = this
const clear = () => {
self.out.write(esc.eraseScreen + esc.cursorTo(0, 0))
}
const resume = () => {
clear()
self.resume()
self.querying = false
self.render()
}
this.querying = true
this.pause()
clear()
return tmpFile()
.then((file) => {
const cmd = process.env.EDITOR || (process.platform === 'win32' ? 'notepad.exe' : 'nano')
return writeFile(file, '# ' + message)
.then(() => spawn(cmd, [file], process.stdin, process.stdout))
.then(() => readFile(file))
})
.then((text) => {
text = text.split(newline)
if (text[0].slice(0, 1) === '#') text = text.slice(1)
text = trim(text.join('\n'))
if (text.length === 0) text = null
return [text, resume]
})
.catch((err) => [null, resume])
},
compose: function (receiver) {
const sender = this.number
let message
this.query('Please enter a message. This line will be ignored.')
.then(([text, resume]) => {
if (!text) {
this.error = 'Message is empty.'
return resume()
}
message = text
if (!receiver) return this.query('Enter a phone number.')
return [receiver, resume]
})
.then(([number, resume]) => {
if (!number) {
this.error = 'Invalid phone number.'
return resume()
}
receiver = number
resume()
this.loading = true
this.render()
this.send(sender, receiver, message)
.then(() => {
this.error = null
this.render()
this.fetch()
})
.catch((err) => {
this.loading = false
this.error = err.message
this.render()
})
})
},
_: function (key) {
if (this.querying) return this.bell()
if (key === 'j') return this.down()
if (key === 'k') return this.up()
if (key === 'f') return this.fetch()
if (key === 'r') {
const message = this.messages[this.cursor]
return this.compose(message.outbound ? message.to : message.from)
}
if (key === 'c') return this.compose()
if (key === 'd') {
const message = this.message || this.messages[this.cursor]
return this.remove(message.id)
}
return this.bell()
},
renderStatus: function () {
const l = this.messages.length
return [
this.loading ? figures.radioOn : figures.radioOff
, this.error ? chalk.red(this.error) : null
, chalk.gray(['no messages', '1 message'][l] || `${l} messages`)
, chalk.gray(this.number)
].filter((s) => !!s).join(' ')
},
renderMessage: function () {
let out = ''
const message = this.message
out += chalk.gray(ms(Date.now() - message.when) + ' ago')
out += ' '
out += chalk.yellow(message.outbound ? message.to : message.from)
out += ' '
out += message.inbound ? chalk.blue('in') : chalk.red('out'),
out += '\n'
out += this.message.text
return out
},
renderMessages: function () {
const out = table([3, 15, 3, width - 3 - 15 - 3 - 3]) // columns + 3x padding of 1
let i = 0
for (let message of this.messages) {
out.push([
chalk.gray(ms(Date.now() - message.when)),
chalk.yellow(message.outbound ? message.to : message.from),
message.inbound ? chalk.blue('in') : chalk.red('out'),
i === this.cursor ? chalk.cyan(message.text) : message.text
])
i++
}
return out.toString()
},
renderBindings: function () {
const out = [
'F to fetch',
this.message ? 'ctrl+D to go back' : 'ctrl+D to leave',
'C to compose',
]
if (!this.error) out.push(
this.messages.length > 0 ? 'R to reply' : '',
this.messages.length > 0 ? 'D to delete' : ''
)
return chalk.gray(out.filter((s) => !!s).join(' | '))
},
render: function (first) {
if (first) this.out.write('\n'.repeat(height))
if (this.querying) return
let out = esc.clearScreen + esc.cursorTo(0, 0)
+ this.renderStatus() + '\n'
if (this.messages.length === 0) {
} else if (this.message) {
out += this.renderMessage()
} else {
out += this.renderMessages()
}
out += '\n' + this.renderBindings()
this.out.write(out + esc.cursorHide)
}
}
const defaults = {
messages: []
, message: null
, cursor: 0
, querying: false
, error: null
}
const ui = (client, number, opt) => {
if (Array.isArray(opt) || 'object' !== typeof opt) opt = {}
const ui = Object.assign(Object.create(UI), defaults, opt)
ui.number = number
const result = wrap(ui)
const fetch = () => {
ui.loading = true
ui.render()
client.list()
.then((messages) => {
ui.loading = false
ui.messages = messages.sort((m1, m2) => m2.when - m1.when)
ui.render()
})
.catch((err) => {
ui.loading = false
ui.error = err.message
ui.render()
})
}
fetch()
setInterval(fetch, 5 * 60 * 1000)
const remove = (id) => {
ui.loading = true
ui.render()
client.delete(id)
.then(() => {
ui.messages = ui.messages.filter((m) => m.id !== id)
})
.catch((err) => {
ui.error = err.message
ui.render()
})
.then(() => {
ui.loading = false
ui.render()
})
}
ui.fetch = fetch
ui.remove = remove
ui.send = client.send
return result
}
module.exports = ui