@reliverse/relinka 1.1.4 → 1.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/dist-npm/components/anykey/index.js +77 -57
  2. package/dist-npm/components/block/block.js +61 -48
  3. package/dist-npm/components/checkbox/index.js +211 -178
  4. package/dist-npm/components/confirm/confirm-main.js +122 -86
  5. package/dist-npm/components/confirm/confirm-three.js +27 -24
  6. package/dist-npm/components/confirm/index.js +45 -36
  7. package/dist-npm/components/core/Separator.js +17 -15
  8. package/dist-npm/components/core/create-prompt.js +124 -101
  9. package/dist-npm/components/core/errors.js +15 -19
  10. package/dist-npm/components/core/hook-engine.js +120 -91
  11. package/dist-npm/components/core/index.js +12 -12
  12. package/dist-npm/components/core/key.js +6 -16
  13. package/dist-npm/components/core/lines.js +54 -45
  14. package/dist-npm/components/core/make-theme.js +30 -21
  15. package/dist-npm/components/core/position.js +23 -27
  16. package/dist-npm/components/core/promise-polyfill.js +14 -11
  17. package/dist-npm/components/core/screen-manager.js +74 -59
  18. package/dist-npm/components/core/theme.js +32 -22
  19. package/dist-npm/components/core/use-effect.js +15 -9
  20. package/dist-npm/components/core/use-keypress.js +23 -19
  21. package/dist-npm/components/core/use-memo.js +16 -10
  22. package/dist-npm/components/core/use-pagination.js +32 -33
  23. package/dist-npm/components/core/use-prefix.js +38 -40
  24. package/dist-npm/components/core/use-ref.js +5 -2
  25. package/dist-npm/components/core/use-state.js +23 -15
  26. package/dist-npm/components/core/useKeyPress.js +17 -14
  27. package/dist-npm/components/core/usePromptState.js +14 -8
  28. package/dist-npm/components/core/utils.js +16 -7
  29. package/dist-npm/components/date/date.js +204 -168
  30. package/dist-npm/components/editor/index.js +92 -71
  31. package/dist-npm/components/expand/index.js +124 -96
  32. package/dist-npm/components/figures/index.js +294 -283
  33. package/dist-npm/components/input/index.js +87 -61
  34. package/dist-npm/components/input/text-main.js +124 -86
  35. package/dist-npm/components/input/text.js +28 -24
  36. package/dist-npm/components/instance/basic.js +25 -17
  37. package/dist-npm/components/instance/browser.js +18 -14
  38. package/dist-npm/components/instance/reporter/basic.js +65 -46
  39. package/dist-npm/components/instance/reporter/browser.js +47 -44
  40. package/dist-npm/components/instance/reporter/fancy.js +96 -83
  41. package/dist-npm/components/instance/shared.js +2 -2
  42. package/dist-npm/components/mono/mono.js +62 -52
  43. package/dist-npm/components/mono/monoTwo.js +49 -35
  44. package/dist-npm/components/multiselect/group-multiselect.js +71 -55
  45. package/dist-npm/components/multiselect/multi-select-two.js +130 -98
  46. package/dist-npm/components/multiselect/multi-select.js +49 -43
  47. package/dist-npm/components/multiselect/multiselect-main.js +146 -120
  48. package/dist-npm/components/multiselect/num-multi-select.js +130 -98
  49. package/dist-npm/components/multiselect/num-multiselect-main.js +35 -24
  50. package/dist-npm/components/next-steps/next-steps.js +25 -24
  51. package/dist-npm/components/number/index.js +112 -78
  52. package/dist-npm/components/number/number-main.js +2 -96
  53. package/dist-npm/components/password/index.js +73 -54
  54. package/dist-npm/components/password/password-main.js +2 -120
  55. package/dist-npm/components/password/password-three.js +30 -26
  56. package/dist-npm/components/progressbar/ProgressBar.js +64 -45
  57. package/dist-npm/components/progressbar/helper.js +40 -33
  58. package/dist-npm/components/progressbar/index.js +1 -1
  59. package/dist-npm/components/prompts/create.js +44 -29
  60. package/dist-npm/components/prompts/index.js +45 -45
  61. package/dist-npm/components/prompts/prompt.js +260 -211
  62. package/dist-npm/components/prompts/promptTwo.js +605 -561
  63. package/dist-npm/components/prompts/relinka.js +295 -237
  64. package/dist-npm/components/range/range.js +294 -247
  65. package/dist-npm/components/rawlist/index.js +107 -87
  66. package/dist-npm/components/results/results.js +37 -31
  67. package/dist-npm/components/search/index.js +193 -148
  68. package/dist-npm/components/select/index.js +186 -148
  69. package/dist-npm/components/select/num-select-main.js +27 -27
  70. package/dist-npm/components/select/num-select.js +5 -125
  71. package/dist-npm/components/select/select-key.js +25 -24
  72. package/dist-npm/components/select/select-main.js +133 -109
  73. package/dist-npm/components/select/select-three.js +36 -32
  74. package/dist-npm/components/select/select-two.js +87 -95
  75. package/dist-npm/components/spinner/index.js +136 -107
  76. package/dist-npm/components/st-end/end.d.ts +1 -1
  77. package/dist-npm/components/st-end/end.js +26 -35
  78. package/dist-npm/components/st-end/start.d.ts +1 -1
  79. package/dist-npm/components/st-end/start.js +15 -30
  80. package/dist-npm/components/toggle/index.js +137 -113
  81. package/dist-npm/components/visual/animate/animate.js +10 -53
  82. package/dist-npm/components/visual/ascii-art/ascii-art.js +1 -12
  83. package/dist-npm/mod.js +2 -1
  84. package/dist-npm/testing/index.js +83 -58
  85. package/dist-npm/types/general.d.ts +2 -1
  86. package/dist-npm/types/general.js +1 -0
  87. package/dist-npm/types/index.js +3 -2
  88. package/dist-npm/types/keypress.js +36 -35
  89. package/dist-npm/types/readline.js +1 -0
  90. package/dist-npm/types/relinka.js +1 -0
  91. package/dist-npm/types/utils.js +1 -0
  92. package/dist-npm/utils/box.js +137 -135
  93. package/dist-npm/utils/color.js +74 -65
  94. package/dist-npm/utils/colorize.js +156 -124
  95. package/dist-npm/utils/component.js +657 -532
  96. package/dist-npm/utils/constants.js +63 -64
  97. package/dist-npm/utils/core.js +3 -2
  98. package/dist-npm/utils/decoder.js +223 -244
  99. package/dist-npm/utils/error.js +9 -4
  100. package/dist-npm/utils/errors.js +4 -14
  101. package/dist-npm/utils/format.js +24 -19
  102. package/dist-npm/utils/keypress.js +414 -316
  103. package/dist-npm/utils/log.js +15 -11
  104. package/dist-npm/utils/mapping.js +52 -45
  105. package/dist-npm/utils/messages.js +183 -183
  106. package/dist-npm/utils/platforms.js +20 -16
  107. package/dist-npm/utils/prompt-tmp.js +286 -235
  108. package/dist-npm/utils/prompt-two.js +286 -235
  109. package/dist-npm/utils/readline.js +7 -5
  110. package/dist-npm/utils/skeleton.js +170 -130
  111. package/dist-npm/utils/stream.js +2 -2
  112. package/dist-npm/utils/string.js +58 -44
  113. package/dist-npm/utils/terminal.js +34 -23
  114. package/dist-npm/utils/tree.js +41 -30
  115. package/dist-npm/utils/types.js +1 -0
  116. package/dist-npm/utils/utils.js +8 -8
  117. package/dist-npm/utils/variants.js +44 -36
  118. package/package.json +20 -28
@@ -1,45 +1,45 @@
1
- export { colorize } from "../../utils/colorize.js";
2
- export { fmt, msg } from "../../utils/messages.js";
3
- export { errorHandler } from "../../utils/errors.js";
4
- export { colorMap } from "../../utils/mapping.js";
5
- export { animateText } from "../../components/visual/animate/animate.js";
6
- export { createAsciiArt } from "../../components/visual/ascii-art/ascii-art.js";
7
- export { startPrompt } from "../../components/st-end/start.js";
8
- export { anykeyPrompt } from "../../components/anykey/index.js";
9
- export { textPrompt } from "../../components/input/text-main.js";
10
- export { confirmPrompt } from "../../components/confirm/confirm-main.js";
11
- export { togglePrompt } from "../../components/toggle/index.js";
12
- export { datePrompt } from "../../components/date/date.js";
13
- export { selectPrompt } from "../../components/select/select-main.js";
14
- export { multiselectPrompt } from "../../components/multiselect/multiselect-main.js";
15
- export { numSelectPrompt } from "../../components/select/num-select.js";
16
- export { numMultiSelectPrompt } from "../../components/multiselect/num-multi-select.js";
17
- export { nextStepsPrompt } from "../../components/next-steps/next-steps.js";
18
- export { numberPrompt } from "../../components/number/number-main.js";
19
- export { passwordPrompt } from "../../components/password/password-main.js";
20
- export { endPrompt } from "../../components/st-end/end.js";
21
- export { progressbar } from "../../components/progressbar/index.js";
22
- export { promptsDisplayResults } from "../../components/results/results.js";
23
- export { prompt } from "../../components/mono/mono.js";
24
- export { default as block } from "../../components/block/block.js";
25
- export { spinner } from "../../components/spinner/index.js";
26
- export { default as checkbox, Separator } from "../../components/checkbox/index.js";
27
- export { default as expand } from "../../components/expand/index.js";
28
- export { default as rawlist } from "../../components/rawlist/index.js";
29
- export { default as search } from "../../components/search/index.js";
30
- export { default as select } from "../../components/select/index.js";
31
- export { default as editor } from "../../components/editor/index.js";
32
- export { default as confirm } from "../../components/confirm/index.js";
33
- export { default as input } from "../../components/input/index.js";
34
- export { default as number } from "../../components/number/index.js";
35
- export { default as password } from "../../components/password/index.js";
36
- export { default as ConfirmPrompt } from "../../components/confirm/confirm-three.js";
37
- export { default as GroupMultiSelectPrompt } from "../../components/multiselect/group-multiselect.js";
38
- export { default as MultiSelectPrompt } from "../../components/multiselect/multi-select.js";
39
- export { default as PasswordPrompt } from "../../components/password/password-three.js";
40
- export { default as Prompt, isCancel } from "../../components/prompts/prompt.js";
41
- export { default as SelectPrompt } from "../../components/select/select-three.js";
42
- export { default as SelectKeyPrompt } from "../../components/select/select-key.js";
43
- export { default as TextPrompt } from "../../components/input/text.js";
44
- export { multiselect } from "../../components/prompts/promptTwo.js";
45
- export { relinka } from "../../components/prompts/create.js";
1
+ export {colorize} from '../../utils/colorize.js';
2
+ export {fmt, msg} from '../../utils/messages.js';
3
+ export {errorHandler} from '../../utils/errors.js';
4
+ export {colorMap} from '../../utils/mapping.js';
5
+ export {animateText} from '../../components/visual/animate/animate.js';
6
+ export {createAsciiArt} from '../../components/visual/ascii-art/ascii-art.js';
7
+ export {startPrompt} from '../../components/st-end/start.js';
8
+ export {anykeyPrompt} from '../../components/anykey/index.js';
9
+ export {textPrompt} from '../../components/input/text-main.js';
10
+ export {confirmPrompt} from '../../components/confirm/confirm-main.js';
11
+ export {togglePrompt} from '../../components/toggle/index.js';
12
+ export {datePrompt} from '../../components/date/date.js';
13
+ export {selectPrompt} from '../../components/select/select-main.js';
14
+ export {multiselectPrompt} from '../../components/multiselect/multiselect-main.js';
15
+ export {numSelectPrompt} from '../../components/select/num-select.js';
16
+ export {numMultiSelectPrompt} from '../../components/multiselect/num-multi-select.js';
17
+ export {nextStepsPrompt} from '../../components/next-steps/next-steps.js';
18
+ export {numberPrompt} from '../../components/number/number-main.js';
19
+ export {passwordPrompt} from '../../components/password/password-main.js';
20
+ export {endPrompt} from '../../components/st-end/end.js';
21
+ export {progressbar} from '../../components/progressbar/index.js';
22
+ export {promptsDisplayResults} from '../../components/results/results.js';
23
+ export {prompt} from '../../components/mono/mono.js';
24
+ export {default as block} from '../../components/block/block.js';
25
+ export {spinner} from '../../components/spinner/index.js';
26
+ export {default as checkbox, Separator} from '../../components/checkbox/index.js';
27
+ export {default as expand} from '../../components/expand/index.js';
28
+ export {default as rawlist} from '../../components/rawlist/index.js';
29
+ export {default as search} from '../../components/search/index.js';
30
+ export {default as select} from '../../components/select/index.js';
31
+ export {default as editor} from '../../components/editor/index.js';
32
+ export {default as confirm} from '../../components/confirm/index.js';
33
+ export {default as input} from '../../components/input/index.js';
34
+ export {default as number} from '../../components/number/index.js';
35
+ export {default as password} from '../../components/password/index.js';
36
+ export {default as ConfirmPrompt} from '../../components/confirm/confirm-three.js';
37
+ export {default as GroupMultiSelectPrompt} from '../../components/multiselect/group-multiselect.js';
38
+ export {default as MultiSelectPrompt} from '../../components/multiselect/multi-select.js';
39
+ export {default as PasswordPrompt} from '../../components/password/password-three.js';
40
+ export {default as Prompt, isCancel} from '../../components/prompts/prompt.js';
41
+ export {default as SelectPrompt} from '../../components/select/select-three.js';
42
+ export {default as SelectKeyPrompt} from '../../components/select/select-key.js';
43
+ export {default as TextPrompt} from '../../components/input/text.js';
44
+ export {multiselect} from '../../components/prompts/promptTwo.js';
45
+ export {relinka} from '../../components/prompts/create.js';
@@ -1,232 +1,281 @@
1
- import { stdin, stdout } from "node:process";
2
- import readline from "node:readline";
3
- import { WriteStream } from "node:tty";
4
- import { cursor, erase } from "sisteransi";
5
- import wrap from "wrap-ansi";
1
+ import process, {stdin, stdout} from 'node:process';
2
+ import readline from 'node:readline';
3
+ import {WriteStream} from 'node:tty';
4
+ import {cursor, erase} from 'sisteransi';
5
+ import wrap from 'wrap-ansi';
6
+
6
7
  function diffLines(a, b) {
7
- if (a === b) {
8
- return;
9
- }
10
- const aLines = a.split("\n");
11
- const bLines = b.split("\n");
12
- const diff = [];
13
- for (let i = 0; i < Math.max(aLines.length, bLines.length); i++) {
14
- if (aLines[i] !== bLines[i]) {
15
- diff.push(i);
8
+ if (a === b) {
9
+ return;
10
+ }
11
+
12
+ const aLines = a.split('\n');
13
+ const bLines = b.split('\n');
14
+ const diff = [];
15
+
16
+ for (let i = 0; i < Math.max(aLines.length, bLines.length); i++) {
17
+ if (aLines[i] !== bLines[i]) {
18
+ diff.push(i);
19
+ }
16
20
  }
17
- }
18
- return diff;
21
+
22
+ return diff;
19
23
  }
20
- const cancel = Symbol("rp:cancel");
24
+
25
+ const cancel = Symbol('rp:cancel');
26
+
21
27
  export function isCancel(value) {
22
- return value === cancel;
28
+ return value === cancel;
23
29
  }
30
+
24
31
  function setRawMode(input, value) {
25
- if (input.isTTY) {
26
- input.setRawMode(value);
27
- }
32
+ if (input.isTTY) {
33
+ input.setRawMode(value);
34
+ }
28
35
  }
29
- const aliases = new Map([
30
- ["k", "up"],
31
- ["j", "down"],
32
- ["h", "left"],
33
- ["l", "right"]
36
+
37
+ const aliases = new Map([
38
+ ['k', 'up'],
39
+ ['j', 'down'],
40
+ ['h', 'left'],
41
+ ['l', 'right'],
42
+ ]);
43
+
44
+ const keys = new Set([
45
+ 'up',
46
+ 'down',
47
+ 'left',
48
+ 'right',
49
+ 'space',
50
+ 'enter',
34
51
  ]);
35
- const keys = new Set(["up", "down", "left", "right", "space", "enter"]);
52
+
36
53
  export default class Prompt {
37
- input;
38
- output;
39
- rl;
40
- opts;
41
- _track = false;
42
- _render;
43
- _cursor = 0;
44
- state = "initial";
45
- value;
46
- error = "";
47
- constructor({
48
- render,
49
- input = stdin,
50
- output = stdout,
51
- initialValue = "",
52
- ...opts
53
- }, trackValue = true) {
54
- this.opts = opts;
55
- this.onKeypress = this.onKeypress.bind(this);
56
- this.close = this.close.bind(this);
57
- this.render = this.render.bind(this);
58
- this._render = render.bind(this);
59
- this._track = trackValue;
60
- this.input = input;
61
- this.output = output;
62
- this.value = initialValue ?? "";
63
- }
64
- prompt() {
65
- const sink = new WriteStream(0);
66
- sink._write = (chunk, encoding, done) => {
67
- if (this._track) {
68
- this.value = this.rl.line.replace(/\t/g, "");
69
- this._cursor = this.rl.cursor;
70
- this.emit("value", this.value);
71
- }
72
- done();
73
- };
74
- this.input.pipe(sink);
75
- this.rl = readline.createInterface({
76
- input: this.input,
77
- output: sink,
78
- tabSize: 2,
79
- prompt: "",
80
- escapeCodeTimeout: 50
81
- });
82
- readline.emitKeypressEvents(this.input, this.rl);
83
- this.rl.prompt();
84
- if (this.opts.initialValue !== void 0 && this._track) {
85
- this.rl.write(this.opts.initialValue);
86
- }
87
- this.input.on("keypress", this.onKeypress);
88
- setRawMode(this.input, true);
89
- this.output.on("resize", this.render);
90
- this.render();
91
- return new Promise((resolve, reject) => {
92
- this.once("submit", () => {
93
- this.output.write(cursor.show);
94
- this.output.off("resize", this.render);
95
- setRawMode(this.input, false);
96
- resolve(this.value);
97
- });
98
- this.once("cancel", () => {
99
- this.output.write(cursor.show);
100
- this.output.off("resize", this.render);
101
- setRawMode(this.input, false);
102
- resolve(cancel);
103
- });
104
- });
105
- }
106
- subscribers = new Map();
107
- on(event, cb) {
108
- const arr = this.subscribers.get(event) ?? [];
109
- arr.push({ cb });
110
- this.subscribers.set(event, arr);
111
- }
112
- once(event, cb) {
113
- const arr = this.subscribers.get(event) ?? [];
114
- arr.push({ cb, once: true });
115
- this.subscribers.set(event, arr);
116
- }
117
- emit(event, ...data) {
118
- const cbs = this.subscribers.get(event) ?? [];
119
- const cleanup = [];
120
- for (const subscriber of cbs) {
121
- subscriber.cb(...data);
122
- if (subscriber.once) {
123
- cleanup.push(() => cbs.splice(cbs.indexOf(subscriber), 1));
124
- }
125
- }
126
- for (const cb of cleanup) {
127
- cb();
54
+ input;
55
+ output;
56
+ rl;
57
+ opts;
58
+ _track = false;
59
+ _render;
60
+ _cursor = 0;
61
+ state = 'initial';
62
+ value;
63
+ error = '';
64
+ constructor({render, input = stdin, output = stdout, initialValue = '', ...opts}, trackValue = true) {
65
+ this.opts = opts;
66
+ this.onKeypress = this.onKeypress.bind(this);
67
+ this.close = this.close.bind(this);
68
+ this.render = this.render.bind(this);
69
+ this._render = render.bind(this);
70
+ this._track = trackValue;
71
+ this.input = input;
72
+ this.output = output;
73
+ this.value = initialValue ?? '';
128
74
  }
129
- }
130
- unsubscribe() {
131
- this.subscribers.clear();
132
- }
133
- onKeypress(char, key) {
134
- if (this.state === "error") {
135
- this.state = "active";
136
- }
137
- if (key?.name && !this._track && aliases.has(key.name)) {
138
- this.emit("cursor", aliases.get(key.name));
139
- }
140
- if (key?.name && keys.has(key.name)) {
141
- this.emit("cursor", key.name);
142
- }
143
- if (char && (char.toLowerCase() === "y" || char.toLowerCase() === "n")) {
144
- this.emit("confirm", char.toLowerCase() === "y");
75
+
76
+ prompt() {
77
+ const sink = new WriteStream(0);
78
+
79
+ sink._write = (chunk, encoding, done) => {
80
+ if (this._track) {
81
+ this.value = this.rl.line.replace(/\t/g, '');
82
+ this._cursor = this.rl.cursor;
83
+ this.emit('value', this.value);
84
+ }
85
+
86
+ done();
87
+ };
88
+ this.input.pipe(sink);
89
+ this.rl = readline.createInterface({
90
+ input: this.input,
91
+ output: sink,
92
+ tabSize: 2,
93
+ prompt: '',
94
+ escapeCodeTimeout: 50,
95
+ });
96
+ readline.emitKeypressEvents(this.input, this.rl);
97
+ this.rl.prompt();
98
+ if (this.opts.initialValue !== void 0 && this._track) {
99
+ this.rl.write(this.opts.initialValue);
100
+ }
101
+
102
+ this.input.on('keypress', this.onKeypress);
103
+ setRawMode(this.input, true);
104
+ this.output.on('resize', this.render);
105
+ this.render();
106
+ return new Promise((resolve) => {
107
+ this.once('submit', () => {
108
+ this.output.write(cursor.show);
109
+ this.output.off('resize', this.render);
110
+ setRawMode(this.input, false);
111
+ resolve(this.value);
112
+ });
113
+ this.once('cancel', () => {
114
+ this.output.write(cursor.show);
115
+ this.output.off('resize', this.render);
116
+ setRawMode(this.input, false);
117
+ resolve(cancel);
118
+ });
119
+ });
145
120
  }
146
- if (char === " " && this.opts.placeholder) {
147
- if (!this.value) {
148
- this.rl.write(this.opts.placeholder);
149
- this.emit("value", this.opts.placeholder);
150
- }
121
+
122
+ subscribers = new Map();
123
+ on(event, cb) {
124
+ const arr = this.subscribers.get(event) ?? [];
125
+ arr.push({
126
+ cb,
127
+ });
128
+ this.subscribers.set(event, arr);
151
129
  }
152
- if (char) {
153
- this.emit("key", char.toLowerCase());
130
+
131
+ once(event, cb) {
132
+ const arr = this.subscribers.get(event) ?? [];
133
+ arr.push({
134
+ cb,
135
+ once: true,
136
+ });
137
+ this.subscribers.set(event, arr);
154
138
  }
155
- if (key?.name === "return") {
156
- if (this.opts.validate) {
157
- const problem = this.opts.validate(this.value);
158
- if (problem) {
159
- this.error = problem;
160
- this.state = "error";
161
- this.rl.write(this.value);
162
- }
163
- }
164
- if (this.state !== "error") {
165
- this.state = "submit";
166
- }
139
+
140
+ emit(event, ...data) {
141
+ const cbs = this.subscribers.get(event) ?? [];
142
+ const cleanup = [];
143
+
144
+ for (const subscriber of cbs) {
145
+ subscriber.cb(...data);
146
+ if (subscriber.once) {
147
+ cleanup.push(() => cbs.splice(cbs.indexOf(subscriber), 1));
148
+ }
149
+ }
150
+
151
+ for (const cb of cleanup) {
152
+ cb();
153
+ }
167
154
  }
168
- if (char === "") {
169
- this.state = "cancel";
155
+
156
+ unsubscribe() {
157
+ this.subscribers.clear();
170
158
  }
171
- if (this.state === "submit" || this.state === "cancel") {
172
- this.emit("finalize");
159
+
160
+ onKeypress(char, key) {
161
+ if (this.state === 'error') {
162
+ this.state = 'active';
163
+ }
164
+
165
+ if (key?.name && !this._track && aliases.has(key.name)) {
166
+ this.emit('cursor', aliases.get(key.name));
167
+ }
168
+
169
+ if (key?.name && keys.has(key.name)) {
170
+ this.emit('cursor', key.name);
171
+ }
172
+
173
+ if (char && (char.toLowerCase() === 'y' || char.toLowerCase() === 'n')) {
174
+ this.emit('confirm', char.toLowerCase() === 'y');
175
+ }
176
+
177
+ if (char === ' ' && this.opts.placeholder && !this.value) {
178
+ this.rl.write(this.opts.placeholder);
179
+ this.emit('value', this.opts.placeholder);
180
+ }
181
+
182
+ if (char) {
183
+ this.emit('key', char.toLowerCase());
184
+ }
185
+
186
+ if (key?.name === 'return') {
187
+ if (this.opts.validate) {
188
+ const problem = this.opts.validate(this.value);
189
+
190
+ if (problem) {
191
+ this.error = problem;
192
+ this.state = 'error';
193
+ this.rl.write(this.value);
194
+ }
195
+ }
196
+
197
+ if (this.state !== 'error') {
198
+ this.state = 'submit';
199
+ }
200
+ }
201
+
202
+ if (char === '') {
203
+ this.state = 'cancel';
204
+ }
205
+
206
+ if (this.state === 'submit' || this.state === 'cancel') {
207
+ this.emit('finalize');
208
+ }
209
+
210
+ this.render();
211
+ if (this.state === 'submit' || this.state === 'cancel') {
212
+ this.close();
213
+ }
173
214
  }
174
- this.render();
175
- if (this.state === "submit" || this.state === "cancel") {
176
- this.close();
215
+
216
+ close() {
217
+ this.input.unpipe();
218
+ this.input.removeListener('keypress', this.onKeypress);
219
+ this.output.write('\n');
220
+ setRawMode(this.input, false);
221
+ this.rl.close();
222
+ this.emit(this.state, this.value);
223
+ this.unsubscribe();
177
224
  }
178
- }
179
- close() {
180
- this.input.unpipe();
181
- this.input.removeListener("keypress", this.onKeypress);
182
- this.output.write("\n");
183
- setRawMode(this.input, false);
184
- this.rl.close();
185
- this.emit(this.state, this.value);
186
- this.unsubscribe();
187
- }
188
- restoreCursor() {
189
- const lines = wrap(this._prevFrame, process.stdout.columns, { hard: true }).split("\n").length - 1;
190
- this.output.write(cursor.move(-999, lines * -1));
191
- }
192
- _prevFrame = "";
193
- render() {
194
- const frame = wrap(this._render(this) ?? "", process.stdout.columns, {
195
- hard: true
196
- });
197
- if (frame === this._prevFrame) {
198
- return;
225
+
226
+ restoreCursor() {
227
+ const lines = wrap(this._prevFrame, process.stdout.columns, {
228
+ hard: true,
229
+ }).split('\n').length - 1;
230
+ this.output.write(cursor.move(-999, lines * -1));
199
231
  }
200
- if (this.state === "initial") {
201
- this.output.write(cursor.hide);
202
- } else {
203
- const diff = diffLines(this._prevFrame, frame);
204
- this.restoreCursor();
205
- if (diff && diff.length === 1) {
206
- const diffLine = diff[0];
207
- this.output.write(cursor.move(0, diffLine));
208
- this.output.write(erase.lines(1));
209
- const lines = frame.split("\n");
210
- this.output.write(lines[diffLine]);
211
- this._prevFrame = frame;
212
- this.output.write(cursor.move(0, lines.length - diffLine - 1));
213
- return;
214
- } else if (diff && diff.length > 1) {
215
- const diffLine = diff[0];
216
- this.output.write(cursor.move(0, diffLine));
217
- this.output.write(erase.down());
218
- const lines = frame.split("\n");
219
- const newLines = lines.slice(diffLine);
220
- this.output.write(newLines.join("\n"));
232
+
233
+ _prevFrame = '';
234
+ render() {
235
+ const frame = wrap(this._render(this) ?? '', process.stdout.columns, {
236
+ hard: true,
237
+ });
238
+
239
+ if (frame === this._prevFrame) {
240
+ return;
241
+ }
242
+
243
+ if (this.state === 'initial') {
244
+ this.output.write(cursor.hide);
245
+ } else {
246
+ const diff = diffLines(this._prevFrame, frame);
247
+ this.restoreCursor();
248
+ if (diff && diff.length === 1) {
249
+ const [diffLine] = diff;
250
+ this.output.write(cursor.move(0, diffLine));
251
+ this.output.write(erase.lines(1));
252
+ const lines = frame.split('\n');
253
+ this.output.write(lines[diffLine]);
254
+ this._prevFrame = frame;
255
+ this.output.write(cursor.move(0, lines.length - diffLine - 1));
256
+ return;
257
+ }
258
+
259
+ if (diff && diff.length > 1) {
260
+ const [diffLine] = diff;
261
+ this.output.write(cursor.move(0, diffLine));
262
+ this.output.write(erase.down());
263
+ const lines = frame.split('\n');
264
+ const newLines = lines.slice(diffLine);
265
+
266
+ this.output.write(newLines.join('\n'));
267
+ this._prevFrame = frame;
268
+ return;
269
+ }
270
+
271
+ this.output.write(erase.down());
272
+ }
273
+
274
+ this.output.write(frame);
275
+ if (this.state === 'initial') {
276
+ this.state = 'active';
277
+ }
278
+
221
279
  this._prevFrame = frame;
222
- return;
223
- }
224
- this.output.write(erase.down());
225
- }
226
- this.output.write(frame);
227
- if (this.state === "initial") {
228
- this.state = "active";
229
280
  }
230
- this._prevFrame = frame;
231
- }
232
281
  }