Skip to content

Commit

Permalink
Improve performance of the .clear() method (#182)
Browse files Browse the repository at this point in the history
Co-authored-by: moofoo <nathancookdev@gmail.com>
  • Loading branch information
G-Rath and moofoo committed Aug 23, 2021
1 parent 476935f commit d51c971
Show file tree
Hide file tree
Showing 3 changed files with 579 additions and 2 deletions.
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

0 comments on commit d51c971

Please sign in to comment.