UNPKG

symfony-style-console

Version:

Use the style and utilities of the Symfony Console in Node.js

309 lines (308 loc) 8.04 kB
import OutputInterface from '../Output/OutputInterface'; /** * A callback function mapping a [[ProgressBar]] to a string and writing it to an [[OutputInterface]]. */ export interface FormatterFn { (bar: ProgressBar, output?: OutputInterface): string; } /** * Represents an object literal mapping formatter names to their respective callbacks */ export interface FormatterHash { [formatter: string]: FormatterFn; } /** * The ProgressBar provides helpers to display progress output. * * @author Fabien Potencier <fabien@symfony.com> * * Original PHP class * * @author Chris Jones <leeked@gmail.com> * * Original PHP class * * @author Florian Reuschel <florian@loilo.de> * * Port to TypeScript * */ export default class ProgressBar { /** * The available formatters. */ private static formatters; /** * The available format placeholders. */ private static formats; /** * The width of the progress bar. */ private barWidth; /** * The character that represents completed progress. */ private barChar; /** * The character that represents uncompleted progress. */ private emptyBarChar; /** * The character that represents the outer pointer of the completed progress. */ private progressChar; /** * The template of the progress bar. */ private format; /** * The originally set template of the progress bar. */ private internalFormat; /** * The frequency of redrawing the bar (in steps). */ private redrawFreq; /** * The output to write the progress bar to. */ private output; /** * The current step of the progress. */ private step; /** * The maximum number of steps of the progress. */ private max; /** * The UNIX timestamp when the progress started. */ private startTime; /** * The width of each single step. */ private stepWidth; /** * The current progress in percent. */ private percent; /** * The number of lines the progress bar template contains. */ private formatLineCount; /** * A Hash containing mapping format template placeholders to custom messages. */ private messages; /** * If the progress should be rewritten to the same position. */ private shouldOverwrite; /** * Indicator for the `overwrite` method if it's the first render cycle. */ private firstRun; /** * Creates a new progress bar. * * @param OutputInterface output An OutputInterface instance * @param int max Maximum steps (0 if unknown) */ constructor(output: OutputInterface, max?: number); /** * Sets a format for a given name. * * This method also allow you to override an existing format. * * @param name The format name * @param format A format string */ static setFormatDefinition(name: string, format: string): void; /** * Gets the format for a given name. * * @param name The format name * @return A format string */ static getFormatDefinition(name: string): string; /** * Sets a placeholder formatter for a given name. * * This method also allow you to override an existing placeholder. * * @param name The placeholder name (including the delimiter char like %) * @param callback A formatter callback */ static setPlaceholderFormatterDefinition(name: string, callable: FormatterFn): void; /** * Gets the placeholder formatter for a given name. * * @param name The placeholder name (including the delimiter char like %) * @return A formatter callback */ static getPlaceholderFormatterDefinition(name: string): FormatterFn; /** * Gets the initially available format templates. */ private static initFormats; /** * Gets the initially available format placeholder callbacks. */ private static initPlaceholderFormatters; /** * Associates a text with a named placeholder. * * The text is displayed when the progress bar is rendered but only * when the corresponding placeholder is part of the custom format line * (by wrapping the name with %). * * @param message The text to associate with the placeholder * @param name The name of the placeholder */ setMessage(message: string, name?: string): void; /** * Gets the message associated with a certain placeholder. * * @param name A format placeholder */ getMessage(name?: string): string; /** * Gets the progress bar start time. */ getStartTime(): number; /** * Gets the progress bar maximal steps. */ getMaxSteps(): number; /** * Gets the current step position. */ getProgress(): number; /** * Gets the progress bar step width. */ getStepWidth(): number; /** * Gets the current progress bar percent. */ getProgressPercent(): number; /** * Sets the progress bar width. */ setBarWidth(size: number): void; /** * Gets the progress bar width in characters. */ getBarWidth(): number; /** * Sets the bar character. * * @param char A character */ setBarCharacter(char: string): void; /** * Gets the bar character. */ getBarCharacter(): string; /** * Sets the empty bar character. * * @param char A character */ setEmptyBarCharacter(char: string): void; /** * Gets the empty bar character. */ getEmptyBarCharacter(): string; /** * Sets the progress bar character. * * @param char A character */ setProgressCharacter(char: string): void; /** * Gets the progress bar character. */ getProgressCharacter(): string; /** * Sets the progress bar format. * * @param format The format */ setFormat(format: string): void; /** * Sets the redraw frequency. * * @param freq The frequency in steps */ setRedrawFrequency(freq: number): void; /** * Starts the progress output. * * @param max Number of steps to complete the bar (`0` if indeterminate), `null` to leave unchanged */ start(max?: number): void; /** * Advances the progress output X steps. * * @param step Number of steps to advance */ advance(step?: number): void; /** * Sets whether to overwrite the progress bar, `false` for new line. * * @param overwrite Whether the progress bar should be overwritten */ setOverwrite(overwrite: boolean): void; /** * Sets the current progress. * * @param step The current progress */ setProgress(step: number): void; /** * Finishes the progress output. */ finish(): void; /** * Outputs the current progress string. */ display(): void; /** * Removes the progress bar from the current line. * * This is useful if you wish to write some output while a progress bar is running. * Call display() to show the progress bar again. */ clear(): void; /** * Sets the progress bar format template. * * @param format The format template */ private setRealFormat; /** * Sets the progress bar maximal steps. * * @param max The progress bar max steps */ private setMaxSteps; /** * Overwrites a previous message to the output. * * @param message The message */ private overwrite; /** * Determines the fitting format template for the currently set verbosity. */ private determineBestFormat; /** * Renders the current state of the progress bar. * * @return The output of the progress bar's current state */ private buildLine; }