@tamara027/lerna-terminal
Version:
Powerful cli ui for monorepos
309 lines (289 loc) • 8.09 kB
JavaScript
/* eslint no-console: 0*/
/* eslint complexity: 0*/
;
var stringLength = require('../stringLength');
var stripTerminalString = require('../stripTerminalString');
var chalk = require('chalk');
var getFilledArray = require('../getFilledArray');
var truncate = require('../truncate');
var _require = require('../commander'),
program = _require.program;
/* istanbul ignore next */
var themeSymbolMap = {
test: {
title: function title(text) {
return text;
},
titleStopped: function titleStopped(text) {
return text;
},
msg: function msg(text) {
return text;
},
error: function error(text) {
return text;
},
titleSpace: ' ',
topLeft: '┌',
topRight: '┐',
content: '│',
separatorTop: '─',
separatorBottom: '─',
bottomLeft: '└',
bottomRight: '┘'
},
default: {
title: function title(text) {
return chalk.blue.bgWhite.bold(text);
},
titleStopped: function titleStopped(text) {
return chalk.red.bgWhite.bold(text);
},
msg: function msg(text) {
return text;
},
error: function error(text) {
return chalk.red(text);
},
titleSpace: chalk.bgWhite(' '),
topLeft: chalk.white('┌'),
topRight: chalk.white('┐'),
content: chalk.white('│'),
separatorTop: chalk.white('─'),
separatorBottom: chalk.white('─'),
bottomLeft: chalk.white('└'),
bottomRight: chalk.white('┘')
},
massive: {
title: function title(text) {
return chalk.blue.bgWhite.bold(text);
},
titleStopped: function titleStopped(text) {
return chalk.red.bgWhite.bold(text);
},
msg: function msg(text) {
return text;
},
error: function error(text) {
return chalk.red(text);
},
titleSpace: chalk.bgWhite(' '),
topLeft: chalk.bgWhite(' '),
topRight: chalk.bgWhite(' '),
content: chalk.bgWhite(' '),
separatorTop: chalk.bgWhite(' '),
separatorBottom: chalk.bgWhite(' '),
bottomLeft: chalk.bgWhite(' '),
bottomRight: chalk.bgWhite(' ')
},
minimal: {
title: function title(text) {
return chalk.blue.bgWhite.bold(text);
},
titleStopped: function titleStopped(text) {
return chalk.red.bgWhite.bold(text);
},
msg: function msg(text) {
return text;
},
error: function error(text) {
return chalk.red(text);
},
titleSpace: chalk.bgWhite(' '),
topLeft: ' ',
topRight: ' ',
content: ' ',
separatorTop: ' ',
separatorBottom: ' ',
bottomLeft: ' ',
bottomRight: ' '
}
};
/**
* @param {string} type - the symbol type eg. content,topLeft,...
* @returns {Object} returns the package.json data object
**/
function getThemeSymbol(type) {
/* istanbul ignore next */
if (process.env.NODE_ENV === 'test') {
return themeSymbolMap.test[type];
}
/* istanbul ignore next */
return themeSymbolMap[program.theme || 'default'][type];
}
/**
* @param {string} filled - empty space of line filled with
* @param {string} start - the first part of the line
* @param {string} content - the inner part of the line, filled with "filled" till end of line
* @param {string} end - the last part of the line
* @param {number} width - the line width
* @returns {string} - returns an line
**/
function getLine(_ref) {
var filled = _ref.filled,
start = _ref.start,
content = _ref.content,
end = _ref.end,
width = _ref.width;
if (filled !== '') {
return getFilledArray(width, filled).join('');
}
var out = '';
var maxContentLength = width - 3;
if (start) {
out += start;
}
if (content) {
content = stripTerminalString(content);
if (stringLength(content) > maxContentLength) {
content = truncate(content, maxContentLength, false);
}
out += ' ' + content;
out += getFilledArray(maxContentLength - stringLength(content), ' ').join('');
} else {
out += getFilledArray(maxContentLength, ' ').join('');
}
if (end) {
out += end;
}
return out;
}
/**
* @param {string} title - the title which will be displayed in start line
* @param {number} width - the line width
* @param {boolean} isRunning - flag for the child process is running or not
* @returns {string} - returns an start line
**/
function getStartLine(title, width, isRunning) {
title = isRunning ? getThemeSymbol('title')(title) : getThemeSymbol('titleStopped')(title);
var separator = getThemeSymbol('separatorTop');
var titleSpace = getThemeSymbol('titleSpace');
var topLeft = getThemeSymbol('topLeft');
var topRight = getThemeSymbol('topRight');
var line = '' + topLeft + separator + separator + titleSpace + title + titleSpace;
line += getFilledArray(width - stringLength(line) - 1, separator).join('');
line += topRight;
return line;
}
/**
* @param {number} width - the line width
* @returns {string} - returns an empty line
**/
function getEmptyLine(width) {
return getLine({
filled: '',
start: getThemeSymbol('content'),
content: ' ',
end: getThemeSymbol('content'),
width: width
});
}
/**
* @param {number} width - the line width
* @returns {string} - returns an end line
**/
function getEndLine(width) {
var line = getFilledArray(width, getThemeSymbol('separatorBottom'));
line[0] = getThemeSymbol('bottomLeft');
line[line.length - 1] = getThemeSymbol('bottomRight');
return line.join('');
}
/**
* @param {number} width - the panel width
* @param {number} height - the panel height
* @param {string} title - the title which will be displayed in start line
* @param {Array<string>} lines - array with the content of the panel
* @param {boolean} isRunning - flag for is running or not
* @returns {string} - returns an bordered panel
**/
function getTerminalPanel(_ref2) {
var width = _ref2.width,
height = _ref2.height,
title = _ref2.title,
lines = _ref2.lines,
_ref2$isRunning = _ref2.isRunning,
isRunning = _ref2$isRunning === undefined ? false : _ref2$isRunning;
width = parseInt(width, 10);
var out = [];
var rLines = lines.concat([]);
var availableLines = height - 4;
var renderedLines = 0;
var shortenLines = void 0;
if (rLines.length > availableLines) {
shortenLines = rLines.slice(rLines.length - availableLines, rLines.length);
} else {
shortenLines = rLines.concat([]);
}
out.push(getStartLine(title, width, isRunning));
out.push(getEmptyLine(width));
while (availableLines > 0) {
if (shortenLines[renderedLines]) {
out.push(getLine({
filled: '',
start: getThemeSymbol('content'),
content: shortenLines[renderedLines],
end: getThemeSymbol('content'),
width: width
}));
} else {
out.push(getEmptyLine(width));
}
availableLines -= 1;
renderedLines += 1;
}
out.push(getEmptyLine(width));
out.push(getEndLine(width));
return out;
}
/**
* @param {number} width - the panel width
* @param {number} height - the panel height
* @param {string} title - the title which will be displayed in start line
* @param {Array<string>} lines - array with the content of the panel
* @returns {string} - returns an bordered panel
**/
function getTerminal(width, height, title, lines) {
return lines.map(function (line) {
return line + '\n';
});
}
/**
* @param {string} text - the text
* @param {string} type - the text type eg. error or msg
* @returns {string} - returns an colored text
**/
function getText(text, type) {
return getThemeSymbol(type)(text);
}
/**
* @param {string} title - the title which will be displayed in start line
* @param {Array<string>} lines - the content array
* @param {number} width - the line width
* @returns {string} - returns an box arrount the given lines
**/
function getBox(title, lines, width) {
var out = [];
out.push(getStartLine(title, width));
out.push(getEmptyLine(width));
lines.forEach(function (line) {
out.push(getLine({
filled: '',
start: getThemeSymbol('content'),
content: line,
end: getThemeSymbol('content'),
width: width
}));
});
out.push(getEmptyLine(width));
out.push(getEndLine(width));
return out.join('');
}
module.exports = {
getTerminal: getTerminal,
getTerminalPanel: getTerminalPanel,
getLine: getLine,
getStartLine: getStartLine,
getEndLine: getEndLine,
getText: getText,
getBox: getBox
};