Skip to content

Instantly share code, notes, and snippets.

@GramThanos
Last active May 28, 2018 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GramThanos/250acb1b890f5873b641c3cc4697b71a to your computer and use it in GitHub Desktop.
Save GramThanos/250acb1b890f5873b641c3cc4697b71a to your computer and use it in GitHub Desktop.
/*
* Console.Title v0.0.2-beta
*
*
* MIT License
*
* Copyright (c) 2018 Grammatopoulos Athanasios-Vasileios
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
/*
Example Usage:
console.title("THIS IS AWESOME!\nSO COOL!");
console.title(
"STOP!\n\nThis feature is only\nfor developers",
{padding:[1,5,1,5], margin:[1,0,1,0]}
);
console.title("TEXT\nAligned on the left", {align:'left'});
console.title("TEXT\nAligned on the right", {align:'right'});
*/
(function () {
// Border chars
var BORDER = {
TOP : '─',
TOP_LEFT : '╭',
TOP_RIGHT : '╮',
BOTTOM : '─',
BOTTOM_LEFT : '╰',
BOTTOM_RIGHT : '╯',
LEFT : '│',
RIGHT : '│'
};
// Options parser
var parseOptions = function(options) {
if (!options)
options = {};
// Margin
if (typeof options.margin !== 'object') {
options.margin = [1, 3, 1, 3];
}
else {
if (typeof options.margin[0] !== 'number') options.margin[0] = 1;
if (typeof options.margin[1] !== 'number') options.margin[1] = 3;
if (typeof options.margin[2] !== 'number') options.margin[2] = options.margin[0];
if (typeof options.margin[3] !== 'number') options.margin[3] = options.margin[1];
}
// Padding
if (typeof options.padding !== 'object') {
options.padding = [1, 3, 1, 3];
}
else {
if (typeof options.padding[0] !== 'number') options.padding[0] = 1;
if (typeof options.padding[1] !== 'number') options.padding[1] = 3;
if (typeof options.padding[2] !== 'number') options.padding[2] = options.padding[0];
if (typeof options.padding[3] !== 'number') options.padding[3] = options.padding[1];
}
// Width
if (typeof options.width !== 'number') {
options.width = 0;
}
// Height
if (typeof options.height !== 'number') {
options.height = 0;
}
// Align
if (typeof options.align !== 'string') {
options.align = 'center';
}
else {
if (
options.align !== 'center' &&
options.align !== 'left' &&
options.align !== 'right'
) {
options.align = 'center';
}
}
return options;
};
// Split message to lines
var messageToLines = function(message) {
// Split on line endings
message = message.split('\n');
return message;
};
// Construct a line
var createLine = function () {
var options = arguments[arguments.length - 1];
var i, j;
var text, space, prefix, suffix;
var line = '';
for (i = 0; i < arguments.length - 1; i++) {
if (typeof arguments[i] === 'object') {
text = arguments[i][0];
space = arguments[i][1] - text.length;
if (options.align == 'left') prefix = 0;
else if (options.align == 'right') prefix = space;
else prefix = Math.floor(space/2);
suffix = space - prefix;
for (j = 0; j < prefix; j++) {
line += ' ';
}
line += text;
for (j = 0; j < suffix; j++) {
line += ' ';
}
}
else if (typeof arguments[i] === 'number') {
for (j = arguments[i] - 1; j >= 0; j--) {
line += ' ';
}
}
else {
line += arguments[i];
}
}
return line;
};
// Construct a body
var createBody = function (lines, options) {
var body = [];
var i;
var innerHeight = options.padding[0] + lines.length + options.padding[2];
if (innerHeight < options.height) {
innerHeight = options.height;
}
var innerWidth = 0;
for (i = lines.length - 1; i >= 0; i--) {
if (innerWidth < lines[i].length)
innerWidth = lines[i].length;
}
if (innerWidth < options.width) {
innerWidth = options.width;
}
var innerVertical_space = innerHeight - options.padding[0] - lines.length - options.padding[2];
var innerTop_lines = Math.floor(innerVertical_space/2);
var innerBottom_lines = innerVertical_space - innerTop_lines;
// Margin top - bottom lines
var marginVertical = createLine(
options.margin[3] + 1 + options.padding[3],
innerWidth,
options.padding[1] + 1 + options.margin[1],
options
);
// Create margin top
for (i = 0; i < options.margin[0]; i++) {
body.push(marginVertical);
}
// Create top border
var borderTop = '';
var borderBottom = '';
for (i = options.padding[3] + innerWidth + options.padding[1] - 1; i >= 0; i--) {
borderTop += BORDER.TOP;
borderBottom += BORDER.BOTTOM;
}
body.push(
createLine(
options.margin[3],
BORDER.TOP_LEFT,
borderTop,
BORDER.TOP_RIGHT,
options.margin[1],
options
)
);
// Add top space
var spaceLine = createLine(
options.margin[3],
BORDER.LEFT,
options.padding[3],
innerWidth,
options.padding[1],
BORDER.RIGHT,
options.margin[1],
options
);
for (i = 0; i < innerTop_lines + options.padding[0]; i++) {
body.push(spaceLine);
}
// Add lines
for (i = 0; i < lines.length; i++) {
body.push(
createLine(
options.margin[3],
BORDER.LEFT,
options.padding[3],
[lines[i], innerWidth],
options.padding[1],
BORDER.RIGHT,
options.margin[1],
options
)
);
}
// Add bottom space
for (i = 0; i < innerBottom_lines + options.padding[2]; i++) {
body.push(spaceLine);
}
// Create bottom border
body.push(
createLine(
options.margin[3],
BORDER.BOTTOM_LEFT,
borderBottom,
BORDER.BOTTOM_RIGHT,
options.margin[1],
options
)
);
// Create margin bottom
for (i = 0; i < options.margin[2]; i++) {
body.push(marginVertical);
}
return body;
};
// Parser
var parser = function(message, options) {
if (typeof message !== 'string')
throw 'console.title: Unsupported format!';
// Parse options
options = parseOptions(options);
// Break message to lines
var lines = messageToLines(message);
// Create message body
var body = createBody(lines, options);
return body;
};
// Define Console Title
console.title = function(message, options) {
// Parse it!
var body = parser(message, options);
// Construct and print
console.log(body.join('\n'));
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment