UNPKG

symfony-style-console

Version:

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

165 lines (164 loc) 5.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var OutputFormatter_1 = require("../Formatter/OutputFormatter"); var OutputInterface_1 = require("./OutputInterface"); var Helper_1 = require("../Helper/Helper"); /** * Base class for output classes. * * There are five levels of verbosity: * * * normal: no option passed (normal output) * * verbose: `-v` (more output) * * very verbose: `-vv` (highly extended output) * * debug: `-vvv` (all debug output) * * quiet: `-q` (no output) * * @author Fabien Potencier <fabien@symfony.com> * * Original PHP class * * @author Florian Reuschel <florian@loilo.de> * * Port to TypeScript */ var Output = /** @class */ (function () { function Output(verbosity, decorated, formatter) { if (verbosity === void 0) { verbosity = OutputInterface_1.VERBOSITY_NORMAL; } if (decorated === void 0) { decorated = false; } if (formatter === void 0) { formatter = null; } this.verbosity = verbosity; this.formatter = formatter || new OutputFormatter_1.default(); this.formatter.setDecorated(decorated); } /** * Sets the decorated flag. * * @param decorated Whether to decorate the messages */ Output.prototype.setDecorated = function (decorated) { this.formatter.setDecorated(decorated); }; /** * Gets the decorated flag. * * @return boolean true if the output will decorate messages, false otherwise */ Output.prototype.isDecorated = function () { return this.formatter.isDecorated(); }; /** * Sets output formatter. * * @param formatter */ Output.prototype.setFormatter = function (formatter) { this.formatter = formatter; }; /** * Returns current output formatter instance. */ Output.prototype.getFormatter = function () { return this.formatter; }; /** * Sets the verbosity of the output. * * @param number level The level of verbosity (one of the VERBOSITY constants) */ Output.prototype.setVerbosity = function (level) { this.verbosity = level; }; /** * Gets the current verbosity of the output. * * @return number The current level of verbosity (one of the VERBOSITY constants) */ Output.prototype.getVerbosity = function () { return this.verbosity; }; /** * Returns whether verbosity is quiet (-q). * * @return bool true if verbosity is set to VERBOSITY_QUIET, false otherwise */ Output.prototype.isQuiet = function () { return OutputInterface_1.VERBOSITY_QUIET === this.verbosity; }; /** * Returns whether verbosity is verbose (-v). * * @return bool true if verbosity is set to VERBOSITY_VERBOSE, false otherwise */ Output.prototype.isVerbose = function () { return OutputInterface_1.VERBOSITY_VERBOSE === this.verbosity; }; /** * Returns whether verbosity is very verbose (-vv). * * @return bool true if verbosity is set to VERBOSITY_VERY_VERBOSE, false otherwise */ Output.prototype.isVeryVerbose = function () { return OutputInterface_1.VERBOSITY_VERY_VERBOSE === this.verbosity; }; /** * Returns whether verbosity is debug (-vvv). * * @return bool true if verbosity is set to VERBOSITY_DEBUG, false otherwise */ Output.prototype.isDebug = function () { return OutputInterface_1.VERBOSITY_DEBUG === this.verbosity; }; /** * 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 options A bitmask of options (one of the OUTPUT or VERBOSITY constants), * 0 is considered the same as OUTPUT_NORMAL | VERBOSITY_NORMAL */ Output.prototype.writeln = function (messages, options) { if (options === void 0) { options = OutputInterface_1.OUTPUT_NORMAL; } this.write(messages, true, options); }; /** * 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 options A bitmask of options (one of the OUTPUT or VERBOSITY constants), * 0 is considered the same as OUTPUT_NORMAL | VERBOSITY_NORMAL */ Output.prototype.write = function (messages, newline, options) { if (newline === void 0) { newline = false; } if (options === void 0) { options = OutputInterface_1.OUTPUT_NORMAL; } if (!Array.isArray(messages)) messages = [messages]; var types = OutputInterface_1.OUTPUT_NORMAL | OutputInterface_1.OUTPUT_RAW | OutputInterface_1.OUTPUT_PLAIN; var type = types & options || OutputInterface_1.OUTPUT_NORMAL; var verbosities = OutputInterface_1.VERBOSITY_QUIET | OutputInterface_1.VERBOSITY_NORMAL | OutputInterface_1.VERBOSITY_VERBOSE | OutputInterface_1.VERBOSITY_VERY_VERBOSE | OutputInterface_1.VERBOSITY_DEBUG; var verbosity = verbosities & options || OutputInterface_1.VERBOSITY_NORMAL; if (verbosity > this.getVerbosity()) { return; } for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) { var message = messages_1[_i]; switch (type) { case OutputInterface_1.OUTPUT_NORMAL: message = this.formatter.format(message); break; case OutputInterface_1.OUTPUT_RAW: break; case OutputInterface_1.OUTPUT_PLAIN: message = Helper_1.stripTags(this.formatter.format(message)); break; } this.doWrite(message, newline); } }; return Output; }()); exports.default = Output;