render polkit command as code block
d47a5361d2f3d4631fc4453494f723f1d62de428
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: a023a8d8fc2f
bugabingas dorkfiles
d47a5361d2f3d4631fc4453494f723f1d62de428
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+62 / −3 text lines
+2 / −0 text lines
new file mode 100644+39 / −0 text lines
// privilege.js — pure rendering helpers for the Nuguland polkit surface.// Dual-mode: node `require` tests + QML import.function escapeHtml(value) { return String(value == null ? "" : value) .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, """);}function commandFromMessage(message) { var text = String(message || "").trim(); var match = /^Authentication is needed to run\s+(.+?)\s+as the super user\.?$/i.exec(text); return match ? match[1] : text;}function commandLines(command) { return String(command || "") .split(/\s+&&\s+/) .map(function (line) { return line.trim(); }) .filter(function (line) { return line.length > 0; });}function commandMarkdown(message) { var lines = commandLines(commandFromMessage(message)); if (lines.length === 0) lines = ["A system change needs your password."]; return "<pre style=\"font-family:'IBM Plex Mono'; font-size:13px; line-height:1.35; white-space:pre-wrap; margin:0\">" + lines.map(escapeHtml).join("\n") + "</pre>";}if (typeof module !== 'undefined' && module.exports) { module.exports = { escapeHtml: escapeHtml, commandFromMessage: commandFromMessage, commandMarkdown: commandMarkdown };}new file mode 100644+18 / −0 text lines
var assert = require('assert');var P = require('./privilege.js');var message = "Authentication is needed to run /usr/bin/sh -c pacman -S --needed --noconfirm ddcutil && modprobe i2c-dev && udevadm trigger --subsystem-match=i2c-dev as the super user";assert.strictEqual(P.commandFromMessage(message), "/usr/bin/sh -c pacman -S --needed --noconfirm ddcutil && modprobe i2c-dev && udevadm trigger --subsystem-match=i2c-dev");assert.strictEqual(P.commandFromMessage("Authentication required"), "Authentication required");var html = P.commandMarkdown(message);assert(html.indexOf("<pre") >= 0, "renders a code block");assert(html.indexOf("/usr/bin/sh -c") >= 0, "keeps shell command visible");assert(html.indexOf("pacman -S --needed --noconfirm ddcutil") >= 0, "splits chained command onto readable line");assert(html.indexOf("modprobe i2c-dev") >= 0, "shows modprobe line");assert(html.indexOf("udevadm trigger --subsystem-match=i2c-dev") >= 0, "shows udevadm line");assert(html.indexOf("&&") < 0, "does not leave && noise in formatted command");assert.strictEqual(P.escapeHtml("<x>&\""), "<x>&"");console.log('privilege.test.js: ALL PASS');+3 / −3 text lines