-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (152 loc) · 4.03 KB
/
index.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
'use strict'
const md5 = require('md5-hex')
const stripAnsi = require('strip-ansi')
const chalk = require('chalk')
const termSize = require('window-size').get
const ms = require('ms')
const wrap = require('prompt-skeleton')
const cli = require('cli-styles')
const stringWidth = require('string-width')
const runes = require('runes')
const colorHashes = Object.create(null)
const colorHash = (user) => {
if (user in colorHashes) return colorHashes[user]
const hash = '#' + md5(user).slice(0, 6)
colorHashes[user] = hash
return hash
}
const _cleanStr = str => stripAnsi(str).replace(/(\n|\r|\r\n)/g, ' ↵ ')
const cleanedStrs = Object.create(null)
const cleanStr = (str) => {
if (str in cleanedStrs) return cleanedStrs[str]
const cleanedStr = _cleanStr(str)
cleanedStrs[str] = cleanedStr
return cleanedStr
}
const renderTime = t => Math.abs(t) < 1000 ? 'now' : ms(t)
const UI = {
abort: function () {
clearInterval(this.renderInterval)
this.close()
},
submit: function () {
if (!this.input) return
this.send(this.input)
this.input = ''
this.render()
clearInterval(this.renderInterval)
},
_: function (key) {
this.input += _cleanStr(key)
this.render()
},
delete: function () {
this.input = this.input.slice(0, -1)
this.render()
},
render: function (first) {
const now = Date.now()
const {width: termWidth, height: termHeight} = termSize()
const msgs = this.messages
const lines = []
const rows = []
const widths = []
const maxWidth = [1, 4, 6, 0]
// compute table column widths
for (let i = msgs.length - 1; i >= 0; i--) {
const msg = msgs[i]
const from = cleanStr(msg.from)
const content = cleanStr(msg.content)
const row = [
msg.sending ? '…' : ' ',
renderTime(now - msg.when),
from,
content
]
const width = [ // todo: cache width
1,
row[1].length, // we don't expect non-ASCII here
stringWidth(from),
stringWidth(content)
]
if (width[1] > maxWidth[1]) maxWidth[1] = width[1]
if (width[2] > maxWidth[2]) maxWidth[2] = width[2]
if (width[3] > maxWidth[3]) maxWidth[3] = width[3]
rows.push(row)
widths.push(width)
}
// render table
for (
let i = rows.length - 1, linesUsed = 0;
i >= 0 && linesUsed <= termHeight;
i--
) {
const r = rows[i]
const w = widths[i]
let pad = maxWidth[0] + maxWidth[1] + 1 + maxWidth[2]
lines.push((
r[0] + // sending
' '.repeat(maxWidth[0] - w[0]) + // padding
chalk.gray(r[1]) + // when
' '.repeat(maxWidth[1] - w[1]) + // padding
chalk.hex(colorHash(r[2]))(r[2]) + // from
' '.repeat(1 + maxWidth[2] - w[2]) + // padding
runes.substr(r[3], 0, termWidth - pad) // fill up first line
))
linesUsed++
// fill more lines if necessary
let start = termWidth - pad
while (start < w[3]) {
const chunkWidth = termWidth - pad
lines.push(' '.repeat(pad) + runes.substr(r[3], start, chunkWidth))
linesUsed++
start += chunkWidth
}
}
const w = stringWidth(this.input)
if (this.messages.length === 0) lines.push(chalk.gray('no messages'))
if (this.error) {
const err = cleanStr(this.error.message || (this.error + ''))
let start = 0
while (start < w) { // word wrap
const line = runes.substr(err, start, termWidth)
lines.push(chalk.red(line))
start += termWidth
}
}
if (this.input) {
// word wrap input
let start = 0
while (start < w) {
const line = runes.substr(this.input, start, termWidth)
lines.push(chalk.cyan(line))
start += termWidth
}
} else lines.push(chalk.gray('type a message…'))
this.out.write(lines.slice(-termHeight).join('\n'))
}
}
const defaults = {
messages: [],
input: '',
open: false,
error: null,
send: () => {},
renderInterval: null
}
const createUI = (send) => {
const ui = Object.assign(Object.create(UI), defaults)
ui.send = send
ui.renderInterval = setInterval(() => {
ui.render()
}, 10 * 1000)
const render = (open, messages, err) => {
ui.open = open
ui.messages = messages
ui.error = err
ui.render()
}
wrap(ui)
return render
}
module.exports = createUI