symfony-style-console
Version:
Use the style and utilities of the Symfony Console in Node.js
142 lines (141 loc) • 3.9 kB
JavaScript
import { EOL } from '../env';
import ConsoleOutput from '../Output/ConsoleOutput';
import { OUTPUT_NORMAL } from '../Output/OutputInterface';
import ProgressBar from '../Helper/ProgressBar';
/**
* Decorates output to add console style guide helpers.
*
* @author Kevin Bond <kevinbond@gmail.com>
*
* Original PHP class
*
* @author Florian Reuschel <florian@loilo.de>
*
* Port to TypeScript
*/
class OutputStyle {
/**
* Create an OutputStyle instance.
*
* @param output The output to style
*/
constructor(output = new ConsoleOutput()) {
this.output = output;
}
/**
* Add newline(s).
*
* @param number count The number of newlines
*/
newLine(count = 1) {
this.output.write(EOL.repeat(count));
}
/**
* @param number max
*
* @return ProgressBar
*/
createProgressBar(max = 0) {
return new ProgressBar(this.output, max);
}
/**
* Writes a message to the output.
*
* @param messages The message as an array of lines or a single string
* @param newline Whether to add a newline
* @param type A bitmask of options (one of the OUTPUT or VERBOSITY constants),
* 0 is considered the same as OUTPUT_NORMAL | VERBOSITY_NORMAL
*/
write(messages, newline = false, type = OUTPUT_NORMAL) {
this.output.write(messages, newline, type);
}
/**
* Writes a message to the output and adds a newline at the end.
*
* @param messages The message as an array of lines of a single string
* @param type A bitmask of options (one of the OUTPUT or VERBOSITY constants),
* 0 is considered the same as OUTPUT_NORMAL | VERBOSITY_NORMAL
*/
writeln(messages, type = OUTPUT_NORMAL) {
this.output.write(messages, true, type);
}
/**
* Sets the verbosity of the output.
*
* @param level The level of verbosity (one of the [[VERBOSITY_LEVEL]] constants)
*/
setVerbosity(level) {
this.output.setVerbosity(level);
}
/**
* Gets the current verbosity of the output.
*
* @return number The current level of verbosity (one of the [[VERBOSITY_LEVEL]] constants)
*/
getVerbosity() {
return this.output.getVerbosity();
}
/**
* Sets the decorated flag.
*
* @param decorated Whether to decorate the messages
*/
setDecorated(decorated) {
this.output.setDecorated(decorated);
}
/**
* Gets the decorated flag.
*
* @return True if the output will decorate messages, false otherwise
*/
isDecorated() {
return this.output.isDecorated();
}
/**
* Sets output formatter.
*
* @param The formatter to use
*/
setFormatter(formatter) {
this.output.setFormatter(formatter);
}
/**
* Returns current output formatter instance.
*/
getFormatter() {
return this.output.getFormatter();
}
/**
* Returns whether verbosity is quiet (-q).
*
* @return True if verbosity is set to [[VERBOSITY_QUIET]], false otherwise
*/
isQuiet() {
return this.output.isQuiet();
}
/**
* Returns whether verbosity is verbose (-v).
*
* @return True if verbosity is set to [[VERBOSITY_VERBOSE]], false otherwise
*/
isVerbose() {
return this.output.isVerbose();
}
/**
* Returns whether verbosity is very verbose (-vv).
*
* @return True if verbosity is set to [[VERBOSITY_VERY_VERBOSE]], false otherwise
*/
isVeryVerbose() {
return this.output.isVeryVerbose();
}
/**
* Returns whether verbosity is debug (-vvv).
*
* @return True if verbosity is set to [[VERBOSITY_DEBUG]], false otherwise
*/
isDebug() {
return this.output.isDebug();
}
}
export default OutputStyle;