All files / lib welcome_text.js

64.22% Statements 158/246
100% Branches 1/1
0% Functions 0/3
64.22% Lines 158/246

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 24711x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x                     11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x             11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x                                                                                                                                                 11x 11x 11x 11x 11x  
/**
* @license Apache-2.0
*
* Copyright (c) 2019 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// VARIABLES //
 
// Minimum terminal width (in columns) for rendering the logo and info text side-by-side. Below this threshold, the welcome message switches to a stacked layout. NOTE: this value corresponds to the width of the widest side-by-side line; if the logo, info text, or command descriptions are modified, re-verify that this threshold still exceeds the widest rendered line.
var SIDE_BY_SIDE_MIN_WIDTH = 70;
 
// Minimum number of spaces separating the command column from the description column in the side-by-side help layout:
var HELP_GAP = 4;
 
// Indentation preceding each command in the help section:
var HELP_INDENT = '    ';
 
// Indentation preceding each description in the stacked (narrow) help layout:
var HELP_DESC_INDENT = '        ';
 
// Indentation preceding each line of info text in the stacked (narrow) layout:
var INFO_INDENT = '  ';
 
// Gutter placed on each side of the vertical bar separating the logo from the info text in the side-by-side layout:
var LOGO_GUTTER = '  ';
 
// Vertical bar separating the logo from the info text in the side-by-side layout:
var LOGO_BAR = '|';
 
var LOGO = [
	'     _      _ _ _ _',
	' ___| |_ __| | (_) |__',
	'/ __| __/ _` | | | \'_ \\',
	'\\__ \\ || (_| | | | |_) |',
	'|___/\\__\\__,_|_|_|_.__/'
];
 
// Info text rendered beside the logo. In the side-by-side layout, the first logo line has no counterpart, so these lines align to the logo lines beginning with the second (hence `LOGO.length` must equal `INFO_LINES.length+1`):
var INFO_LINES = [
	'A better REPL for JavaScript and Node.js.',
	'',
	'For more info, see the source repository:',
	'https://github.com/stdlib-js/stdlib'
];
 
var COMMANDS = [
	{
		'cmd': 'help()',
		'desc': 'Print help text.'
	},
	{
		'cmd': 'help(alias)',
		'desc': 'Print help text for a specified alias.'
	},
	{
		'cmd': 'help(alias.<key>)',
		'desc': 'Print help text for a specified property.'
	},
	{
		'cmd': '',
		'desc': ''
	},
	{
		'cmd': 'example(alias)',
		'desc': 'Run examples for a specified alias.'
	},
	{
		'cmd': 'example(alias.<key>)',
		'desc': 'Run examples for a specified property.'
	},
	{
		'cmd': '',
		'desc': ''
	},
	{
		'cmd': 'reset()',
		'desc': 'Reset the REPL.'
	},
	{
		'cmd': 'quit()',
		'desc': 'Exit the REPL.'
	},
	{
		'cmd': '',
		'desc': ''
	},
	{
		'cmd': 'license()',
		'desc': 'Print license information.'
	},
	{
		'cmd': 'copyright()',
		'desc': 'Print copyright information.'
	}
];
 
 
// FUNCTIONS //
 
/**
* Returns a string consisting of a character repeated a specified number of times.
*
* @private
* @param {string} ch - character to repeat
* @param {NonNegativeInteger} n - number of repetitions
* @returns {string} repeated string
*
* @example
* var str = repeat( '-', 3 );
* // returns '---'
*/
function repeat( ch, n ) {
	var out;
	var i;

	out = '';
	for ( i = 0; i < n; i++ ) {
		out += ch;
	}
	return out;
}
 
/**
* Right-pads a string with spaces up to a minimum length.
*
* ## Notes
*
* -   If the input string is already at least the desired length, the string is returned unchanged.
*
* @private
* @param {string} str - input string
* @param {NonNegativeInteger} len - minimum output length
* @returns {string} padded string
*
* @example
* var str = rightPad( 'abc', 5 );
* // returns 'abc  '
*/
function rightPad( str, len ) {
	if ( str.length >= len ) {
		return str;
	}
	return str + repeat( ' ', len-str.length );
}
 
 
// MAIN //
 
/**
* Returns the welcome message formatted for the given terminal width.
*
* @private
* @param {PositiveInteger} columns - terminal width in columns
* @returns {string} formatted welcome message
*/
function welcomeText( columns ) {
	var cmdWidth;
	var isWide;
	var logoW;
	var lines;
	var line;
	var text;
	var i;

	isWide = ( columns >= SIDE_BY_SIDE_MIN_WIDTH );

	// Resolve the width of the widest logo line (used to align the separator in both layouts):
	logoW = 0;
	for ( i = 0; i < LOGO.length; i++ ) {
		if ( LOGO[ i ].length > logoW ) {
			logoW = LOGO[ i ].length;
		}
	}
	lines = [];
	if ( isWide ) {
		// Side-by-side layout: render the first logo line on its own, then pair each subsequent logo line with a line of info text separated by a vertical bar...
		lines.push( LOGO[ 0 ] );
		for ( i = 1; i < LOGO.length; i++ ) {
			line = rightPad( LOGO[ i ], logoW ) + LOGO_GUTTER + LOGO_BAR;
			text = INFO_LINES[ i-1 ];
			if ( text ) {
				line += LOGO_GUTTER + text;
			}
			lines.push( line );
		}
	} else {
		// Stacked layout for narrow terminals: render the logo, a separator, and then the info text on successive lines...
		for ( i = 0; i < LOGO.length; i++ ) {
			lines.push( LOGO[ i ] );
		}
		lines.push( '' );
		lines.push( repeat( '-', logoW ) );
		lines.push( '' );
		for ( i = 0; i < INFO_LINES.length; i++ ) {
			lines.push( INFO_INDENT + INFO_LINES[ i ] );
		}
	}

	// Append the help text section:
	lines.push( '' );

	// In the side-by-side layout, resolve the command column width from the longest command so that descriptions remain aligned and separated by at least `HELP_GAP` spaces:
	cmdWidth = 0;
	if ( isWide ) {
		for ( i = 0; i < COMMANDS.length; i++ ) {
			if ( COMMANDS[ i ].cmd.length > cmdWidth ) {
				cmdWidth = COMMANDS[ i ].cmd.length;
			}
		}
		cmdWidth += HELP_GAP;
	}
	for ( i = 0; i < COMMANDS.length; i++ ) {
		if ( COMMANDS[ i ].cmd === '' ) {
			lines.push( '' );
		} else if ( isWide ) {
			lines.push( HELP_INDENT + rightPad( COMMANDS[ i ].cmd, cmdWidth ) + COMMANDS[ i ].desc );
		} else {
			// Stack the command and its description on separate lines:
			lines.push( HELP_INDENT + COMMANDS[ i ].cmd );
			lines.push( HELP_DESC_INDENT + COMMANDS[ i ].desc );
		}
	}
	lines.push( '' );
	lines.push( '' );

	return lines.join( '\n' );
}
 
 
// EXPORTS //
 
module.exports = welcomeText;