// human.js — the one judge of machine identifiers in labels. NO I/O. // Dual-mode: node `require` (tests, ui-test guard) + QML import. // // A machine identifier is a string a person never chose and cannot read: // temp and runtime paths, file URIs, UUIDs, hashes, generated names, D-Bus // names and object paths, desktop ids, PipeWire/ALSA nodes, device paths and // hardware addresses. Every surface that turns external data into a label // passes it through `label` (names and titles) or `text` (authored lines that // carry data); user text such as clipboard bodies stays as it is. var TEMP_PATH = /^\/(?:tmp|var\/tmp|dev\/shm|run\/user\/\d+)(?:\/|$)/; var DEVICE_PATH = /^\/(?:dev|sys|proc)(?:\/|$)/; var OBJECT_PATH = /^\/(?:org|com|net|io|dev|app|de)(?:\/[A-Za-z0-9_]+)+$/; var FILE_URI = /^file:\/\//i; var BUS_UNIQUE = /^:\d+\.\d+$/; // Reverse-DNS names with at least three parts: D-Bus names, app and desktop ids. var REVERSE_DNS = /^(?:org|com|net|io|dev|app|me|de|eu|uk|fr|ch|nl|edu|info|xyz)\.[A-Za-z0-9_-]+(?:\.[A-Za-z0-9_-]+)+$/; var DESKTOP_ID = /\.desktop$/i; var PROTOCOL_WORD = /StatusNotifierItem|NotificationItem|MediaPlayer2/i; var AUDIO_NODE = /^(?:alsa|bluez|v4l2|libcamera|jack|pipewire|pulse)_[A-Za-z0-9_]*[._]/i; var HARDWARE_ADDRESS = /^[0-9a-f]{2}([:_-])[0-9a-f]{2}(?:\1[0-9a-f]{2}){4}$/i; var UUID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i; var MKTEMP = /^tmp\.[A-Za-z0-9]{6,}$/; var INSTANCE = /(?:^|[._-])(?:instance|status_?icon)_?\d+$/i; var UNIT = /^(.+?)(?:@[^\/]*)?\.(?:service|scope|slice|socket|timer|mount|target)$/; var IMAGE_EXT = /\.(?:png|jpe?g|webp|gif|bmp|svg|avif|heic|tiff?|ico)$/i; var VIDEO_EXT = /\.(?:mp4|mkv|webm|mov|avi)$/i; var AUDIO_EXT = /\.(?:mp3|ogg|opus|flac|wav|m4a)$/i; var DOCUMENT_EXT = /\.(?:pdf|odt|docx?|txt|md)$/i; function count(text, pattern) { return (String(text).match(pattern) || []).length; } // --- one name part: a hash, base64-ish blob or random suffix --- function machineSegment(segment, compound) { var hex = /^[0-9a-f]+$/i.test(segment) && /\d/.test(segment) && /[a-f]/i.test(segment); if (hex && segment.length >= 12) return "hash"; if (segment.length >= 20 && /[A-Z]/.test(segment) && /[a-z]/.test(segment) && count(segment, /\d/g) >= 3) return "hash"; if (!compound) return ""; if (hex && segment.length >= 8) return "generated name"; if (segment.length >= 6 && count(segment, /[A-Z]/g) >= 2 && count(segment, /[a-z]/g) >= 2 && count(segment, /\d/g) >= 2) return "generated name"; return ""; } // --- what kind of machine identifier one token is, or "" for a human word --- function machineKind(token) { var value = String(token == null ? "" : token); if (value.length === 0) return ""; if (FILE_URI.test(value)) return "file uri"; if (TEMP_PATH.test(value)) return "temp path"; if (DEVICE_PATH.test(value)) return "device path"; if (OBJECT_PATH.test(value)) return "object path"; if (BUS_UNIQUE.test(value) || REVERSE_DNS.test(value) || DESKTOP_ID.test(value) || PROTOCOL_WORD.test(value)) return "bus name"; if (AUDIO_NODE.test(value)) return "audio node"; if (HARDWARE_ADDRESS.test(value)) return "hardware address"; if (UUID.test(value)) return "uuid"; if (MKTEMP.test(value) || INSTANCE.test(value)) return "generated name"; var segments = value.split(/[^A-Za-z0-9+=]+/).filter(function (part) { return part.length > 0; }); for (var i = 0; i < segments.length; i++) { var kind = machineSegment(segments[i], segments.length > 1); if (kind) return kind; } return ""; } // A phrase is never one identifier, except a file path, which may hold spaces. function wholeKind(text) { var value = String(text == null ? "" : text).trim(); if (/\s/.test(value) && !/^(?:\/|file:\/\/)[^\n]*\.[A-Za-z0-9]{1,5}$/i.test(value)) return ""; return machineKind(value); } // --- true when the whole string is a machine identifier --- function isMachineIdentifier(text) { return wholeKind(text).length > 0; } // Leading and trailing punctuation around a token in a sentence. function tokenParts(token) { var match = /^([("'`<\[{]*)(.*?)([)"'`>\]},;!?.]*)$/.exec(token); return { lead: match[1], core: match[2], tail: match[3] }; } // --- the first machine identifier anywhere in a text, or "" (the ui-test guard) --- function findMachineIdentifier(text) { var value = String(text == null ? "" : text); if (isMachineIdentifier(value)) return value.trim(); var tokens = value.split(/\s+/); for (var i = 0; i < tokens.length; i++) { var core = tokenParts(tokens[i]).core; if (machineKind(core)) return core; } return ""; } // --- the kind of a file, from its name: image, video, audio, document, screenshot or file --- function fileKind(name) { var base = String(name == null ? "" : name); if (/^screen[ _-]?shot/i.test(base)) return "screenshot"; if (IMAGE_EXT.test(base)) return "image"; if (VIDEO_EXT.test(base)) return "video"; if (AUDIO_EXT.test(base)) return "audio"; if (DOCUMENT_EXT.test(base)) return "document"; return "file"; } function decodedPath(value) { var path = String(value).replace(FILE_URI, ""); try { return decodeURIComponent(path); } catch (error) { return path; } } // --- a path's human file name, or its kind when the name was generated, or "" --- function pathLabel(path) { var trimmed = String(path).replace(/\/+$/, ""); if (TEMP_PATH.test(trimmed) && !/^\/(?:tmp|var\/tmp|dev\/shm|run\/user\/\d+)\/./.test(trimmed)) return ""; var base = trimmed.slice(trimmed.lastIndexOf("/") + 1); var stem = base.replace(/\.[A-Za-z0-9]{1,5}$/, ""); if (/[A-Za-z]/.test(stem) && !machineKind(base) && !/^\./.test(base)) return base; return base.length > 0 ? fileKind(base) : ""; } // --- the app a D-Bus name, desktop id or MPRIS name stands for, or "" --- function appName(id) { var value = String(id == null ? "" : id).replace(DESKTOP_ID, "").replace(/^org\.mpris\.MediaPlayer2\./, ""); value = value.replace(/\.instance_?\d+.*$/i, ""); var name = value.slice(value.lastIndexOf(".") + 1).replace(/(?:[-_]\d+)+$/, ""); if (!/[A-Za-z]{2}/.test(name) || PROTOCOL_WORD.test(name) || machineKind(name)) return ""; return name.replace(/[-_]+/g, " ").toLowerCase(); } // --- the app a systemd unit or its cgroup path runs, e.g. app-org.foo.Bar@.service, or "" --- function unitName(text) { var match = UNIT.exec(String(text).slice(String(text).lastIndexOf("/") + 1)); if (!match) return ""; var name = match[1].replace(/^app-/, "").replace(/[-_][0-9a-f]{8,}$/i, ""); if (REVERSE_DNS.test(name) || DESKTOP_ID.test(name)) return appName(name); return machineKind(name) ? "" : name; } // --- what a machine identifier stands for: a unit's app, a file name or kind, an app, or "" --- function derived(text, machine) { var unit = unitName(text); if (unit) return unit; if (machine === "file uri" || (text.charAt(0) === "/" && machine !== "device path" && machine !== "object path")) return pathLabel(decodedPath(text)); return machine === "bus name" ? appName(text) : ""; } // --- a human name for a name or title: the text itself, else what it stands for, else `fallback` --- function label(value, fallback) { var text = String(value == null ? "" : value).trim(); var kind = String(fallback || "item"); if (text.length === 0) return kind; var machine = wholeKind(text); return machine ? (derived(text, machine) || kind) : text; } // --- the first of `candidates` that is a human name, else what one stands for, else `fallback` --- function pick(candidates, fallback) { var list = (candidates || []).map(function (candidate) { return String(candidate == null ? "" : candidate).trim(); }); for (var i = 0; i < list.length; i++) { if (list[i].length > 0 && !isMachineIdentifier(list[i])) return list[i]; } for (var j = 0; j < list.length; j++) { var name = list[j].length > 0 ? derived(list[j], wholeKind(list[j])) : ""; if (name.length > 0) return name; } return String(fallback || "item"); } // --- a line that carries data: each machine identifier becomes its label --- function text(value, fallback) { var line = String(value == null ? "" : value); if (isMachineIdentifier(line)) return label(line, fallback); return line.split(/(\s+)/).map(function (token) { if (/^\s*$/.test(token)) return token; var parts = tokenParts(token); return machineKind(parts.core) ? parts.lead + label(parts.core, fallback) + parts.tail : token; }).join(""); } // Vertical workspace labels: drop vowels, use three original characters if short. function abbreviate(text) { var value = String(text); var stripped = value.replace(/[aeiou]/gi, ""); return (stripped.length >= 3 ? stripped : value).slice(0, 3); } if (typeof module !== 'undefined' && module.exports) { module.exports = { machineKind: machineKind, isMachineIdentifier: isMachineIdentifier, findMachineIdentifier: findMachineIdentifier, fileKind: fileKind, appName: appName, label: label, pick: pick, text: text, abbreviate: abbreviate }; }