// One size vocabulary across the app (KB, MB, GB, binary multiples), as in the explorer and plans. export const humanFileSize = value => String(window.humanFileSize(value)).replace(/([KMGTPEZY])iB$/, "$1B").replace(/\.0 ([KMGTPEZY]?B)$/, " $1"); export const bigNum = (function () { return function bigNum(v) { const n = Number(v) || 0; const abs = Math.abs(n); if (abs < 1000) return String(n); if (abs < 10000) return (n / 1000).toFixed(1).replace(/\.0$/, "") + "k"; if (abs < 1000000) return Math.round(n / 1000) + "k"; if (abs < 10000000) return (n / 1000000).toFixed(1).replace(/\.0$/, "") + "M"; return Math.round(n / 1000000) + "M"; }; })(); export const humanTime = (function () { return function humanTime(seconds) { if (!seconds) { return "never"; } var when = null; if (seconds instanceof Date) { when = seconds; seconds = Math.round((Date.now() - seconds) / 1000); } if (typeof seconds == "string" || typeof seconds == "number") { when = new Date(seconds); seconds = Math.round((Date.now() - when) / 1000); } var suffix = seconds < 0 ? "from now" : "ago"; // more than 2 days ago display Date. Spell the month out so the date // is unambiguous regardless of the reader's locale (9/6 vs 6/9). if (Math.abs(seconds) > 2 * 60 * 60 * 24) { const now = when && !isNaN(when) ? when : new Date(Date.now() - seconds * 1000); return ( now.toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric", }) ); } seconds = Math.abs(seconds); var times = [ seconds / 60 / 60 / 24 / 365, // years seconds / 60 / 60 / 24 / 30, // months seconds / 60 / 60 / 24 / 7, // weeks seconds / 60 / 60 / 24, // days seconds / 60 / 60, // hours seconds / 60, // minutes seconds, // seconds ]; var names = ["year", "month", "week", "day", "hour", "minute", "second"]; for (var i = 0; i < names.length; i++) { var time = Math.floor(times[i]); var name = names[i]; if (time > 1) name += "s"; if (time >= 1) return time + " " + name + " " + suffix; } return "0 seconds " + suffix; }; })(); export const title = (function () { return function (str) { if (!str) return str; str = str.toLowerCase(); var words = str.split(" "); var capitalized = words.map(function (word) { return word.charAt(0).toUpperCase() + word.substring(1, word.length); }); return capitalized.join(" "); }; })(); export const statusLabel = (function () { var labels = { ready: "Ready", error: "Error", expired: "Expired", expiring: "Expiring", removed: "Removed", removing: "Removing", queue: "Queued", download: "Downloading", downloaded: "Downloaded", preparing: "Preparing", anonymizing: "Anonymizing", }; return function (status) { if (!status) return ""; if (labels[status]) return labels[status]; var s = String(status).replace(/[_-]+/g, " ").toLowerCase(); return s.charAt(0).toUpperCase() + s.slice(1); }; })(); export const statusMsg = (function () { // Known machine codes → sentences. Unknown snake_case codes are // converted to a sentence instead of leaking `branch_not_found`. var codes = { branch_not_found: "Branch not found on GitHub", repo_not_found: "Repository not found on GitHub", repository_not_found: "Repository not found on GitHub", repo_not_accessible: "Repository is not accessible with your token", pr_not_found: "Pull request not found on GitHub", gist_not_found: "Gist not found on GitHub", commit_not_found: "Commit not found on GitHub", repo_too_big: "Repository exceeds the size limit", repository_too_big: "Repository exceeds the size limit", quota_exceeded: "Hosting allowance exceeded", quota_repository_limit: "Repository limit reached", quota_storage_limit: "Storage allowance exceeded", incomplete_record: "Incomplete record: missing identifier", }; return function (msg) { if (!msg) return msg; var m = msg.match(/^rate_limited:(\d+)$/); if (m) { var remaining = Math.max(0, Math.ceil((parseInt(m[1], 10) - Date.now()) / 1000)); if (remaining <= 0) return "Rate limited — resuming soon"; var min = Math.floor(remaining / 60); var sec = remaining % 60; return "Rate limited — retrying in " + (min > 0 ? min + "m " + sec + "s" : sec + "s"); } if (codes[msg]) return codes[msg]; // "code: server detail" → the plain sentence for the code; the raw text stays in the tooltip. var coded = /^([a-z0-9]+(?:_[a-z0-9]+)+):\s*\S/.exec(msg); if (coded) { if (codes[coded[1]]) return codes[coded[1]]; var t = coded[1].replace(/_/g, " "); return t.charAt(0).toUpperCase() + t.slice(1); } if (/^[a-z0-9]+(_[a-z0-9]+)+$/.test(msg)) { var s = msg.replace(/_/g, " "); return s.charAt(0).toUpperCase() + s.slice(1); } return msg; }; })(); // A unified diff split into files: paths, status, +/- counts and numbered lines. export function parseDiff(str) { const files = []; if (!str) return files; // oldLeft/newLeft: lines still due in the current hunk. Inside a hunk a // removed "-- x" or added "++ x" line is content, not a "---"/"+++" header. let file = null, oldNo = 0, newNo = 0, oldLeft = 0, newLeft = 0; const finish = () => { if (!file || !(file.lines.length || file.oldPath || file.newPath)) { file = null; return; } file.status = file.oldPath === "/dev/null" ? "added" : file.newPath === "/dev/null" ? "deleted" : file.oldPath && file.newPath && file.oldPath !== file.newPath ? "renamed" : "modified"; file.path = file.newPath && file.newPath !== "/dev/null" ? file.newPath : file.oldPath || ""; file.additions = file.lines.filter((l) => l.kind === "add").length; file.deletions = file.lines.filter((l) => l.kind === "remove").length; files.push(file); file = null; }; const ensureFile = () => (file = file || { oldPath: "", newPath: "", lines: [] }); // The diff's final newline is not a context line. const lines = str.replace(/\n$/, "").split("\n"); for (const ln of lines) { if (file && (oldLeft > 0 || newLeft > 0) && /^([-+ ]|$)/.test(ln)) { if (ln.startsWith("+")) { file.lines.push({ kind: "add", oldNo: "", newNo: newNo++, text: ln.slice(1) }); newLeft--; } else if (ln.startsWith("-")) { file.lines.push({ kind: "remove", oldNo: oldNo++, newNo: "", text: ln.slice(1) }); oldLeft--; } else { file.lines.push({ kind: "ctx", oldNo: oldNo++, newNo: newNo++, text: ln.slice(1) }); oldLeft--; newLeft--; } continue; } if (ln.startsWith("diff --git")) { finish(); ensureFile(); continue; } if (ln.startsWith("--- ")) { // New file boundary if the previous file already had lines. if (file && file.lines.length) finish(); ensureFile().oldPath = ln.replace(/^--- (a\/)?/, "").trim(); continue; } if (ln.startsWith("+++ ")) { ensureFile().newPath = ln.replace(/^\+\+\+ (b\/)?/, "").trim(); continue; } if (/^(index |similarity index|rename |new file mode|deleted file mode|Binary files)/.test(ln)) continue; if (ln.startsWith("@@")) { const m = ln.match(/@@\s+-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s+@@/); if (m) { oldNo = parseInt(m[1], 10); newNo = parseInt(m[3], 10); oldLeft = m[2] == null ? 1 : parseInt(m[2], 10); newLeft = m[4] == null ? 1 : parseInt(m[4], 10); } ensureFile().lines.push({ kind: "hunk", oldNo: "", newNo: "", text: ln }); continue; } if (!file) continue; if (ln.startsWith("+")) file.lines.push({ kind: "add", oldNo: "", newNo: newNo++, text: ln.slice(1) }); else if (ln.startsWith("-")) file.lines.push({ kind: "remove", oldNo: oldNo++, newNo: "", text: ln.slice(1) }); else if (ln.startsWith("\\")) continue; // "\ No newline at end of file" else file.lines.push({ kind: "ctx", oldNo: oldNo++, newNo: newNo++, text: ln.startsWith(" ") ? ln.slice(1) : ln }); } finish(); return files; } // CSV/TSV text as rows of cells (quoted fields, doubled quotes, CRLF). export const parseDelimited = (text, sep) => { const rows = []; let row = [], cell = "", quoted = false; for (let i = 0; i < text.length; i++) { const ch = text[i]; if (quoted) { if (ch === '"') { if (text[i + 1] === '"') { cell += '"'; i++; } else quoted = false; } else cell += ch; } else if (ch === '"' && cell === "") quoted = true; else if (ch === sep) { row.push(cell); cell = ""; } else if (ch === "\n" || ch === "\r") { if (ch === "\r" && text[i + 1] === "\n") i++; row.push(cell); rows.push(row); row = []; cell = ""; } else cell += ch; } if (cell !== "" || row.length) { row.push(cell); rows.push(row); } return rows.filter((r) => r.length > 1 || r[0] !== ""); }; export const hangIndent = (text) => Math.min(((text || "").match(/^[ \t]*/)[0] || "").replace(/\t/g, " ").length + 4, 24); export const diff = (function () { const esc = (s) => s.replace(/&/g, "&").replace(//g, ">"); const renderFile = (file) => { const out = ['
| ' + (line.oldNo || "") + " | " + '' + (line.newNo || "") + " | " + '' + (line.kind === "add" ? "+" : line.kind === "remove" ? "-" : "") + " | " + '' + esc(line.text) + " |