Skip to content

Commit d611766

Browse files
authored
Fix line duplication bug with proper line wrapping (#97)
2 parents 68dc69f + 58a1df1 commit d611766

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

.changeset/good-islands-provide.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clack/core': patch
3+
'@clack/prompts': patch
4+
---
5+
6+
Fix line duplication bug by automatically wrapping prompts to `process.stdout.columns`

packages/core/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,8 @@
5555
"dependencies": {
5656
"picocolors": "^1.0.0",
5757
"sisteransi": "^1.0.5"
58+
},
59+
"devDependencies": {
60+
"wrap-ansi": "^8.1.0"
5861
}
5962
}

packages/core/src/prompts/prompt.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import readline from 'node:readline';
55
import { Readable, Writable } from 'node:stream';
66
import { WriteStream } from 'node:tty';
77
import { cursor, erase } from 'sisteransi';
8+
import wrap from 'wrap-ansi';
89

910
function diffLines(a: string, b: string) {
1011
if (a === b) return;
@@ -104,17 +105,20 @@ export default class Prompt {
104105

105106
this.input.on('keypress', this.onKeypress);
106107
setRawMode(this.input, true);
108+
this.output.on('resize', this.render);
107109

108110
this.render();
109111

110112
return new Promise<string | symbol>((resolve, reject) => {
111113
this.once('submit', () => {
112114
this.output.write(cursor.show);
115+
this.output.off('resize', this.render);
113116
setRawMode(this.input, false);
114117
resolve(this.value);
115118
});
116119
this.once('cancel', () => {
117120
this.output.write(cursor.show);
121+
this.output.off('resize', this.render);
118122
setRawMode(this.input, false);
119123
resolve(cancel);
120124
});
@@ -203,13 +207,13 @@ export default class Prompt {
203207

204208
// TODO: handle wrapping
205209
private restoreCursor() {
206-
const lines = this._prevFrame.split('\n').length - 1;
210+
const lines = wrap(this._prevFrame, process.stdout.columns).split('\n').length - 1;
207211
this.output.write(cursor.move(-999, lines * -1));
208212
}
209213

210214
private _prevFrame = '';
211215
private render() {
212-
const frame = this._render(this) ?? '';
216+
const frame = wrap(this._render(this) ?? '', process.stdout.columns);
213217
if (frame === this._prevFrame) return;
214218

215219
if (this.state === 'initial') {

pnpm-lock.yaml

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)