symfony-style-console
Version:
Use the style and utilities of the Symfony Console in Node.js
354 lines (353 loc) • 13.3 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var env_1 = require("../env");
var Helper_1 = require("../Helper/Helper");
var OutputInterface_1 = require("../Output/OutputInterface");
var ConsoleInput_1 = require("../Input/ConsoleInput");
var ConsoleOutput_1 = require("../Output/ConsoleOutput");
var BufferedOutput_1 = require("../Output/BufferedOutput");
var Questionnaire_1 = require("../Helper/Questionnaire");
var Table_1 = require("../Helper/Table");
var OutputFormatter_1 = require("../Formatter/OutputFormatter");
var OutputStyle_1 = require("./OutputStyle");
var SymfonyStyle = /** @class */ (function (_super) {
__extends(SymfonyStyle, _super);
function SymfonyStyle(input, output) {
if (input === void 0) { input = new ConsoleInput_1.default(); }
if (output === void 0) { output = new ConsoleOutput_1.default(); }
var _this = _super.call(this, output) || this;
_this.input = input;
_this.bufferedOutput = new BufferedOutput_1.default(output.getVerbosity(), false, output.getFormatter().clone());
var width = SymfonyStyle.LINE_LENGTH ||
process.stdout.columns ||
SymfonyStyle.MAX_LINE_LENGTH;
_this.lineLength = Math.min(width - Number(env_1.DIRECTORY_SEPARATOR === '\\'), SymfonyStyle.MAX_LINE_LENGTH);
return _this;
}
/**
* Formats a message as a block of text.
*
* @param messages The message to write in the block
* @param type The block type (in [] on first line)
* @param style The style to apply to the whole block
* @param prefix The prefix for the block
* @param padding Whether to add vertical padding
* @param escape Whether to escape the messages
*/
SymfonyStyle.prototype.block = function (messages, type, style, prefix, padding) {
if (prefix === void 0) { prefix = ' '; }
if (padding === void 0) { padding = false; }
if (!Array.isArray(messages))
messages = [messages];
this.autoPrependBlock();
this.writeln(this.createBlock(messages, type, style, prefix, padding, true));
this.newLine();
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.title = function (message) {
this.autoPrependBlock();
this.writeln([
"<comment>" + OutputFormatter_1.default.escapeTrailingBackslash(message) + "</>",
"<comment>" + '='.repeat(Helper_1.lengthWithoutDecoration(this.getFormatter(), message)) + "</>"
]);
this.newLine();
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.section = function (message) {
this.autoPrependBlock();
this.writeln([
"<comment>" + OutputFormatter_1.default.escapeTrailingBackslash(message) + "</>",
"<comment>" + '-'.repeat(Helper_1.lengthWithoutDecoration(this.getFormatter(), message)) + "</>"
]);
this.newLine();
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.listing = function (elements) {
this.autoPrependText();
elements = elements.map(function (element) { return " * " + element; });
this.writeln(elements);
this.newLine();
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.text = function (message) {
this.autoPrependText();
var messages = Array.isArray(message) ? message.slice(0) : [message];
for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) {
var message_1 = messages_1[_i];
this.writeln(" " + message_1);
}
};
/**
* Formats a command comment.
*
* @param message
*/
SymfonyStyle.prototype.comment = function (message) {
var messages = Array.isArray(message) ? message.slice(0) : [message];
this.autoPrependBlock();
this.writeln(this.createBlock(messages, null, null, '<fg=default;bg=default> // </>'));
this.newLine();
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.success = function (message) {
this.block(message, 'OK', 'fg=black;bg=green', ' ', true);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.error = function (message) {
this.block(message, 'ERROR', 'fg=white;bg=red', ' ', true);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.warning = function (message) {
this.block(message, 'WARNING', 'fg=white;bg=red', ' ', true);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.note = function (message) {
this.block(message, 'NOTE', 'fg=yellow', ' ! ');
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.caution = function (message) {
this.block(message, 'CAUTION', 'fg=white;bg=red', ' ! ', true);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.table = function (headers, rows) {
var style = Table_1.default.getStyleDefinition('symfony-style-guide').clone();
style.setCellHeaderFormat('<info>%s</info>');
var table = new Table_1.default(this);
table.setHeaders(headers);
table.setRows(rows);
table.setStyle(style);
table.render();
this.newLine();
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.ask = function (question, defaultAnswer, validator) {
if (defaultAnswer === void 0) { defaultAnswer = null; }
if (validator === void 0) { validator = null; }
if (!this.input.isInteractive())
return Promise.resolve(null);
this.initQuestionnaire();
return this.questionnaire.ask(question, defaultAnswer, validator);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.askHidden = function (question, validator) {
if (validator === void 0) { validator = null; }
if (!this.input.isInteractive())
return Promise.resolve(null);
this.initQuestionnaire();
return this.questionnaire.askHidden(question, validator);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.confirm = function (question, defaultAnswer) {
if (defaultAnswer === void 0) { defaultAnswer = true; }
if (!this.input.isInteractive())
return Promise.resolve(null);
this.initQuestionnaire();
return this.questionnaire.confirm(question, defaultAnswer);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.choice = function (question, choices, defaultAnswer) {
if (defaultAnswer === void 0) { defaultAnswer = null; }
if (!this.input.isInteractive())
return Promise.resolve(null);
this.initQuestionnaire();
return this.questionnaire.choice(question, choices, defaultAnswer);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.progressStart = function (max) {
if (max === void 0) { max = 0; }
this.progressBar = this.createProgressBar(max);
this.progressBar.start();
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.progressAdvance = function (step) {
if (step === void 0) { step = 1; }
this.getProgressBar().advance(step);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.progressSet = function (step) {
this.getProgressBar().setProgress(step);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.progressFinish = function () {
this.getProgressBar().finish();
this.newLine(2);
this.progressBar = null;
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.createProgressBar = function (max) {
if (max === void 0) { max = 0; }
var progressBar = _super.prototype.createProgressBar.call(this, max);
if ('\\' !== env_1.DIRECTORY_SEPARATOR) {
progressBar.setEmptyBarCharacter('░'); // light shade character \u2591
progressBar.setProgressCharacter('');
progressBar.setBarCharacter('▓'); // dark shade character \u2593
}
return progressBar;
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.writeln = function (messages, type) {
if (type === void 0) { type = OutputInterface_1.OUTPUT_NORMAL; }
_super.prototype.writeln.call(this, messages, type);
this.bufferedOutput.writeln(this.reduceBuffer(messages), type);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.write = function (messages, newline, type) {
if (newline === void 0) { newline = false; }
if (type === void 0) { type = OutputInterface_1.OUTPUT_NORMAL; }
_super.prototype.write.call(this, messages, newline, type);
this.bufferedOutput.write(this.reduceBuffer(messages), newline, type);
};
/**
* {@inheritdoc}
*/
SymfonyStyle.prototype.newLine = function (count) {
if (count === void 0) { count = 1; }
_super.prototype.newLine.call(this, count);
this.bufferedOutput.write('\n'.repeat(count));
};
/**
* @return ProgressBar
*/
SymfonyStyle.prototype.getProgressBar = function () {
if (!this.progressBar) {
throw new Error('The ProgressBar is not started.');
}
return this.progressBar;
};
SymfonyStyle.prototype.autoPrependBlock = function () {
var chars = this.bufferedOutput
.fetch()
.replace(new RegExp(env_1.EOL, 'g'), '\n')
.slice(-2);
if (!chars.length) {
return this.newLine(); // Empty history, so we should start with a new line
}
// Prepend new line for each non LF chars (This means no blank line was output before)
this.newLine(2 - (Helper_1.countOccurences(chars, '\n') || 0));
};
SymfonyStyle.prototype.autoPrependText = function () {
var fetched = this.bufferedOutput.fetch();
// Prepend new line if last char isn't EOL:
if ('\n' !== fetched.slice(-1)) {
this.newLine();
}
};
SymfonyStyle.prototype.reduceBuffer = function (messages) {
if (!Array.isArray(messages))
messages = [messages];
// We need to know if the two last chars are EOL
// Preserve the last 4 chars inserted (EOL on windows is two chars) in the history buffer
return [this.bufferedOutput.fetch()]
.concat(messages)
.map(function (value) { return value.slice(-4); });
};
SymfonyStyle.prototype.createBlock = function (messages, type, style, prefix, padding, escape) {
if (type === void 0) { type = null; }
if (style === void 0) { style = null; }
if (prefix === void 0) { prefix = ' '; }
if (padding === void 0) { padding = false; }
if (escape === void 0) { escape = false; }
var indentLength = 0;
var lineIndetation;
var prefixLength = Helper_1.lengthWithoutDecoration(this.getFormatter(), prefix);
var lines = [];
if (null !== type) {
type = "[" + type + "] ";
indentLength = type.length;
lineIndetation = ' '.repeat(indentLength);
}
for (var key = 0; key < messages.length; key++) {
var message = messages[key];
if (escape) {
message = OutputFormatter_1.default.escape(message);
}
lines.push.apply(lines, Helper_1.wordwrap(message, this.lineLength - prefixLength - indentLength, env_1.EOL).split(env_1.EOL));
if (messages.length > 1 && key < messages.length - 1) {
lines.push('');
}
}
var firstLineIndex = 0;
if (padding && this.isDecorated()) {
firstLineIndex = 1;
lines.unshift('');
lines.push('');
}
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
if (null !== type) {
line = firstLineIndex === i ? type + line : lineIndetation + line;
}
line = prefix + line;
line += ' '.repeat(this.lineLength - Helper_1.lengthWithoutDecoration(this.getFormatter(), line));
if (style) {
line = "<" + style + ">" + line + "</>";
}
lines[i] = line;
}
return lines;
};
SymfonyStyle.prototype.initQuestionnaire = function () {
if (!this.questionnaire) {
this.questionnaire = new Questionnaire_1.default(this);
}
};
SymfonyStyle.LINE_LENGTH = 0;
SymfonyStyle.MAX_LINE_LENGTH = 120;
return SymfonyStyle;
}(OutputStyle_1.default));
exports.default = SymfonyStyle;