Skip to content

Commit

Permalink
fix: handle line duplication bug with proper line wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Mar 5, 2023
1 parent 68dc69f commit 58a1df1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/good-islands-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clack/core': patch
'@clack/prompts': patch
---

Fix line duplication bug by automatically wrapping prompts to `process.stdout.columns`
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@
"dependencies": {
"picocolors": "^1.0.0",
"sisteransi": "^1.0.5"
},
"devDependencies": {
"wrap-ansi": "^8.1.0"
}
}
8 changes: 6 additions & 2 deletions packages/core/src/prompts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import readline from 'node:readline';
import { Readable, Writable } from 'node:stream';
import { WriteStream } from 'node:tty';
import { cursor, erase } from 'sisteransi';
import wrap from 'wrap-ansi';

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

this.input.on('keypress', this.onKeypress);
setRawMode(this.input, true);
this.output.on('resize', this.render);

this.render();

return new Promise<string | symbol>((resolve, reject) => {
this.once('submit', () => {
this.output.write(cursor.show);
this.output.off('resize', this.render);
setRawMode(this.input, false);
resolve(this.value);
});
this.once('cancel', () => {
this.output.write(cursor.show);
this.output.off('resize', this.render);
setRawMode(this.input, false);
resolve(cancel);
});
Expand Down Expand Up @@ -203,13 +207,13 @@ export default class Prompt {

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

private _prevFrame = '';
private render() {
const frame = this._render(this) ?? '';
const frame = wrap(this._render(this) ?? '', process.stdout.columns);
if (frame === this._prevFrame) return;

if (this.state === 'initial') {
Expand Down
46 changes: 46 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58a1df1

Please sign in to comment.