/**
* @license Apache-2.0
*
* Copyright (c) 2020 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';

// MODULES //

var RE_EOL = require( '@stdlib/regexp/eol' );


// VARIABLES //

var WIDTH = 76;
var TAB = '    ';


// MAIN //

/**
* Wraps a description.
*
* @param {string} txt - text to wrap
* @returns {string} wrapped text
*
* @example
* // TODO
*/
function wrap( txt ) {
	var lines;
	var start;
	var stop;
	var line;
	var tmp;
	var out;
	var ch;
	var i;
	var j;

	lines = txt.split( RE_EOL );
	out = [];
	tmp = [];
	for ( i = 0; i < lines.length; i++ ) {
		line = lines[ i ];

		// WARNING: we assume no Unicode. We should probably be determining length by the number of grapheme clusters (see @stdlib/string/num-grapheme-clusters), but we can reasonably assume that REPL text descriptions, at least for this project, do not contain visual characters such as Emojis, etc.
		start = 0;
		stop = WIDTH;
		tmp.length = 0;
		while ( stop < line.length ) {
			for ( j = stop; j >= start; j-- ) {
				ch = line.charAt( j );
				if ( ch === ' ' || ch === '-' ) { // e.g., `foo bar` or `foo-bar`
					// We found a break...
					tmp.push( TAB + line.slice( start, j+1 ) );
					start = j + 1;
					stop = start + WIDTH - 1;
				}
			}
			if ( j === -1 ) {
				break;
			}
		}
		tmp.push( TAB + line.slice( start ) );
		for ( j = 0; j < tmp.length; j++ ) {
			out.push( tmp[ j ] );
		}
	}
	return out.join( '\n' );
}


// EXPORTS //

module.exports = wrap;

/**
* @license Apache-2.0
*
* Copyright (c) 2020 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';

// MODULES //

var RE_EOL = require( '@stdlib/regexp/eol' );
var rtrim = require( '@stdlib/string/right-trim' );


// VARIABLES //

var WIDTH = 76;
var TAB = '    ';


// MAIN //

/**
* Wraps a description.
*
* @param {string} txt - text to wrap
* @returns {string} wrapped text
*
* @example
* // TODO
*/
function wrap( txt ) {
	var lines;
	var start;
	var stop;
	var line;
	var tmp;
	var out;
	var ch;
	var i;
	var j;

	lines = txt.split( RE_EOL );
	out = [];
	tmp = [];
	for ( i = 0; i < lines.length; i++ ) {
		line = lines[ i ];

		// WARNING: we assume no Unicode. We should probably be determining length by the number of grapheme clusters (see @stdlib/string/num-grapheme-clusters), but we can reasonably assume that REPL text descriptions, at least for this project, do not contain visual characters such as Emojis, etc.
		start = 0;
		stop = WIDTH;
		tmp.length = 0;
		while ( stop < line.length ) {
			for ( j = stop; j >= start; j-- ) {
				ch = line.charAt( j );
				if ( ch === ' ' || ch === '-' ) { // e.g., `foo bar` or `foo-bar`
					// We found a break...
					tmp.push( TAB + line.slice( start, j+1 ) );
					start = j + 1;
					stop = start + WIDTH - 1;
				}
			}
			if ( j === -1 ) {
				break;
			}
		}
		tmp.push( TAB + line.slice( start ) );
		for ( j = 0; j < tmp.length; j++ ) {
			out.push( rtrim( tmp[ j ] ) );
		}
	}
	return out.join( '\n' );
}


// EXPORTS //

module.exports = wrap;

Fix potential trailing spaces