This commit is contained in:
alex wiesner
2026-04-09 11:31:06 +01:00
parent 8fec8e28f4
commit 18245c778e
155 changed files with 16206 additions and 2980 deletions

View File

@@ -76,3 +76,59 @@ export function allQuestionsAnswered(questions, answers) {
export function nextTabAfterAnswer(currentTab, questionCount) {
return currentTab < questionCount - 1 ? currentTab + 1 : questionCount;
}
function takeWrappedSegment(text, maxWidth) {
if (text.length <= maxWidth) {
return { line: text, rest: "" };
}
let breakpoint = -1;
for (let index = 0; index < maxWidth; index += 1) {
if (/\s/.test(text[index])) {
breakpoint = index;
}
}
if (breakpoint > 0) {
return {
line: text.slice(0, breakpoint).trimEnd(),
rest: text.slice(breakpoint + 1).trimStart(),
};
}
return {
line: text.slice(0, maxWidth),
rest: text.slice(maxWidth),
};
}
export function wrapPrefixedText(text, width, firstPrefix = "", continuationPrefix = firstPrefix) {
const source = String(text ?? "");
if (source.length === 0) {
return [firstPrefix];
}
const lines = [];
const blocks = source.split(/\r?\n/);
let isFirstLine = true;
for (const block of blocks) {
let remaining = block.trim();
if (remaining.length === 0) {
lines.push(isFirstLine ? firstPrefix : continuationPrefix);
isFirstLine = false;
continue;
}
while (remaining.length > 0) {
const prefix = isFirstLine ? firstPrefix : continuationPrefix;
const maxTextWidth = Math.max(1, width - prefix.length);
const { line, rest } = takeWrappedSegment(remaining, maxTextWidth);
lines.push(prefix + line);
remaining = rest;
isFirstLine = false;
}
}
return lines;
}