Son CV dans un terminal web en Javascript! https://terminal-cv.gregandev.fr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
8.5 KiB

1 year ago
"use strict";
1 year ago
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = codeFrame;
1 year ago
function _chalk() {
const data = _interopRequireDefault(require("chalk"));
1 year ago
_chalk = function () {
return data;
};
1 year ago
return data;
}
1 year ago
function _emphasize() {
const data = _interopRequireDefault(require("emphasize"));
1 year ago
_emphasize = function () {
return data;
};
1 year ago
return data;
}
1 year ago
function _stringWidth() {
const data = _interopRequireDefault(require("string-width"));
1 year ago
_stringWidth = function () {
return data;
};
1 year ago
return data;
}
1 year ago
function _sliceAnsi() {
const data = _interopRequireDefault(require("slice-ansi"));
1 year ago
_sliceAnsi = function () {
return data;
};
1 year ago
return data;
}
1 year ago
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1 year ago
const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
const TAB_REPLACE_REGEX = /\t/g;
const TAB_REPLACEMENT = ' ';
const DEFAULT_TERMINAL_WIDTH = 80;
1 year ago
const highlightSyntax = (txt, lang) => {
if (lang) {
try {
return _emphasize().default.highlight(lang, txt).value;
} catch (e) {// fallback for unknown languages...
}
}
1 year ago
return _emphasize().default.highlightAuto(txt).value;
};
1 year ago
function codeFrame(code, highlights, inputOpts = {}) {
var _inputOpts$maxLines;
1 year ago
if (highlights.length < 1) return '';
let opts = {
useColor: !!inputOpts.useColor,
syntaxHighlighting: !!inputOpts.syntaxHighlighting,
language: inputOpts.language,
maxLines: (_inputOpts$maxLines = inputOpts.maxLines) !== null && _inputOpts$maxLines !== void 0 ? _inputOpts$maxLines : 12,
terminalWidth: inputOpts.terminalWidth || DEFAULT_TERMINAL_WIDTH,
padding: inputOpts.padding || {
before: 1,
after: 2
}
}; // Highlights messages and prefixes when colors are enabled
1 year ago
const highlighter = (s, bold) => {
if (opts.useColor) {
let redString = _chalk().default.red(s);
1 year ago
return bold ? _chalk().default.bold(redString) : redString;
}
1 year ago
return s;
}; // Prefix lines with the line number
1 year ago
const lineNumberPrefixer = params => {
let {
lineNumber,
lineNumberLength,
isHighlighted
} = params;
return `${isHighlighted ? highlighter('>') : ' '} ${lineNumber ? lineNumber.padStart(lineNumberLength, ' ') : ' '.repeat(lineNumberLength)} | `;
}; // Make columns/lines start at 1
1 year ago
highlights = highlights.map(h => {
return {
1 year ago
start: {
column: h.start.column - 1,
line: h.start.line - 1
},
end: {
column: h.end.column - 1,
line: h.end.line - 1
},
message: h.message
};
1 year ago
}); // Find first and last highlight
1 year ago
let firstHighlight = highlights.length > 1 ? highlights.sort((a, b) => a.start.line - b.start.line)[0] : highlights[0];
let lastHighlight = highlights.length > 1 ? highlights.sort((a, b) => b.end.line - a.end.line)[0] : highlights[0]; // Calculate first and last line index of codeframe
1 year ago
let startLine = firstHighlight.start.line - opts.padding.before;
startLine = startLine < 0 ? 0 : startLine;
let endLineIndex = lastHighlight.end.line + opts.padding.after;
endLineIndex = endLineIndex - startLine > opts.maxLines ? startLine + opts.maxLines - 1 : endLineIndex;
let lineNumberLength = (endLineIndex + 1).toString(10).length; // Split input into lines and highlight syntax
1 year ago
let lines = code.split(NEWLINE);
let syntaxHighlightedLines = (opts.syntaxHighlighting ? highlightSyntax(code, opts.language) : code).replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT).split(NEWLINE); // Loop over all lines and create codeframe
1 year ago
let resultLines = [];
1 year ago
for (let currentLineIndex = startLine; currentLineIndex < syntaxHighlightedLines.length; currentLineIndex++) {
if (currentLineIndex > endLineIndex) break;
if (currentLineIndex > syntaxHighlightedLines.length - 1) break; // Find highlights that need to get rendered on the current line
1 year ago
let lineHighlights = highlights.filter(highlight => highlight.start.line <= currentLineIndex && highlight.end.line >= currentLineIndex).sort((a, b) => (a.start.line < currentLineIndex ? 0 : a.start.column) - (b.start.line < currentLineIndex ? 0 : b.start.column)); // Check if this line has a full line highlight
1 year ago
let isWholeLine = lineHighlights.length && !!lineHighlights.find(h => h.start.line < currentLineIndex && h.end.line > currentLineIndex);
let lineLengthLimit = opts.terminalWidth > lineNumberLength + 7 ? opts.terminalWidth - (lineNumberLength + 5) : 10; // Split the line into line parts that will fit the provided terminal width
1 year ago
let colOffset = 0;
let lineEndCol = lineLengthLimit;
let syntaxHighlightedLine = syntaxHighlightedLines[currentLineIndex];
1 year ago
if ((0, _stringWidth().default)(syntaxHighlightedLine) > lineLengthLimit) {
if (lineHighlights.length > 0) {
if (lineHighlights[0].start.line === currentLineIndex) {
colOffset = lineHighlights[0].start.column - 5;
} else if (lineHighlights[0].end.line === currentLineIndex) {
colOffset = lineHighlights[0].end.column - 5;
}
1 year ago
}
1 year ago
colOffset = colOffset > 0 ? colOffset : 0;
lineEndCol = colOffset + lineLengthLimit;
syntaxHighlightedLine = (0, _sliceAnsi().default)(syntaxHighlightedLine, colOffset, lineEndCol);
} // Write the syntax highlighted line part
1 year ago
resultLines.push(lineNumberPrefixer({
lineNumber: (currentLineIndex + 1).toString(10),
lineNumberLength,
isHighlighted: lineHighlights.length > 0
}) + syntaxHighlightedLine);
let lineWidth = (0, _stringWidth().default)(syntaxHighlightedLine);
let highlightLine = '';
1 year ago
if (isWholeLine) {
highlightLine = highlighter('^'.repeat(lineWidth));
} else if (lineHighlights.length > 0) {
let lastCol = 0;
let highlight = null;
let highlightHasEnded = false;
1 year ago
for (let highlightIndex = 0; highlightIndex < lineHighlights.length; highlightIndex++) {
// Set highlight to current highlight
highlight = lineHighlights[highlightIndex];
highlightHasEnded = false; // Calculate the startColumn and get the real width by doing a substring of the original
// line and replacing tabs with our tab replacement to support tab handling
1 year ago
let startCol = 0;
1 year ago
if (highlight.start.line === currentLineIndex && highlight.start.column > colOffset) {
startCol = lines[currentLineIndex].substring(colOffset, highlight.start.column).replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT).length;
} // Calculate the endColumn and get the real width by doing a substring of the original
// line and replacing tabs with our tab replacement to support tab handling
1 year ago
let endCol = lineWidth - 1;
1 year ago
if (highlight.end.line === currentLineIndex) {
endCol = lines[currentLineIndex].substring(colOffset, highlight.end.column).replace(TAB_REPLACE_REGEX, TAB_REPLACEMENT).length; // If the endCol is too big for this line part, trim it so we can handle it in the next one
1 year ago
if (endCol > lineWidth) {
endCol = lineWidth - 1;
}
1 year ago
highlightHasEnded = true;
} // If endcol is smaller than lastCol it overlaps with another highlight and is no longer visible, we can skip those
1 year ago
if (endCol >= lastCol) {
let characters = endCol - startCol + 1;
1 year ago
if (startCol > lastCol) {
// startCol is before lastCol, so add spaces as padding before the highlight indicators
highlightLine += ' '.repeat(startCol - lastCol);
} else if (lastCol > startCol) {
// If last column is larger than the start, there's overlap in highlights
// This line adjusts the characters count to ensure we don't add too many characters
characters += startCol - lastCol;
} // Append the highlight indicators
1 year ago
highlightLine += highlighter('^'.repeat(characters)); // Set the lastCol equal to character count between start of line part and highlight end-column
1 year ago
lastCol = endCol + 1;
} // There's no point in processing more highlights if we reached the end of the line
1 year ago
if (endCol >= lineEndCol - 1) {
break;
}
} // Append the highlight message if the current highlights ends on this line part
1 year ago
if (highlight && highlight.message && highlightHasEnded) {
highlightLine += ' ' + highlighter(highlight.message, true);
}
}
1 year ago
if (highlightLine) {
resultLines.push(lineNumberPrefixer({
lineNumberLength,
isHighlighted: true
}) + highlightLine);
}
1 year ago
}
1 year ago
return resultLines.join('\n');
}