source: frontend/node_modules/prompts/lib/elements/toggle.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.5 KB
Line 
1const color = require('kleur');
2const Prompt = require('./prompt');
3const { style, clear } = require('../util');
4const { cursor, erase } = require('sisteransi');
5
6/**
7 * TogglePrompt Base Element
8 * @param {Object} opts Options
9 * @param {String} opts.message Message
10 * @param {Boolean} [opts.initial=false] Default value
11 * @param {String} [opts.active='no'] Active label
12 * @param {String} [opts.inactive='off'] Inactive label
13 * @param {Stream} [opts.stdin] The Readable stream to listen to
14 * @param {Stream} [opts.stdout] The Writable stream to write readline data to
15 */
16class TogglePrompt extends Prompt {
17 constructor(opts={}) {
18 super(opts);
19 this.msg = opts.message;
20 this.value = !!opts.initial;
21 this.active = opts.active || 'on';
22 this.inactive = opts.inactive || 'off';
23 this.initialValue = this.value;
24 this.render();
25 }
26
27 reset() {
28 this.value = this.initialValue;
29 this.fire();
30 this.render();
31 }
32
33 exit() {
34 this.abort();
35 }
36
37 abort() {
38 this.done = this.aborted = true;
39 this.fire();
40 this.render();
41 this.out.write('\n');
42 this.close();
43 }
44
45 submit() {
46 this.done = true;
47 this.aborted = false;
48 this.fire();
49 this.render();
50 this.out.write('\n');
51 this.close();
52 }
53
54 deactivate() {
55 if (this.value === false) return this.bell();
56 this.value = false;
57 this.render();
58 }
59
60 activate() {
61 if (this.value === true) return this.bell();
62 this.value = true;
63 this.render();
64 }
65
66 delete() {
67 this.deactivate();
68 }
69 left() {
70 this.deactivate();
71 }
72 right() {
73 this.activate();
74 }
75 down() {
76 this.deactivate();
77 }
78 up() {
79 this.activate();
80 }
81
82 next() {
83 this.value = !this.value;
84 this.fire();
85 this.render();
86 }
87
88 _(c, key) {
89 if (c === ' ') {
90 this.value = !this.value;
91 } else if (c === '1') {
92 this.value = true;
93 } else if (c === '0') {
94 this.value = false;
95 } else return this.bell();
96 this.render();
97 }
98
99 render() {
100 if (this.closed) return;
101 if (this.firstRender) this.out.write(cursor.hide);
102 else this.out.write(clear(this.outputText, this.out.columns));
103 super.render();
104
105 this.outputText = [
106 style.symbol(this.done, this.aborted),
107 color.bold(this.msg),
108 style.delimiter(this.done),
109 this.value ? this.inactive : color.cyan().underline(this.inactive),
110 color.gray('/'),
111 this.value ? color.cyan().underline(this.active) : this.active
112 ].join(' ');
113
114 this.out.write(erase.line + cursor.to(0) + this.outputText);
115 }
116}
117
118module.exports = TogglePrompt;
Note: See TracBrowser for help on using the repository browser.