-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
161 lines (125 loc) · 3.53 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
'use strict'
const ui = require('cli-styles')
const esc = require('ansi-escapes')
const chalk = require('chalk')
const wrap = require('prompt-skeleton')
const flattenChoices = (choices) =>
choices.reduce((flat, choice) => {
if (choice.expanded && Array.isArray(choice.children))
return flat.concat(choice, flattenChoices(choice.children))
return flat.concat(choice)
}, [])
const choiceAtCursor = (choices, cursor) => flattenChoices(choices)[cursor]
const nrOfChoices = (choices) => flattenChoices(choices).length
const TreeSelectPrompt = {
moveCursor: function (n) {
this.cursor = n
this.choice = choiceAtCursor(this.values, n)
this.value = this.choice.value
this.emit()
}
, reset: function () {
this.moveCursor(0)
this.render()
}
, abort: function () {
this.done = this.aborted = true
this.emit()
this.render()
this.out.write('\n')
this.close()
}
, submit: function () {
this.done = true
this.aborted = false
this.emit()
this.render()
this.out.write('\n')
this.close()
}
, first: function () {
this.moveCursor(0)
this.render()
}
, last: function () {
this.moveCursor(nrOfChoices(this.values) - 1)
this.render()
}
, up: function () {
if (this.cursor === 0) return this.bell()
this.moveCursor(this.cursor - 1)
this.render()
}
, down: function () {
if (this.cursor === (nrOfChoices(this.values) - 1)) return this.bell()
this.moveCursor(this.cursor + 1)
this.render()
}
, next: function () {
this.moveCursor((this.cursor + 1) % nrOfChoices(this.values))
this.render()
}
, _: function (c) { // on space key
if (c === ' ') {
this.choice.expanded = !this.choice.expanded
this.render()
}
}
, renderChoice: function (choice, indent, selected) {
return ' '.repeat(+indent)
+ chalk.gray(ui.item(Array.isArray(choice.children), choice.expanded)) + ' '
+ (selected ? chalk.cyan.underline(choice.title) : choice.title)
+ '\n'
}
, renderChoices: function (choices, selected, indent = 0, offset = 0) {
let out = ''
for (let choice of choices) {
out += this.renderChoice(choice, indent, offset === selected)
offset++
if (Array.isArray(choice.children)) {
if (choice.expanded)
out += this.renderChoices(choice.children, selected, indent + 4, offset)
offset += choice.children.length
}
}
return out
}
, render: function (first) {
if (first) this.out.write(esc.cursorHide)
else this.out.write(esc.eraseLines(this.previousNrOfChoices + 2))
this.previousNrOfChoices = nrOfChoices(this.values)
this.out.write([
ui.symbol(this.done, this.aborted)
, chalk.bold(this.msg), ui.delimiter(false)
, (this.done ? choiceAtCursor(this.values, this.cursor).title : chalk.gray(this.hint))
].join(' '))
if (!this.done) this.out.write(
'\n'
+ this.renderChoices(this.values, this.cursor)
)
}
}
const defaults = {
hint: '– Space to expand. Return to select.'
, values: []
, value: null
, cursor: 0
, previousNrOfChoices: 0
, done: false
, aborted: false
}
const treeSelectPrompt = (msg, values, opt) => {
if ('string' !== typeof msg) throw new Error('Message must be string.')
if (!Array.isArray(values)) throw new Error('Values must be in an array.')
if (Array.isArray(opt) || 'object' !== typeof opt) opt = {}
let p = Object.assign(Object.create(TreeSelectPrompt), defaults, opt)
p.msg = msg
p.values = values.map((v) => {
v = Object.create(v)
v.expanded = !!v.expanded
return v
})
p.value = p.values[p.cursor].value
return wrap(p)
}
module.exports = Object.assign(treeSelectPrompt, {TreeSelectPrompt})