@inquirer/core 3.0.0 → 3.1.0

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.
package/dist/cjs/index.js CHANGED
@@ -183,6 +183,9 @@ function createPrompt(view) {
183
183
  const screen = new screen_manager_mjs_1.default(sessionRl);
184
184
  let cancel = () => { };
185
185
  const answer = new type_1.CancelablePromise((resolve, reject) => {
186
+ const checkCursorPos = () => {
187
+ screen.checkCursorPos();
188
+ };
186
189
  const onExit = () => {
187
190
  try {
188
191
  let len = hooksCleanup.length;
@@ -200,8 +203,9 @@ function createPrompt(view) {
200
203
  screen.clearContent();
201
204
  }
202
205
  screen.done();
203
- resetHookState();
204
206
  process.removeListener('SIGINT', onForceExit);
207
+ sessionRl === null || sessionRl === void 0 ? void 0 : sessionRl.input.removeListener('keypress', checkCursorPos);
208
+ resetHookState();
205
209
  };
206
210
  cancel = () => {
207
211
  onExit();
@@ -243,7 +247,14 @@ function createPrompt(view) {
243
247
  }
244
248
  };
245
249
  // TODO: we should display a loader while we get the default options.
246
- (0, options_mjs_1.getPromptConfig)(config).then(workLoop, reject);
250
+ (0, options_mjs_1.getPromptConfig)(config).then((resolvedConfig) => {
251
+ workLoop(resolvedConfig);
252
+ // Re-renders only happen when the state change; but the readline cursor could change position
253
+ // and that also requires a re-render (and a manual one because we mute the streams).
254
+ // We set the listener after the initial workLoop to avoid a double render if render triggered
255
+ // by a state change sets the cursor to the right position.
256
+ sessionRl === null || sessionRl === void 0 ? void 0 : sessionRl.input.on('keypress', checkCursorPos);
257
+ }, reject);
247
258
  });
248
259
  answer.catch(() => {
249
260
  resetHookState();
@@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const cli_width_1 = __importDefault(require("cli-width"));
7
7
  const strip_ansi_1 = __importDefault(require("strip-ansi"));
8
- const string_width_1 = __importDefault(require("string-width"));
9
8
  const ansi_escapes_1 = __importDefault(require("ansi-escapes"));
10
9
  const utils_mjs_1 = require('./utils.js');
11
10
  const height = (content) => content.split('\n').length;
@@ -31,7 +30,14 @@ class ScreenManager {
31
30
  writable: true,
32
31
  value: 0
33
32
  });
33
+ Object.defineProperty(this, "cursorPos", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
34
39
  this.rl = rl;
40
+ this.cursorPos = rl.getCursorPos();
35
41
  }
36
42
  render(content, bottomContent = '') {
37
43
  this.clean();
@@ -50,7 +56,7 @@ class ScreenManager {
50
56
  }
51
57
  this.rl.setPrompt(prompt);
52
58
  // SetPrompt will change cursor position, now we can get correct value
53
- const cursorPos = this.rl._getCursorPos();
59
+ this.cursorPos = this.rl.getCursorPos();
54
60
  const width = (0, cli_width_1.default)({ defaultWidth: 80, output: this.rl.output });
55
61
  content = (0, utils_mjs_1.breakLines)(content, width);
56
62
  bottomContent = (0, utils_mjs_1.breakLines)(bottomContent, width);
@@ -66,17 +72,13 @@ class ScreenManager {
66
72
  */
67
73
  // We need to consider parts of the prompt under the cursor as part of the bottom
68
74
  // content in order to correctly cleanup and re-render.
69
- const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - cursorPos.rows;
75
+ const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - this.cursorPos.rows;
70
76
  const bottomContentHeight = promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);
71
77
  // Return cursor to the input position (on top of the bottomContent)
72
78
  if (bottomContentHeight > 0)
73
79
  output += ansi_escapes_1.default.cursorUp(bottomContentHeight);
74
- // Move cursor at the start of the line, then return to the initial left offset.
75
- const backward = (0, string_width_1.default)(lastLine(output));
76
- if (backward > 0)
77
- output += ansi_escapes_1.default.cursorBackward(backward);
78
- if (cursorPos.cols > 0)
79
- output += ansi_escapes_1.default.cursorForward(cursorPos.cols);
80
+ // Return cursor to the initial left offset.
81
+ output += ansi_escapes_1.default.cursorTo(this.cursorPos.cols);
80
82
  /**
81
83
  * Set up state for next re-rendering
82
84
  */
@@ -85,6 +87,15 @@ class ScreenManager {
85
87
  this.rl.output.write(output);
86
88
  this.rl.output.mute();
87
89
  }
90
+ checkCursorPos() {
91
+ const cursorPos = this.rl.getCursorPos();
92
+ if (cursorPos.cols !== this.cursorPos.cols) {
93
+ this.rl.output.unmute();
94
+ this.rl.output.write(ansi_escapes_1.default.cursorTo(cursorPos.cols));
95
+ this.rl.output.mute();
96
+ this.cursorPos = cursorPos;
97
+ }
98
+ }
88
99
  clean() {
89
100
  this.rl.output.unmute();
90
101
  this.rl.output.write([
@@ -3,8 +3,10 @@ export default class ScreenManager {
3
3
  private readonly rl;
4
4
  private height;
5
5
  private extraLinesUnderPrompt;
6
+ private cursorPos;
6
7
  constructor(rl: InquirerReadline);
7
8
  render(content: string, bottomContent?: string): void;
9
+ checkCursorPos(): void;
8
10
  clean(): void;
9
11
  clearContent(): void;
10
12
  done(): void;
@@ -156,6 +156,9 @@ export function createPrompt(view) {
156
156
  const screen = new ScreenManager(sessionRl);
157
157
  let cancel = () => { };
158
158
  const answer = new CancelablePromise((resolve, reject) => {
159
+ const checkCursorPos = () => {
160
+ screen.checkCursorPos();
161
+ };
159
162
  const onExit = () => {
160
163
  try {
161
164
  let len = hooksCleanup.length;
@@ -173,8 +176,9 @@ export function createPrompt(view) {
173
176
  screen.clearContent();
174
177
  }
175
178
  screen.done();
176
- resetHookState();
177
179
  process.removeListener('SIGINT', onForceExit);
180
+ sessionRl?.input.removeListener('keypress', checkCursorPos);
181
+ resetHookState();
178
182
  };
179
183
  cancel = () => {
180
184
  onExit();
@@ -216,7 +220,14 @@ export function createPrompt(view) {
216
220
  }
217
221
  };
218
222
  // TODO: we should display a loader while we get the default options.
219
- getPromptConfig(config).then(workLoop, reject);
223
+ getPromptConfig(config).then((resolvedConfig) => {
224
+ workLoop(resolvedConfig);
225
+ // Re-renders only happen when the state change; but the readline cursor could change position
226
+ // and that also requires a re-render (and a manual one because we mute the streams).
227
+ // We set the listener after the initial workLoop to avoid a double render if render triggered
228
+ // by a state change sets the cursor to the right position.
229
+ sessionRl?.input.on('keypress', checkCursorPos);
230
+ }, reject);
220
231
  });
221
232
  answer.catch(() => {
222
233
  resetHookState();
@@ -1,6 +1,5 @@
1
1
  import cliWidth from 'cli-width';
2
2
  import stripAnsi from 'strip-ansi';
3
- import stringWidth from 'string-width';
4
3
  import ansiEscapes from 'ansi-escapes';
5
4
  import { breakLines } from './utils.mjs';
6
5
  const height = (content) => content.split('\n').length;
@@ -10,9 +9,11 @@ export default class ScreenManager {
10
9
  // These variables are keeping information to allow correct prompt re-rendering
11
10
  height = 0;
12
11
  extraLinesUnderPrompt = 0;
12
+ cursorPos;
13
13
  constructor(rl) {
14
14
  this.rl = rl;
15
15
  this.rl = rl;
16
+ this.cursorPos = rl.getCursorPos();
16
17
  }
17
18
  render(content, bottomContent = '') {
18
19
  this.clean();
@@ -31,7 +32,7 @@ export default class ScreenManager {
31
32
  }
32
33
  this.rl.setPrompt(prompt);
33
34
  // SetPrompt will change cursor position, now we can get correct value
34
- const cursorPos = this.rl._getCursorPos();
35
+ this.cursorPos = this.rl.getCursorPos();
35
36
  const width = cliWidth({ defaultWidth: 80, output: this.rl.output });
36
37
  content = breakLines(content, width);
37
38
  bottomContent = breakLines(bottomContent, width);
@@ -47,17 +48,13 @@ export default class ScreenManager {
47
48
  */
48
49
  // We need to consider parts of the prompt under the cursor as part of the bottom
49
50
  // content in order to correctly cleanup and re-render.
50
- const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - cursorPos.rows;
51
+ const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - this.cursorPos.rows;
51
52
  const bottomContentHeight = promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);
52
53
  // Return cursor to the input position (on top of the bottomContent)
53
54
  if (bottomContentHeight > 0)
54
55
  output += ansiEscapes.cursorUp(bottomContentHeight);
55
- // Move cursor at the start of the line, then return to the initial left offset.
56
- const backward = stringWidth(lastLine(output));
57
- if (backward > 0)
58
- output += ansiEscapes.cursorBackward(backward);
59
- if (cursorPos.cols > 0)
60
- output += ansiEscapes.cursorForward(cursorPos.cols);
56
+ // Return cursor to the initial left offset.
57
+ output += ansiEscapes.cursorTo(this.cursorPos.cols);
61
58
  /**
62
59
  * Set up state for next re-rendering
63
60
  */
@@ -66,6 +63,15 @@ export default class ScreenManager {
66
63
  this.rl.output.write(output);
67
64
  this.rl.output.mute();
68
65
  }
66
+ checkCursorPos() {
67
+ const cursorPos = this.rl.getCursorPos();
68
+ if (cursorPos.cols !== this.cursorPos.cols) {
69
+ this.rl.output.unmute();
70
+ this.rl.output.write(ansiEscapes.cursorTo(cursorPos.cols));
71
+ this.rl.output.mute();
72
+ this.cursorPos = cursorPos;
73
+ }
74
+ }
69
75
  clean() {
70
76
  this.rl.output.unmute();
71
77
  this.rl.output.write([
@@ -3,8 +3,10 @@ export default class ScreenManager {
3
3
  private readonly rl;
4
4
  private height;
5
5
  private extraLinesUnderPrompt;
6
+ private cursorPos;
6
7
  constructor(rl: InquirerReadline);
7
8
  render(content: string, bottomContent?: string): void;
9
+ checkCursorPos(): void;
8
10
  clean(): void;
9
11
  clearContent(): void;
10
12
  done(): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "engines": {
5
5
  "node": ">=14.18.0"
6
6
  },
@@ -68,7 +68,6 @@
68
68
  "figures": "^3.2.0",
69
69
  "mute-stream": "^1.0.0",
70
70
  "run-async": "^3.0.0",
71
- "string-width": "^4.2.3",
72
71
  "strip-ansi": "^6.0.1",
73
72
  "wrap-ansi": "^6.0.1"
74
73
  },
@@ -97,5 +96,5 @@
97
96
  }
98
97
  }
99
98
  },
100
- "gitHead": "18144628b03b62c346cb7d9d55a2c526b1ed5d8a"
99
+ "gitHead": "ab9dd2f301e02d2cc0fec699ce100b174f2b798e"
101
100
  }