UNPKG

less-pager-mini

Version:

A scrollable terminal pager for Node.js CLI apps (terminal only)

77 lines (76 loc) 2.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapLongLines = wrapLongLines; const config_1 = require("./config"); const helpers_1 = require("./helpers"); /** * Wraps lines into subrows to fit screen width and fills the window. * * @param content - Full content lines. * @param lines - Output array of wrapped lines. */ function wrapLongLines(content, lines) { const maxRow = content.length - config_1.config.row; let row = 0; let isCompleteLine = true; let line = content[config_1.config.row]; if (config_1.config.subRow) { isCompleteLine = partitionLine(lines, line, config_1.config.subRow); row++; } while (lines.length < config_1.config.window - 1 && row < maxRow) { line = content[config_1.config.row + row]; if ((0, helpers_1.visualWidth)(line) > config_1.config.screenWidth) { isCompleteLine = partitionLine(lines, line, 0); } else { lines.push(line); } row++; } config_1.mode.EOF = isCompleteLine && row === maxRow; } /** * Wraps a long line into subrows and appends them to `lines`. * * - Starts from `subRowStart` for partial line display. * - Stops early if window limit is reached. * * @param lines - Output array for wrapped segments. * @param longLine - The line to split and wrap. * @param subRowStart - Starting subrow index (default 0). * @returns `true` if the line is fully wrapped, `false` if truncated. */ function partitionLine(lines, longLine, subRowStart) { let line = []; const segments = Array.from(longLine); let length = 0; let subRow = 0; for (let i = 0; i < segments.length; i++) { const segment = segments[i]; const segmentWidth = (0, helpers_1.visualWidth)(segment); const concatLength = length + segmentWidth; if (concatLength < config_1.config.screenWidth) { line.push(segment); length = concatLength; continue; } const overflow = concatLength > config_1.config.screenWidth; if (subRow >= subRowStart) { if (lines.length === config_1.config.window - 1) return false; if (!overflow) line.push(segment); lines.push(line.join('')); } line = overflow ? [segment] : []; length = overflow ? segmentWidth : 0; subRow++; } if (line.length && subRow >= subRowStart) { if (lines.length === config_1.config.window - 1) return false; lines.push(line.join('')); } return true; }