UNPKG

10.2 kBTypeScriptView Raw
1// Type definitions for cli-progress 3.11
2// Project: https://github.com/AndiDittrich/Node.CLI-Progress
3// Definitions by: Mohamed Hegazy <https://github.com/mhegazy>
4// Álvaro Martínez <https://github.com/alvaromartmart>
5// Piotr Błażejewicz <https://github.com/peterblazejewicz>
6// Marko Schilde <https://github.com/mschilde>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8/// <reference types="node" />
9
10import EventEmitter = require("events");
11
12export interface Params {
13 progress: number;
14 eta: number;
15 startTime: number;
16 total: number;
17 value: number;
18 maxWidth: number;
19}
20
21export interface Options {
22 /**
23 * progress bar output format.
24 * The progressbar can be customized by using the following build-in placeholders. They can be combined in any order.
25 * {bar} - the progress bar, customizable by the options barsize, barCompleteString and barIncompleteString
26 * {percentage} - the current progress in percent (0-100)
27 * {total} - the end value
28 * {value} - the current value set by last update() call
29 * {eta} - expected time of accomplishment in seconds
30 * {duration} - elapsed time in seconds
31 * {eta_formatted} - expected time of accomplishment formatted into appropriate units
32 * {duration_formatted} - elapsed time formatted into appropriate units
33 *
34 * Example:
35 * progress [{bar}] {percentage}% | ETA: {eta}s | {value}/{total}
36 * is rendered as
37 * progress [========================================] 100% | ETA: 0s | 200/200
38 */
39 format?: string | GenericFormatter | undefined;
40
41 /** a custom bar formatter function which renders the bar-element (default: format-bar.js) */
42 formatBar?: BarFormatter | undefined;
43
44 /** a custom timer formatter function which renders the formatted time elements like eta_formatted and duration-formatted (default: format-time.js) */
45 formatTime?: TimeFormatter | undefined;
46
47 /** a custom value formatter function which renders all other values (default: format-value.js) */
48 formatValue?: ValueFormatter | undefined;
49
50 /** the maximum update rate (default: 10) */
51 fps?: number | undefined;
52
53 /** output stream to use (default: process.stderr) */
54 stream?: NodeJS.WritableStream | undefined;
55
56 /** automatically call stop() when the value reaches the total (default: false) */
57 stopOnComplete?: boolean | undefined;
58
59 /** clear the progress bar on complete / stop() call (default: false) */
60 clearOnComplete?: boolean | undefined;
61
62 /** the length of the progress bar in chars (default: 40) */
63 barsize?: number | undefined;
64
65 /** position of the progress bar - 'left' (default), 'right' or 'center */
66 align?: 'left' | 'right' | 'center' | undefined;
67
68 /** character to use as "complete" indicator in the bar (default: "=") */
69 barCompleteString?: string | undefined;
70
71 /** character to use as "incomplete" indicator in the bar (default: "-") */
72 barIncompleteString?: string | undefined;
73
74 /** character to use as "complete" indicator in the bar (default: "=") */
75 barCompleteChar?: string | undefined;
76
77 /** character to use as "incomplete" indicator in the bar (default: "-") */
78 barIncompleteChar?: string | undefined;
79
80 /**
81 * hide the cursor during progress operation; restored on complete (default: false)
82 * - pass `null` to keep terminal settings
83 */
84 hideCursor?: boolean | null | undefined;
85
86 /** glue sequence (control chars) between bar elements (default: '') */
87 barGlue?: string | undefined;
88
89 /** number of updates with which to calculate the eta; higher numbers give a more stable eta (default: 10) */
90 etaBuffer?: number | undefined;
91
92 /**
93 * trigger an eta calculation update during asynchronous rendering trigger using the current value
94 * - should only be used for long running processes in conjunction with lof `fps` values and large `etaBuffer`
95 * @default false
96 */
97 etaAsynchronousUpdate?: boolean | undefined;
98
99 /** progress calculation relative to start value ? default start at 0 (default: false) */
100 progressCalculationRelative?: boolean | undefined;
101
102 /** disable line wrapping (default: false) - pass null to keep terminal settings; pass true to trim the output to terminal width */
103 linewrap?: boolean | null | undefined;
104
105 /** trigger redraw during update() in case threshold time x2 is exceeded (default: true) - limited to single bar usage */
106 synchronousUpdate?: boolean | undefined;
107
108 /** enable scheduled output to notty streams - e.g. redirect to files (default: false) */
109 noTTYOutput?: boolean | undefined;
110
111 /** set the output schedule/interval for notty output in ms (default: 2000ms) */
112 notTTYSchedule?: number | undefined;
113
114 /** display progress bars with 'total' of zero(0) as empty, not full (default: false) */
115 emptyOnZero?: boolean | undefined;
116
117 /** trigger redraw on every frame even if progress remains the same; can be useful if progress bar gets overwritten by other concurrent writes to the terminal (default: false) */
118 forceRedraw?: boolean | undefined;
119
120 /** add padding chars to formatted time and percentage to force fixed width (default: false) */
121 autopadding?: boolean | undefined;
122
123 /** the character sequence used for autopadding (default: " ") */
124 autopaddingChar?: string | undefined;
125
126 /** stop bar on SIGINT/SIGTERM to restore cursor settings (default: true) */
127 gracefulExit?: boolean | undefined;
128}
129
130export interface Preset {
131 barCompleteChar: string;
132 barIncompleteChar: string;
133
134 /**
135 * Example: 'progress [{bar}] {percentage}% | ETA: {eta}s | {value}/{total}'
136 *
137 * {bar} - the progress bar, customizable by the options barsize, barCompleteString and barIncompleteString
138 *
139 * {percentage} - the current progress in percent (0-100)
140 *
141 * {total} - the end value
142 *
143 * {value} - the current value set by last update() call
144 *
145 * {eta} - expected time of accomplishment in seconds (limited to 115days, otherwise INF is displayed)
146 *
147 * {duration} - elapsed time in seconds
148 *
149 * {eta_formatted} - expected time of accomplishment formatted into appropriate units
150 *
151 * {duration_formatted} - elapsed time formatted into appropriate units
152 *
153 */
154 format: string;
155}
156
157export class GenericBar extends EventEmitter {
158 /** Initialize a new Progress bar. An instance can be used multiple times! it's not required to re-create it! */
159 constructor(opt: Options, preset?: Preset);
160
161 /** Internal render function */
162 render(forceRendering?: boolean): void;
163
164 /** Starts the progress bar and set the total and initial value */
165 start(total: number, startValue: number, payload?: object): void;
166
167 /** Stops the progress bar and go to next line */
168 stop(): void;
169
170 /** Sets the current progress value and optionally the payload with values of custom tokens as a second parameter */
171 update(current: number, payload?: object): void;
172 update(payload: object): void;
173
174 /** Calculate the actual progress value */
175 getProgress(): number;
176
177 /** Increases the current progress value by a specified amount (default +1). Update payload optionally */
178 increment(step?: number, payload?: object): void;
179 increment(payload: object): void;
180
181 /** Get the total (limit) value */
182 getTotal(): number;
183
184 /** Sets the total progress value while progressbar is active. Especially useful handling dynamic tasks. */
185 setTotal(total: number): void;
186
187 /** Force eta calculation update (long running processes) without altering the progress values. */
188 updateETA(): void;
189}
190
191export class SingleBar extends GenericBar {
192 /** Initialize a new Progress bar. An instance can be used multiple times! it's not required to re-create it! */
193 constructor(opt: Options, preset?: Preset);
194
195 /** Internal render function */
196 render(): void;
197
198 /** Sets the current progress value and optionally the payload with values of custom tokens as a second parameter */
199 update(current: number, payload?: object): void;
200 update(payload: object): void;
201
202 /** Starts the progress bar and set the total and initial value */
203 start(total: number, startValue: number, payload?: object): void;
204
205 /** Stops the progress bar and go to next line */
206 stop(): void;
207}
208
209export class MultiBar extends EventEmitter {
210 constructor(opt: Options, preset?: Preset);
211
212 /** add a new bar to the stack */
213 create(total: number, startValue: number, payload?: any, barOptions?: Options): SingleBar;
214
215 /** remove a bar from the stack */
216 remove(bar: SingleBar): boolean;
217
218 /** internal update routine */
219 update(): void;
220
221 stop(): void;
222
223 /** log output above the progress bars; string must end with newline character! */
224 log(data: string): void;
225}
226
227export const Presets: {
228 /** Styles as of cli-progress v1.3.0 */
229 legacy: Preset;
230
231 /** Unicode Rectangles */
232 rect: Preset;
233
234 /** Unicode background shades are used for the bar */
235 shades_classic: Preset;
236
237 /** Unicode background shades with grey bar */
238 shades_grey: Preset;
239};
240
241export interface GenericFormatter {
242 (options: Options, params: Params, payload: any): string;
243}
244
245export interface TimeFormatter {
246 (t: number, options: Options, roundToMultipleOf: number): string;
247}
248
249export interface ValueFormatter {
250 (v: number, options: Options, type: ValueType): string;
251}
252
253export interface BarFormatter {
254 (progress: number, options: Options): string;
255}
256
257export type ValueType = 'percentage' | 'total' | 'value' | 'eta' | 'duration';
258
259declare const defaultFormatter: GenericFormatter;
260declare const formatBar: BarFormatter;
261declare const formatValue: ValueFormatter;
262declare const formatTime: TimeFormatter;
263
264export const Format: {
265 Formatter: typeof defaultFormatter;
266 BarFormat: typeof formatBar;
267 ValueFormat: typeof formatValue;
268 TimeFormat: typeof formatTime;
269};
270
271export class Bar extends SingleBar {}
272
273export {};