@reliverse/relinka 1.1.7 → 1.1.8

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 (121) hide show
  1. package/dist-npm/components/anykey/index.js +57 -77
  2. package/dist-npm/components/block/block.js +48 -61
  3. package/dist-npm/components/checkbox/index.js +178 -211
  4. package/dist-npm/components/confirm/confirm-main.js +85 -122
  5. package/dist-npm/components/confirm/confirm-three.js +24 -27
  6. package/dist-npm/components/confirm/index.js +36 -45
  7. package/dist-npm/components/core/Separator.js +15 -17
  8. package/dist-npm/components/core/create-prompt.js +101 -124
  9. package/dist-npm/components/core/errors.js +19 -15
  10. package/dist-npm/components/core/hook-engine.js +91 -120
  11. package/dist-npm/components/core/index.js +12 -12
  12. package/dist-npm/components/core/key.js +16 -6
  13. package/dist-npm/components/core/lines.js +45 -54
  14. package/dist-npm/components/core/make-theme.js +21 -30
  15. package/dist-npm/components/core/position.js +27 -23
  16. package/dist-npm/components/core/promise-polyfill.js +11 -14
  17. package/dist-npm/components/core/screen-manager.js +59 -74
  18. package/dist-npm/components/core/theme.js +22 -32
  19. package/dist-npm/components/core/use-effect.js +9 -15
  20. package/dist-npm/components/core/use-keypress.js +19 -23
  21. package/dist-npm/components/core/use-memo.js +10 -16
  22. package/dist-npm/components/core/use-pagination.js +33 -32
  23. package/dist-npm/components/core/use-prefix.js +40 -38
  24. package/dist-npm/components/core/use-ref.js +2 -5
  25. package/dist-npm/components/core/use-state.js +15 -23
  26. package/dist-npm/components/core/useKeyPress.js +14 -17
  27. package/dist-npm/components/core/usePromptState.js +8 -14
  28. package/dist-npm/components/core/utils.js +7 -16
  29. package/dist-npm/components/date/date.js +173 -204
  30. package/dist-npm/components/editor/index.js +71 -92
  31. package/dist-npm/components/expand/index.js +96 -124
  32. package/dist-npm/components/figures/index.js +283 -294
  33. package/dist-npm/components/input/index.js +61 -87
  34. package/dist-npm/components/input/text-main.js +97 -124
  35. package/dist-npm/components/input/text.js +24 -28
  36. package/dist-npm/components/instance/basic.js +17 -25
  37. package/dist-npm/components/instance/browser.js +14 -18
  38. package/dist-npm/components/instance/reporter/basic.js +46 -65
  39. package/dist-npm/components/instance/reporter/browser.js +44 -47
  40. package/dist-npm/components/instance/reporter/fancy.js +83 -96
  41. package/dist-npm/components/instance/shared.js +2 -2
  42. package/dist-npm/components/mono/mono.js +52 -62
  43. package/dist-npm/components/mono/monoTwo.js +35 -49
  44. package/dist-npm/components/multiselect/group-multiselect.js +55 -71
  45. package/dist-npm/components/multiselect/multi-select-two.js +97 -130
  46. package/dist-npm/components/multiselect/multi-select.js +43 -49
  47. package/dist-npm/components/multiselect/multiselect-main.d.ts +1 -0
  48. package/dist-npm/components/multiselect/multiselect-main.js +158 -145
  49. package/dist-npm/components/multiselect/num-multi-select.js +97 -130
  50. package/dist-npm/components/multiselect/num-multiselect-main.js +24 -35
  51. package/dist-npm/components/next-steps/next-steps.js +23 -25
  52. package/dist-npm/components/number/index.js +78 -112
  53. package/dist-npm/components/number/number-main.js +95 -2
  54. package/dist-npm/components/password/index.js +54 -73
  55. package/dist-npm/components/password/password-main.js +119 -2
  56. package/dist-npm/components/password/password-three.js +26 -30
  57. package/dist-npm/components/progressbar/ProgressBar.js +45 -64
  58. package/dist-npm/components/progressbar/helper.js +33 -40
  59. package/dist-npm/components/progressbar/index.js +1 -1
  60. package/dist-npm/components/prompts/create.js +29 -44
  61. package/dist-npm/components/prompts/index.d.ts +1 -0
  62. package/dist-npm/components/prompts/index.js +46 -45
  63. package/dist-npm/components/prompts/prompt.js +211 -260
  64. package/dist-npm/components/prompts/promptTwo.js +561 -605
  65. package/dist-npm/components/prompts/relinka.js +237 -295
  66. package/dist-npm/components/range/range.js +247 -294
  67. package/dist-npm/components/rawlist/index.js +87 -107
  68. package/dist-npm/components/results/results.js +31 -37
  69. package/dist-npm/components/search/index.js +148 -193
  70. package/dist-npm/components/select/index.js +148 -186
  71. package/dist-npm/components/select/num-select-main.js +27 -27
  72. package/dist-npm/components/select/num-select.js +124 -5
  73. package/dist-npm/components/select/select-key.js +24 -25
  74. package/dist-npm/components/select/select-main.d.ts +1 -0
  75. package/dist-npm/components/select/select-main.js +143 -133
  76. package/dist-npm/components/select/select-three.js +32 -36
  77. package/dist-npm/components/select/select-two.js +94 -87
  78. package/dist-npm/components/spinner/index.js +107 -136
  79. package/dist-npm/components/st-end/end.js +34 -26
  80. package/dist-npm/components/st-end/start.js +29 -15
  81. package/dist-npm/components/toggle/index.js +113 -137
  82. package/dist-npm/components/visual/animate/animate.js +53 -10
  83. package/dist-npm/components/visual/ascii-art/ascii-art.js +12 -1
  84. package/dist-npm/main.js +1 -0
  85. package/dist-npm/testing/index.js +58 -83
  86. package/dist-npm/types/general.d.ts +1 -1
  87. package/dist-npm/types/general.js +0 -1
  88. package/dist-npm/types/index.js +2 -3
  89. package/dist-npm/types/keypress.js +35 -36
  90. package/dist-npm/types/readline.js +0 -1
  91. package/dist-npm/types/relinka.js +0 -1
  92. package/dist-npm/types/utils.js +0 -1
  93. package/dist-npm/utils/box.js +135 -137
  94. package/dist-npm/utils/color.js +65 -74
  95. package/dist-npm/utils/colorize.js +124 -156
  96. package/dist-npm/utils/component.js +532 -657
  97. package/dist-npm/utils/constants.js +64 -63
  98. package/dist-npm/utils/core.js +2 -3
  99. package/dist-npm/utils/decoder.js +244 -223
  100. package/dist-npm/utils/error.js +4 -9
  101. package/dist-npm/utils/errors.js +14 -4
  102. package/dist-npm/utils/format.js +19 -24
  103. package/dist-npm/utils/keypress.js +316 -414
  104. package/dist-npm/utils/log.js +11 -15
  105. package/dist-npm/utils/mapping.js +45 -52
  106. package/dist-npm/utils/messages.js +196 -183
  107. package/dist-npm/utils/platforms.js +16 -20
  108. package/dist-npm/utils/prompt-tmp.js +235 -286
  109. package/dist-npm/utils/prompt-two.js +235 -286
  110. package/dist-npm/utils/readline.js +5 -7
  111. package/dist-npm/utils/skeleton.js +130 -170
  112. package/dist-npm/utils/stream.js +2 -2
  113. package/dist-npm/utils/string.js +44 -58
  114. package/dist-npm/utils/terminal.js +23 -34
  115. package/dist-npm/utils/tree.js +30 -41
  116. package/dist-npm/utils/types.js +0 -1
  117. package/dist-npm/utils/utils.js +8 -8
  118. package/dist-npm/utils/variants.js +36 -44
  119. package/package.json +14 -14
  120. package/dist-npm/mod.js +0 -2
  121. /package/dist-npm/{mod.d.ts → main.d.ts} +0 -0
@@ -1,45 +1,46 @@
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 { deleteLastLine, deleteLastLines, countLines, removeCursor, restoreCursor } from "../../utils/terminal.js";
2
+ export { colorize } from "../../utils/colorize.js";
3
+ export { fmt, msg } from "../../utils/messages.js";
4
+ export { errorHandler } from "../../utils/errors.js";
5
+ export { colorMap } from "../../utils/mapping.js";
6
+ export { animateText } from "../../components/visual/animate/animate.js";
7
+ export { createAsciiArt } from "../../components/visual/ascii-art/ascii-art.js";
8
+ export { startPrompt } from "../../components/st-end/start.js";
9
+ export { anykeyPrompt } from "../../components/anykey/index.js";
10
+ export { textPrompt } from "../../components/input/text-main.js";
11
+ export { confirmPrompt } from "../../components/confirm/confirm-main.js";
12
+ export { togglePrompt } from "../../components/toggle/index.js";
13
+ export { datePrompt } from "../../components/date/date.js";
14
+ export { selectPrompt } from "../../components/select/select-main.js";
15
+ export { multiselectPrompt } from "../../components/multiselect/multiselect-main.js";
16
+ export { numSelectPrompt } from "../../components/select/num-select.js";
17
+ export { numMultiSelectPrompt } from "../../components/multiselect/num-multi-select.js";
18
+ export { nextStepsPrompt } from "../../components/next-steps/next-steps.js";
19
+ export { numberPrompt } from "../../components/number/number-main.js";
20
+ export { passwordPrompt } from "../../components/password/password-main.js";
21
+ export { endPrompt } from "../../components/st-end/end.js";
22
+ export { progressbar } from "../../components/progressbar/index.js";
23
+ export { promptsDisplayResults } from "../../components/results/results.js";
24
+ export { prompt } from "../../components/mono/mono.js";
25
+ export { default as block } from "../../components/block/block.js";
26
+ export { spinner } from "../../components/spinner/index.js";
27
+ export { default as checkbox, Separator } from "../../components/checkbox/index.js";
28
+ export { default as expand } from "../../components/expand/index.js";
29
+ export { default as rawlist } from "../../components/rawlist/index.js";
30
+ export { default as search } from "../../components/search/index.js";
31
+ export { default as select } from "../../components/select/index.js";
32
+ export { default as editor } from "../../components/editor/index.js";
33
+ export { default as confirm } from "../../components/confirm/index.js";
34
+ export { default as input } from "../../components/input/index.js";
35
+ export { default as number } from "../../components/number/index.js";
36
+ export { default as password } from "../../components/password/index.js";
37
+ export { default as ConfirmPrompt } from "../../components/confirm/confirm-three.js";
38
+ export { default as GroupMultiSelectPrompt } from "../../components/multiselect/group-multiselect.js";
39
+ export { default as MultiSelectPrompt } from "../../components/multiselect/multi-select.js";
40
+ export { default as PasswordPrompt } from "../../components/password/password-three.js";
41
+ export { default as Prompt, isCancel } from "../../components/prompts/prompt.js";
42
+ export { default as SelectPrompt } from "../../components/select/select-three.js";
43
+ export { default as SelectKeyPrompt } from "../../components/select/select-key.js";
44
+ export { default as TextPrompt } from "../../components/input/text.js";
45
+ export { multiselect } from "../../components/prompts/promptTwo.js";
46
+ export { relinka } from "../../components/prompts/create.js";
@@ -1,281 +1,232 @@
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
-
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";
7
6
  function diffLines(a, b) {
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
- }
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);
20
16
  }
21
-
22
- return diff;
17
+ }
18
+ return diff;
23
19
  }
24
-
25
- const cancel = Symbol('rp:cancel');
26
-
20
+ const cancel = Symbol("rp:cancel");
27
21
  export function isCancel(value) {
28
- return value === cancel;
22
+ return value === cancel;
29
23
  }
30
-
31
24
  function setRawMode(input, value) {
32
- if (input.isTTY) {
33
- input.setRawMode(value);
34
- }
25
+ if (input.isTTY) {
26
+ input.setRawMode(value);
27
+ }
35
28
  }
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',
29
+ const aliases = new Map([
30
+ ["k", "up"],
31
+ ["j", "down"],
32
+ ["h", "left"],
33
+ ["l", "right"]
51
34
  ]);
52
-
35
+ const keys = new Set(["up", "down", "left", "right", "space", "enter"]);
53
36
  export default class Prompt {
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 ?? '';
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);
74
86
  }
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
- });
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
+ }
120
125
  }
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);
126
+ for (const cb of cleanup) {
127
+ cb();
129
128
  }
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);
129
+ }
130
+ unsubscribe() {
131
+ this.subscribers.clear();
132
+ }
133
+ onKeypress(char, key) {
134
+ if (this.state === "error") {
135
+ this.state = "active";
138
136
  }
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
- }
137
+ if (key?.name && !this._track && aliases.has(key.name)) {
138
+ this.emit("cursor", aliases.get(key.name));
154
139
  }
155
-
156
- unsubscribe() {
157
- this.subscribers.clear();
140
+ if (key?.name && keys.has(key.name)) {
141
+ this.emit("cursor", key.name);
158
142
  }
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
- }
143
+ if (char && (char.toLowerCase() === "y" || char.toLowerCase() === "n")) {
144
+ this.emit("confirm", char.toLowerCase() === "y");
214
145
  }
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();
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
+ }
224
151
  }
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));
152
+ if (char) {
153
+ this.emit("key", char.toLowerCase());
231
154
  }
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
-
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
+ }
167
+ }
168
+ if (char === "") {
169
+ this.state = "cancel";
170
+ }
171
+ if (this.state === "submit" || this.state === "cancel") {
172
+ this.emit("finalize");
173
+ }
174
+ this.render();
175
+ if (this.state === "submit" || this.state === "cancel") {
176
+ this.close();
177
+ }
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;
199
+ }
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]);
279
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"));
221
+ 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";
280
229
  }
230
+ this._prevFrame = frame;
231
+ }
281
232
  }