This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
152 lines (118 loc) · 3.01 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
'use strict'
const ui = require('cli-styles')
const esc = require('ansi-escapes')
const chalk = require('chalk')
const figures = require('figures')
const wrap = require('prompt-skeleton')
const MultiselectPrompt = {
reset: function () {
for (let v of this.value) v.selected = false
this.cursor = 0
this.emit()
this.render()
}
, selected: function () {
return this.value.filter((v) => v.selected)
}
, 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.cursor = 0
this.render()
}
, last: function () {
this.cursor = this.value.length - 1
this.render()
}
, up: function () {
if (this.cursor === 0) return this.bell()
this.cursor--
this.render()
}
, down: function () {
if (this.cursor === (this.value.length - 1)) return this.bell()
this.cursor++
this.render()
}
, next: function () {
this.cursor = (this.cursor + 1) % this.value.length
this.render()
}
, left: function () {
this.value[this.cursor].selected = false
this.render()
}
, right: function () {
if (this.value.filter (e => e.selected).length >= this.maxChoices)
return this.bell()
this.value[this.cursor].selected = true
this.render()
}
, _: function (c) { // on space key
if (c !== ' ') return this.bell()
const v = this.value[this.cursor]
if (v.selected) {
v.selected = false
this.render()
}
else if (this.value.filter (e => e.selected).length >= this.maxChoices)
return this.bell()
else {
v.selected = true
this.render()
}
}
, lastRendered: ''
, render: function (first) {
if (first) this.out.write(esc.cursorHide)
let prompt = [
ui.symbol(this.done, this.aborted)
, chalk.bold(this.msg)
, this.done ? '' : chalk.gray(this.hint)
].join(' ')
if (!this.done) {
const c = this.cursor
prompt += '\n' + this.value.map((v, i) =>
(v.selected ? chalk.green(figures.tick) : ' ') + ' '
+ (c === i ? chalk.cyan.underline(v.title) : v.title)
).join('\n')
}
this.out.write(ui.clear(this.lastRendered) + prompt)
this.lastRendered = prompt
}
}
const defaults = {
hint: '– Space to select. Return to submit.'
, value: []
, cursor: 0
, done: false
, aborted: false
}
const multiselectPrompt = (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 = {}
values = values.map((v) => {
v = Object.create(v)
if (!('selected' in v)) v.selected = false
return v
})
let p = Object.assign(Object.create(MultiselectPrompt), defaults, opt)
p.msg = msg
p.value = values // singular for compatibility with prompt-skeleton
return wrap(p)
}
module.exports = Object.assign(multiselectPrompt, {MultiselectPrompt})