-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd12899
commit 178474f
Showing
4 changed files
with
92 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export class Progress { | ||
constructor(items, stream = process.stdout) { | ||
this.items = [...items]; | ||
this.stream = stream; | ||
this.interval = null; | ||
this._update = null; | ||
} | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
line(item) { | ||
throw new Error('Must override in subclass'); | ||
} | ||
|
||
start(interval) { | ||
if (this.interval) { | ||
throw new Error('Already started'); | ||
} | ||
|
||
const {items, stream} = this; | ||
const {isTTY} = stream; | ||
|
||
let clear = ''; | ||
const update = (this._update = () => { | ||
let output = clear; | ||
for (const item of items) { | ||
output += `${this.line(item)}\n`; | ||
} | ||
stream.write(output); | ||
}); | ||
|
||
update(); | ||
|
||
clear = isTTY ? '\x1B[F\x1B[2K'.repeat(items.length) : ''; | ||
this.interval = setInterval(update, interval); | ||
} | ||
|
||
stop() { | ||
const {interval} = this; | ||
if (!interval) { | ||
throw new Error('Not started'); | ||
} | ||
|
||
clearInterval(interval); | ||
this.interval = null; | ||
this._update = null; | ||
} | ||
|
||
end() { | ||
const {_update: update} = this; | ||
this.stop(); | ||
update(); | ||
} | ||
} |