Skip to content

Commit

Permalink
feat(cli): make Spinner.message able to be changed on-the-fly (#4079)
Browse files Browse the repository at this point in the history
* initial commit

* update

* update

* don't start interval on interval change if not already running

* revert changes

* tweak

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
  • Loading branch information
timreichen and iuioiua authored Jan 4, 2024
1 parent 023096b commit 9c8b9b5
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cli/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export interface SpinnerOptions {
* @default {["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]}
*/
spinner?: string[];
/** The message to display next to the spinner. */
/**
* The message to display next to the spinner. This can be changed while the
* spinner is active.
*/
message?: string;
/**
* The time between each frame of the spinner in milliseconds.
Expand All @@ -63,7 +66,7 @@ export interface SpinnerOptions {
*/
export class Spinner {
#spinner: string[];
#message: string;
message: string;
#interval: number;
#color?: Color;
#intervalId: number | undefined;
Expand All @@ -81,7 +84,7 @@ export class Spinner {
*/
constructor(options?: SpinnerOptions) {
this.#spinner = options?.spinner ?? DEFAULT_SPINNER;
this.#message = options?.message ?? "";
this.message = options?.message ?? "";
this.#interval = options?.interval ?? DEFAULT_INTERVAL;
this.#color = options?.color ? COLORS[options.color] : undefined;
}
Expand All @@ -107,7 +110,7 @@ export class Spinner {
const updateFrame = () => {
Deno.stdout.writeSync(LINE_CLEAR);
const frame = encoder.encode(
color + this.#spinner[i] + COLOR_RESET + " " + this.#message,
color + this.#spinner[i] + COLOR_RESET + " " + this.message,
);
Deno.stdout.writeSync(frame);
i = (i + 1) % this.#spinner.length;
Expand Down

0 comments on commit 9c8b9b5

Please sign in to comment.