This repository has been archived by the owner on Jan 23, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
115 lines (94 loc) · 2.78 KB
/
app.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
const neatLog = require('neat-log')
const chalk = require('chalk')
const State = require('./state')
const Commander = require('./commander')
const Layout = require('./layout')
class App {
constructor (rc, dc) {
const state = new State(rc, dc)
const commander = new Commander(state, dc)
const layout = new Layout(rc)
const render = layout.render.bind(layout)
const userInput = () => {
const arr = state.userInput
return arr.length > 0 ? arr[0].question() : ''
}
const { input, use } = neatLog(render, {
fullscreen: true,
logspeed: rc.logspeed,
state: state,
style: (start, cursor, end) => {
if (!cursor) cursor = ' '
return userInput() + start + chalk.bgWhite(cursor) + end
}
})
use((state, bus) => {
state.input = input
this.bus = bus
})
const pageUp = () => {
state.currentPage().pageUp(layout.maxPageHeight())
}
const pageDown = () => {
state.currentPage().pageDown()
}
input.on('ctrl-n', () => state.nextPage())
input.on('ctrl-p', () => state.prevPage())
input.on('pageup', pageUp)
input.on('pagedown', pageDown)
input.on('alt-p', pageUp)
input.on('alt-n', pageDown)
for (let i = 1; i <= 9; ++i) {
input.on(`alt-${i}`, () => state.setCurrentPageIndex(i - 1))
}
input.on('enter', line => {
if (state.userInput.length > 0) {
line = line.trim().toLowerCase()
const answer = state.userInput[0].answers[line]
if (typeof answer === 'function') {
answer()
state.userInput.shift()
}
} else {
commander.onEnter(line)
}
})
input.on('tab', () => {
if (state.userInput.length === 0) {
commander.onTab()
}
})
input.on('up', commander.onUp.bind(commander))
input.on('down', commander.onDown.bind(commander))
input.on('keypress', (ch, key) => this.render())
dc.on('ready', () => {
state.loadChats()
this.render()
})
dc.on('ALL', (event, data1, data2) => {
state.logEvent(event, data1, data2)
this.render()
})
dc.on('DC_EVENT_MSGS_CHANGED', (chatId, msgId) => {
const msg = dc.getMessage(msgId)
if (msg === null) return
if (msg.getState().isPending()) {
state.appendMessage(chatId, msgId)
} else if (msg.isDeadDrop()) {
state.queueDeadDropMessage(msg)
}
})
dc.on('DC_EVENT_INCOMING_MSG', (chatId, msgId) => {
state.appendMessage(chatId, msgId)
})
dc.on('DC_EVENT_WARNING', state.warning.bind(state))
dc.on('DC_EVENT_ERROR', (code, error) => {
state.error(`${error} (code = ${code})`)
})
this.render()
}
render () {
this.bus.emit('render')
}
}
module.exports = (opts, dc) => new App(opts, dc)