UNPKG

@listr2/manager

Version:

Extension for Listr2 to easily create sane defaults for task lists.

134 lines (132 loc) 5.23 kB
const require_string_width = require('./string-width-fWmCaoVR.cjs'); //#region ../../node_modules/.pnpm/wrap-ansi@9.0.0/node_modules/wrap-ansi/index.js const ESCAPES = new Set(["\x1B", "›"]); const END_CODE = 39; const ANSI_ESCAPE_BELL = "\x07"; const ANSI_CSI = "["; const ANSI_OSC = "]"; const ANSI_SGR_TERMINATOR = "m"; const ANSI_ESCAPE_LINK = `${ANSI_OSC}8;;`; const wrapAnsiCode = (code) => `${ESCAPES.values().next().value}${ANSI_CSI}${code}${ANSI_SGR_TERMINATOR}`; const wrapAnsiHyperlink = (url) => `${ESCAPES.values().next().value}${ANSI_ESCAPE_LINK}${url}${ANSI_ESCAPE_BELL}`; const wordLengths = (string) => string.split(" ").map((character) => require_string_width.stringWidth(character)); const wrapWord = (rows, word, columns) => { const characters = [...word]; let isInsideEscape = false; let isInsideLinkEscape = false; let visible = require_string_width.stringWidth(require_string_width.stripAnsi(rows.at(-1))); for (const [index, character] of characters.entries()) { const characterLength = require_string_width.stringWidth(character); if (visible + characterLength <= columns) rows[rows.length - 1] += character; else { rows.push(character); visible = 0; } if (ESCAPES.has(character)) { isInsideEscape = true; const ansiEscapeLinkCandidate = characters.slice(index + 1, index + 1 + ANSI_ESCAPE_LINK.length).join(""); isInsideLinkEscape = ansiEscapeLinkCandidate === ANSI_ESCAPE_LINK; } if (isInsideEscape) { if (isInsideLinkEscape) { if (character === ANSI_ESCAPE_BELL) { isInsideEscape = false; isInsideLinkEscape = false; } } else if (character === ANSI_SGR_TERMINATOR) isInsideEscape = false; continue; } visible += characterLength; if (visible === columns && index < characters.length - 1) { rows.push(""); visible = 0; } } if (!visible && rows.at(-1).length > 0 && rows.length > 1) rows[rows.length - 2] += rows.pop(); }; const stringVisibleTrimSpacesRight = (string) => { const words = string.split(" "); let last = words.length; while (last > 0) { if (require_string_width.stringWidth(words[last - 1]) > 0) break; last--; } if (last === words.length) return string; return words.slice(0, last).join(" ") + words.slice(last).join(""); }; const exec = (string, columns, options = {}) => { if (options.trim !== false && string.trim() === "") return ""; let returnValue = ""; let escapeCode; let escapeUrl; const lengths = wordLengths(string); let rows = [""]; for (const [index, word] of string.split(" ").entries()) { if (options.trim !== false) rows[rows.length - 1] = rows.at(-1).trimStart(); let rowLength = require_string_width.stringWidth(rows.at(-1)); if (index !== 0) { if (rowLength >= columns && (options.wordWrap === false || options.trim === false)) { rows.push(""); rowLength = 0; } if (rowLength > 0 || options.trim === false) { rows[rows.length - 1] += " "; rowLength++; } } if (options.hard && lengths[index] > columns) { const remainingColumns = columns - rowLength; const breaksStartingThisLine = 1 + Math.floor((lengths[index] - remainingColumns - 1) / columns); const breaksStartingNextLine = Math.floor((lengths[index] - 1) / columns); if (breaksStartingNextLine < breaksStartingThisLine) rows.push(""); wrapWord(rows, word, columns); continue; } if (rowLength + lengths[index] > columns && rowLength > 0 && lengths[index] > 0) { if (options.wordWrap === false && rowLength < columns) { wrapWord(rows, word, columns); continue; } rows.push(""); } if (rowLength + lengths[index] > columns && options.wordWrap === false) { wrapWord(rows, word, columns); continue; } rows[rows.length - 1] += word; } if (options.trim !== false) rows = rows.map((row) => stringVisibleTrimSpacesRight(row)); const preString = rows.join("\n"); const pre = [...preString]; let preStringIndex = 0; for (const [index, character] of pre.entries()) { returnValue += character; if (ESCAPES.has(character)) { const { groups } = (/* @__PURE__ */ new RegExp(`(?:\\${ANSI_CSI}(?<code>\\d+)m|\\${ANSI_ESCAPE_LINK}(?<uri>.*)${ANSI_ESCAPE_BELL})`)).exec(preString.slice(preStringIndex)) || { groups: {} }; if (groups.code !== void 0) { const code$1 = Number.parseFloat(groups.code); escapeCode = code$1 === END_CODE ? void 0 : code$1; } else if (groups.uri !== void 0) escapeUrl = groups.uri.length === 0 ? void 0 : groups.uri; } const code = require_string_width.ansi_styles_default.codes.get(Number(escapeCode)); if (pre[index + 1] === "\n") { if (escapeUrl) returnValue += wrapAnsiHyperlink(""); if (escapeCode && code) returnValue += wrapAnsiCode(code); } else if (character === "\n") { if (escapeCode && code) returnValue += wrapAnsiCode(escapeCode); if (escapeUrl) returnValue += wrapAnsiHyperlink(escapeUrl); } preStringIndex += character.length; } return returnValue; }; function wrapAnsi(string, columns, options) { return String(string).normalize().replaceAll("\r\n", "\n").split("\n").map((line) => exec(line, columns, options)).join("\n"); } //#endregion Object.defineProperty(exports, 'wrapAnsi', { enumerable: true, get: function () { return wrapAnsi; } });