forked from cabal-club/cabal-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneat-screen.js
163 lines (138 loc) · 4.76 KB
/
neat-screen.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
var neatLog = require('neat-log')
var output = require('./output')
var strftime = require('strftime')
var Commander = require('./commands.js')
var chalk = require('chalk')
// TODO:
// * introduce messages types
// type: chat/text
// type: chat/info
//
// * rewrite usage of state.messages,
// look at substack's approach in the og chatmesh
const HEADER_ROWS = 6
function NeatScreen (cabal) {
if (!(this instanceof NeatScreen)) return new NeatScreen(cabal)
var self = this
this.cabal = cabal
this.commander = Commander(this, cabal)
this.watcher = null
this.neat = neatLog(view, {fullscreen: true,
style: function (start, cursor, end) {
if (!cursor) cursor = ' '
return start + chalk.underline(cursor) + end
}}
)
this.neat.input.on('update', () => this.neat.render())
this.neat.input.on('enter', (line) => this.commander.process(line))
this.neat.input.on('tab', () => {
var users = Object.keys(self.cabal.users).sort()
var line = self.neat.input.rawLine()
var pattern = (/^(\w+)$/)
var match = pattern.exec(line)
if (match) {
users = users.filter(user => user.startsWith(match[0]))
if (users.length > 0) self.neat.input.set(users[0] + ': ')
}
})
this.neat.input.on('up', () => {
if (self.commander.history.length) {
var command = self.commander.history.pop()
self.commander.history.unshift(command)
self.neat.input.set(command)
}
})
this.neat.input.on('down', () => {
if (self.commander.history.length) {
var command = self.commander.history.shift()
self.commander.history.push(command)
self.neat.input.set(command)
}
})
this.neat.input.on('ctrl-u', () => self.neat.input.set(''))
this.neat.input.on('ctrl-d', () => process.exit(0))
this.neat.input.on('ctrl-w', () => {
const line = this.neat.input.rawLine()
const beforeCursor = line.substring(0, this.neat.input.cursor).replace(/\s*$/, '')
const afterCursor = line.substring(this.neat.input.cursor)
const prunedStart = beforeCursor.split(' ').slice(0, -1).join(' ')
const prunedWithSpace = prunedStart + (prunedStart.length > 0 ? ' ' : '')
this.neat.input.set(prunedWithSpace + afterCursor)
this.neat.input.cursor = prunedWithSpace.length
})
this.neat.use(function (state, bus) {
self.state = state
self.bus = bus
// load initial state of the channel
self.loadChannel('default')
})
self.cabal.on('join', (username) => {
self.writeLine(`* ${username} joined`)
})
self.cabal.on('leave', (username) => {
self.writeLine(`* ${username} left`)
})
function view (state) {
var MAX_MESSAGES = process.stdout.rows - HEADER_ROWS
var msgs = state.messages
if (msgs.length < MAX_MESSAGES) {
msgs = msgs.concat(Array(MAX_MESSAGES - msgs.length).fill())
} else {
msgs = msgs.slice(msgs.length - MAX_MESSAGES, msgs.length)
}
return output(`${chalk.gray('Cabal')}
dat://${self.cabal.db.key.toString('hex')}
${msgs.join('\n')}
[${chalk.cyan(self.cabal.username)}:${state.channel}] ${self.neat.input.line()}`)
}
}
// use to write anything else to the screen, e.g. info messages or emotes
NeatScreen.prototype.writeLine = function (line) {
this.state.messages.push(`${chalk.gray(line)}`)
this.bus.emit('render')
}
NeatScreen.prototype.clear = function () {
this.state.messages = []
this.bus.emit('render')
}
NeatScreen.prototype.loadChannel = function (channel) {
var self = this
self.state.channel = channel
var MAX_MESSAGES = process.stdout.rows - HEADER_ROWS
// clear the old messages array
self.state.messages = []
// if we monitor a new channel, destroy the old watcher first
if (self.watcher) self.watcher.destroy()
function onMessages (err, messages) {
if (err) return
messages.map((arr) => {
arr.forEach((m) => {
self.state.messages.push(self.formatMessage(m))
})
})
self.neat.render()
}
self.cabal.getMessages(channel, MAX_MESSAGES, onMessages)
self.watcher = self.cabal.watch(channel, () => {
self.cabal.getMessages(channel, 1, onMessages)
})
}
NeatScreen.prototype.render = function () {
this.bus.emit('render')
}
NeatScreen.prototype.formatMessage = function (msg) {
var self = this
var hilight = false
var user = self.cabal.username
if (msg.value) { msg = msg.value }
if (msg.content && msg.author && msg.time) {
if (msg.content.indexOf(user) > -1 && msg.author !== user) { hilight = true }
var text = `${chalk.gray(formatTime(msg.time))} ${chalk.gray('<')}${chalk.cyan(msg.author)}${chalk.gray('>')} ${msg.content}`
return hilight ? chalk.bgRed(chalk.black(text)) : text
}
return chalk.cyan('unknown message type: ') + chalk.gray(JSON.stringify(msg))
}
function formatTime (t) {
return strftime('%T', new Date(t))
}
module.exports = NeatScreen