@listr2/manager
Version:
Extension for Listr2 to easily create sane defaults for task lists.
125 lines (121 loc) • 6.04 kB
JavaScript
import { ansi_styles_default, stringWidth } from "./string-width-BnS-vObO.js";
//#region ../../node_modules/.pnpm/is-fullwidth-code-point@4.0.0/node_modules/is-fullwidth-code-point/index.js
function isFullwidthCodePoint(codePoint) {
if (!Number.isInteger(codePoint)) return false;
return codePoint >= 4352 && (codePoint <= 4447 || codePoint === 9001 || codePoint === 9002 || 11904 <= codePoint && codePoint <= 12871 && codePoint !== 12351 || 12880 <= codePoint && codePoint <= 19903 || 19968 <= codePoint && codePoint <= 42182 || 43360 <= codePoint && codePoint <= 43388 || 44032 <= codePoint && codePoint <= 55203 || 63744 <= codePoint && codePoint <= 64255 || 65040 <= codePoint && codePoint <= 65049 || 65072 <= codePoint && codePoint <= 65131 || 65281 <= codePoint && codePoint <= 65376 || 65504 <= codePoint && codePoint <= 65510 || 110592 <= codePoint && codePoint <= 110593 || 127488 <= codePoint && codePoint <= 127569 || 131072 <= codePoint && codePoint <= 262141);
}
//#endregion
//#region ../../node_modules/.pnpm/slice-ansi@5.0.0/node_modules/slice-ansi/index.js
const astralRegex = /^[\uD800-\uDBFF][\uDC00-\uDFFF]$/;
const ESCAPES = ["\x1B", ""];
const wrapAnsi = (code) => `${ESCAPES[0]}[${code}m`;
const checkAnsi = (ansiCodes, isEscapes, endAnsiCode) => {
let output = [];
ansiCodes = [...ansiCodes];
for (let ansiCode of ansiCodes) {
const ansiCodeOrigin = ansiCode;
if (ansiCode.includes(";")) ansiCode = ansiCode.split(";")[0][0] + "0";
const item = ansi_styles_default.codes.get(Number.parseInt(ansiCode, 10));
if (item) {
const indexEscape = ansiCodes.indexOf(item.toString());
if (indexEscape === -1) output.push(wrapAnsi(isEscapes ? item : ansiCodeOrigin));
else ansiCodes.splice(indexEscape, 1);
} else if (isEscapes) {
output.push(wrapAnsi(0));
break;
} else output.push(wrapAnsi(ansiCodeOrigin));
}
if (isEscapes) {
output = output.filter((element, index) => output.indexOf(element) === index);
if (endAnsiCode !== void 0) {
const fistEscapeCode = wrapAnsi(ansi_styles_default.codes.get(Number.parseInt(endAnsiCode, 10)));
output = output.reduce((current, next) => next === fistEscapeCode ? [next, ...current] : [...current, next], []);
}
}
return output.join("");
};
function sliceAnsi(string, begin, end) {
const characters = [...string];
const ansiCodes = [];
let stringEnd = typeof end === "number" ? end : characters.length;
let isInsideEscape = false;
let ansiCode;
let visible = 0;
let output = "";
for (const [index, character] of characters.entries()) {
let leftEscape = false;
if (ESCAPES.includes(character)) {
const code = /\d[^m]*/.exec(string.slice(index, index + 18));
ansiCode = code && code.length > 0 ? code[0] : void 0;
if (visible < stringEnd) {
isInsideEscape = true;
if (ansiCode !== void 0) ansiCodes.push(ansiCode);
}
} else if (isInsideEscape && character === "m") {
isInsideEscape = false;
leftEscape = true;
}
if (!isInsideEscape && !leftEscape) visible++;
if (!astralRegex.test(character) && isFullwidthCodePoint(character.codePointAt())) {
visible++;
if (typeof end !== "number") stringEnd++;
}
if (visible > begin && visible <= stringEnd) output += character;
else if (visible === begin && !isInsideEscape && ansiCode !== void 0) output = checkAnsi(ansiCodes);
else if (visible >= stringEnd) {
output += checkAnsi(ansiCodes, true, ansiCode);
break;
}
}
return output;
}
//#endregion
//#region ../../node_modules/.pnpm/cli-truncate@4.0.0/node_modules/cli-truncate/index.js
function getIndexOfNearestSpace(string, wantedIndex, shouldSearchRight) {
if (string.charAt(wantedIndex) === " ") return wantedIndex;
const direction = shouldSearchRight ? 1 : -1;
for (let index = 0; index <= 3; index++) {
const finalIndex = wantedIndex + index * direction;
if (string.charAt(finalIndex) === " ") return finalIndex;
}
return wantedIndex;
}
function cliTruncate(text, columns, options = {}) {
const { position = "end", space = false, preferTruncationOnSpace = false } = options;
let { truncationCharacter = "…" } = options;
if (typeof text !== "string") throw new TypeError(`Expected \`input\` to be a string, got ${typeof text}`);
if (typeof columns !== "number") throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
if (columns < 1) return "";
if (columns === 1) return truncationCharacter;
const length = stringWidth(text);
if (length <= columns) return text;
if (position === "start") {
if (preferTruncationOnSpace) {
const nearestSpace = getIndexOfNearestSpace(text, length - columns + 1, true);
return truncationCharacter + sliceAnsi(text, nearestSpace, length).trim();
}
if (space === true) truncationCharacter += " ";
return truncationCharacter + sliceAnsi(text, length - columns + stringWidth(truncationCharacter), length);
}
if (position === "middle") {
if (space === true) truncationCharacter = ` ${truncationCharacter} `;
const half = Math.floor(columns / 2);
if (preferTruncationOnSpace) {
const spaceNearFirstBreakPoint = getIndexOfNearestSpace(text, half);
const spaceNearSecondBreakPoint = getIndexOfNearestSpace(text, length - (columns - half) + 1, true);
return sliceAnsi(text, 0, spaceNearFirstBreakPoint) + truncationCharacter + sliceAnsi(text, spaceNearSecondBreakPoint, length).trim();
}
return sliceAnsi(text, 0, half) + truncationCharacter + sliceAnsi(text, length - (columns - half) + stringWidth(truncationCharacter), length);
}
if (position === "end") {
if (preferTruncationOnSpace) {
const nearestSpace = getIndexOfNearestSpace(text, columns - 1);
return sliceAnsi(text, 0, nearestSpace) + truncationCharacter;
}
if (space === true) truncationCharacter = ` ${truncationCharacter}`;
return sliceAnsi(text, 0, columns - stringWidth(truncationCharacter)) + truncationCharacter;
}
throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
}
//#endregion
export { cliTruncate as default };