-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
73 lines (57 loc) · 1.63 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
const readline = require('readline')
const styles = require('./lib/styles')
const keymap = require('./lib/keymap')
const colors = require('./lib/colors')
const {
tty, EOL, Mid, center, hideCursor, tagged, PAD, len
} = require('./lib/utils')
const { faint, conceal, fragFlag } = styles
module.exports = class Qov {
constructor (opts = {}) {
this.slides = []
this.fragments = []
this.frags = []
this.sliceIndex = 0
this.index = 0
this.fragIndex = this.fragCounter = this.escapeCounter = 0
this.step = opts.step || 2000
this.keymap = Object.assign(keymap, opts.keymap)
// link extral apis
this.styles =
this.colors = colors
require('./lib/controls')(this)
hideCursor()
}
// String.raw() function
section (...args) {
tagged(slide => {
if (slide.includes(fragFlag)) {
this.frags.push(this.fragments.slice(this.sliceIndex))
this.sliceIndex += this.fragments.length
} else {
this.frags.push('')
}
this.slides.push(Mid(center(slide), tty.rows))
})(...args)
}
// Create fragments
fragment (...args) {
return tagged(fragment => {
this.fragments.push(fragment)
return fragment.split(EOL)
.map(item => conceal(PAD.repeat(len(item))))
.join(EOL)
})(...args)
}
// Render slides
render (index = 0) {
const total = this.slides.length
this.index = index
readline.cursorTo(tty, 0, 0)
readline.clearScreenDown(tty)
tty.write(this.slides[this.index])
// Footer counter
tty.write(center(faint(`${this.index + 1} of ${total}`)))
this.fragCounter = this.fragIndex = 0
}
}