Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of the .clear() method #182

Merged
merged 3 commits into from Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions index.js
Expand Up @@ -142,6 +142,7 @@ class Ora {
}

this._indent = indent;
this.updateLineCount();
}

_updateInterval(interval) {
Expand Down Expand Up @@ -215,7 +216,7 @@ class Ora {
const columns = this.stream.columns || 80;
const fullPrefixText = this.getFullPrefixText(this.prefixText, '-');
this.lineCount = 0;
for (const line of stripAnsi(fullPrefixText + '--' + this[TEXT]).split('\n')) {
for (const line of stripAnsi(' '.repeat(this.indent) + fullPrefixText + '--' + this[TEXT]).split('\n')) {
this.lineCount += Math.max(1, Math.ceil(wcwidth(line) / columns));
}
}
Expand Down Expand Up @@ -264,15 +265,22 @@ class Ora {
return this;
}

this.stream.cursorTo(0);

for (let i = 0; i < this.linesToClear; i++) {
if (i > 0) {
this.stream.moveCursor(0, -1);
}

this.stream.clearLine();
this.stream.clearLine(1);
}

if (this.indent || this.lastIndent !== this.indent) {
this.stream.cursorTo(this.indent);
}

this.lastIndent = this.indent;

this.linesToClear = 0;

return this;
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -51,6 +51,7 @@
"@types/node": "^14.14.35",
"ava": "^2.4.0",
"get-stream": "^6.0.0",
"transform-tty": "^1.0.11",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
Expand Down