deno.land / std@0.224.0 / cli / spinner.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// TODO(kt3k): Write test when pty is supported in Deno// See: https://github.com/denoland/deno/issues/3994
const encoder = new TextEncoder();
const LINE_CLEAR = encoder.encode("\r\u001b[K"); // From cli/prompt_secret.tsconst COLOR_RESET = "\u001b[0m";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. * * @see {@link https://github.com/microsoft/TypeScript/issues/29729#issuecomment-460346421} * * @internal */// deno-lint-ignore ban-typesexport type Ansi = string & {};
/** Color options for {@linkcode SpinnerOptions.color}. */export 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 { /** * The sequence of characters to be iterated through for animation. * * @default {["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]} */ spinner?: string[]; /** * 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. * * @default {75} */ interval?: number; /** * The color of the spinner. Defaults to the default terminal color. * This can be changed while the spinner is active. */ color?: Color;}
/** * A spinner that can be used to indicate that something is loading. */export class Spinner { #spinner: string[]; /** The message to display next to the spinner. */ message: string; #interval: number; #color?: Color; #intervalId: number | undefined; #active = false;
/** * Creates a new spinner. * * @example * ```ts * import { Spinner } from "https://deno.land/std@$STD_VERSION/cli/spinner.ts"; * * const spinner = new Spinner({ message: "Loading..." }); * ``` */ constructor( { spinner = DEFAULT_SPINNER, message = "", interval = DEFAULT_INTERVAL, color, }: SpinnerOptions = {}, ) { this.#spinner = spinner; this.message = message; this.#interval = interval; this.color = color; }
/** * Set the color of the spinner. This defaults to the default terminal color. * This can be changed while the spinner is active. */ set color(value: Color | undefined) { this.#color = value ? COLORS[value] : undefined; }
get color(): Color | undefined { return this.#color; }
/** * Starts the spinner. * * @example * ```ts * import { Spinner } from "https://deno.land/std@$STD_VERSION/cli/spinner.ts"; * * const spinner = new Spinner({ message: "Loading..." }); * spinner.start(); * ``` */ start() { if (this.#active || Deno.stdout.writable.locked) return; this.#active = true; let i = 0; // Updates the spinner after the given interval. const updateFrame = () => { const color = this.#color ?? ""; Deno.stdout.writeSync(LINE_CLEAR); const frame = encoder.encode( color + this.#spinner[i] + COLOR_RESET + " " + this.message, ); Deno.stdout.writeSync(frame); i = (i + 1) % this.#spinner.length; }; this.#intervalId = setInterval(updateFrame, this.#interval); } /** * Stops the spinner. * * @example * ```ts * import { Spinner } from "https://deno.land/std@$STD_VERSION/cli/spinner.ts"; * * const spinner = new Spinner({ message: "Loading..." }); * spinner.start(); * * setTimeout(() => { * spinner.stop(); * console.log("Finished loading!"); * }, 3000); * ``` */ stop() { if (this.#intervalId && this.#active) { clearInterval(this.#intervalId); Deno.stdout.writeSync(LINE_CLEAR); // Clear the current line this.#active = false; } }}
std

Version Info

Tagged at
3 weeks ago