-
Notifications
You must be signed in to change notification settings - Fork 630
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
feat(cli): command line spinner #3968
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5ad1730
feat(cli): spinner.ts"
darkdarcool c6c441a
Merge branch 'denoland:main' into spinner.ts
darkdarcool dff1a8a
feat(cli/spinner.ts): Added iuioiua's suggestions, and got rid of the…
darkdarcool e61c225
Merge branch 'spinner.ts' of https://github.com/darkdarcool/deno_std_…
darkdarcool 47e482e
Merge branch 'denoland:main' into spinner.ts
darkdarcool 70bc64c
feat(cli/spinner.ts): Added jsdoc to start and stop, and got rid of t…
darkdarcool f0943da
Merge branch 'main' into spinner.ts
iuioiua 0a02309
tweaks
iuioiua 5a05f3e
Merge branch 'denoland:main' into spinner.ts
darkdarcool 1b22a75
changed speed to interval, and added support for human readable colors
darkdarcool ab957cb
fmt
kt3k ffb621c
add comments
kt3k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,26 @@ const encoder = new TextEncoder(); | |
|
||
const LINE_CLEAR = encoder.encode("\r\u001b[K"); // From cli/prompt_secret.ts | ||
const COLOR_RESET = "\u001b[0m"; | ||
const DEFAULT_SPEED = 75; | ||
const DEFAULT_INTERVAL = 75; | ||
const DEFAULT_SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; | ||
|
||
// This is a hack to allow us to use the same type for both the color name and an ANSI escape code. | ||
// deno-lint-ignore ban-types | ||
type Ansi = string & { }; | ||
type Color = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'gray' | Ansi; | ||
|
||
const COLORS: Record<Color, string> = { | ||
black: "\u001b[30m", | ||
red: "\u001b[31m", | ||
green: "\u001b[32m", | ||
yellow: "\u001b[33m", | ||
blue: "\u001b[34m", | ||
magenta: "\u001b[35m", | ||
cyan: "\u001b[36m", | ||
white: "\u001b[37m", | ||
gray: "\u001b[90m", | ||
}; | ||
|
||
/** Options for {@linkcode Spinner}. */ | ||
export interface SpinnerOptions { | ||
/** | ||
|
@@ -17,13 +34,13 @@ export interface SpinnerOptions { | |
spinner?: string[]; | ||
/** The message to display next to the spinner. */ | ||
message?: string; | ||
/** The speed of the spinner. | ||
/** The time between each frame of the spinner. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be helpful to include the unit of time here (milliseconds) — otherwise, the user must review the implementation to understand. For example: - The time between each frame of the spinner.
+ The time (milliseconds) between each frame of the spinner. |
||
* | ||
* @default {75} | ||
*/ | ||
speed?: number; | ||
interval?: number; | ||
/** The color of the spinner. Defaults to the default terminal color. */ | ||
color?: string; | ||
color?: Color; | ||
} | ||
|
||
/** | ||
|
@@ -32,8 +49,8 @@ export interface SpinnerOptions { | |
export class Spinner { | ||
#spinner: string[]; | ||
#message: string; | ||
#speed: number; | ||
#color?: string; | ||
#interval: number; | ||
#color?: Color; | ||
#intervalId: number | undefined; | ||
#active = false; | ||
|
||
|
@@ -50,8 +67,8 @@ export class Spinner { | |
constructor(options?: SpinnerOptions) { | ||
this.#spinner = options?.spinner ?? DEFAULT_SPINNER; | ||
this.#message = options?.message ?? ""; | ||
this.#speed = options?.speed ?? DEFAULT_SPEED; | ||
this.#color = options?.color; | ||
this.#interval = options?.interval ?? DEFAULT_INTERVAL; | ||
this.#color = options?.color ? COLORS[options.color] : undefined; | ||
} | ||
|
||
/** | ||
|
@@ -80,7 +97,7 @@ export class Spinner { | |
Deno.stdout.writeSync(frame); | ||
i = (i + 1) % this.#spinner.length; | ||
}; | ||
this.#intervalId = setInterval(updateFrame, this.#speed); | ||
this.#intervalId = setInterval(updateFrame, this.#interval); | ||
} | ||
/** | ||
* Stops the spinner. | ||
|
@@ -102,8 +119,7 @@ export class Spinner { | |
if (this.#intervalId && this.#active) { | ||
clearInterval(this.#intervalId); | ||
Deno.stdout.writeSync(LINE_CLEAR); // Clear the current line | ||
Deno.removeSignalListener("SIGINT", this.stop.bind(this)); | ||
this.#active = false; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iuioiua I used this typescript hack to allow for type suggestions on colors and allowing for a ANSI string, is this ok?
See microsoft/TypeScript#29729 (comment)