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