repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

render polkit command as code block

d47a5361d2f3d4631fc4453494f723f1d62de428

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (4)

+62 / −3 text lines

Diff layout

quickshell/nuguland/privilege-ui.test.mjs

Modified

+2 / −0 text lines

99const surface = qml.slice(start, end);
1111assert.match(surface, /id:\s*commandText/, "privilege surface has dedicated command text");
12assert.match(surface, /PrivilegeUi\.commandMarkdown/, "command text uses markdown-style formatter");
13assert.match(surface, /textFormat:\s*Text\.RichText/, "command text renders rich text");
1214assert.match(surface, /elide:\s*Text\.ElideNone/, "command text must not truncate");
1315assert.match(surface, /secretInput\.forceActiveFocus\(\)/, "password input is explicitly focused");
1416assert.match(surface, /id:\s*focusRetry/, "focus retry timer exists for map-time focus races");

quickshell/nuguland/privilege.js

Added
new file mode 100644

+39 / −0 text lines

1// privilege.js — pure rendering helpers for the Nuguland polkit surface.
2// Dual-mode: node `require` tests + QML import.
3
4function escapeHtml(value) {
5 return String(value == null ? "" : value)
6 .replace(/&/g, "&amp;")
7 .replace(/</g, "&lt;")
8 .replace(/>/g, "&gt;")
9 .replace(/"/g, "&quot;");
10}
12function commandFromMessage(message) {
13 var text = String(message || "").trim();
14 var match = /^Authentication is needed to run\s+(.+?)\s+as the super user\.?$/i.exec(text);
15 return match ? match[1] : text;
16}
18function commandLines(command) {
19 return String(command || "")
20 .split(/\s+&&\s+/)
21 .map(function (line) { return line.trim(); })
22 .filter(function (line) { return line.length > 0; });
23}
25function commandMarkdown(message) {
26 var lines = commandLines(commandFromMessage(message));
27 if (lines.length === 0) lines = ["A system change needs your password."];
28 return "<pre style=\"font-family:'IBM Plex Mono'; font-size:13px; line-height:1.35; white-space:pre-wrap; margin:0\">" +
29 lines.map(escapeHtml).join("\n") +
30 "</pre>";
31}
33if (typeof module !== 'undefined' && module.exports) {
34 module.exports = {
35 escapeHtml: escapeHtml,
36 commandFromMessage: commandFromMessage,
37 commandMarkdown: commandMarkdown
38 };
39}

quickshell/nuguland/privilege.test.js

Added
new file mode 100644

+18 / −0 text lines

1var assert = require('assert');
2var P = require('./privilege.js');
3
4var 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";
5
6assert.strictEqual(P.commandFromMessage(message), "/usr/bin/sh -c pacman -S --needed --noconfirm ddcutil && modprobe i2c-dev && udevadm trigger --subsystem-match=i2c-dev");
7assert.strictEqual(P.commandFromMessage("Authentication required"), "Authentication required");
8
9var html = P.commandMarkdown(message);
10assert(html.indexOf("<pre") >= 0, "renders a code block");
11assert(html.indexOf("/usr/bin/sh -c") >= 0, "keeps shell command visible");
12assert(html.indexOf("pacman -S --needed --noconfirm ddcutil") >= 0, "splits chained command onto readable line");
13assert(html.indexOf("modprobe i2c-dev") >= 0, "shows modprobe line");
14assert(html.indexOf("udevadm trigger --subsystem-match=i2c-dev") >= 0, "shows udevadm line");
15assert(html.indexOf("&&") < 0, "does not leave && noise in formatted command");
16assert.strictEqual(P.escapeHtml("<x>&\""), "&lt;x&gt;&amp;&quot;");
18console.log('privilege.test.js: ALL PASS');

quickshell/nuguland/shell.qml

Modified

+3 / −3 text lines

66import Quickshell.Wayland
77import Quickshell.Widgets
88import Quickshell.Services.UPower
9import "privilege.js" as PrivilegeUi
1011ShellRoot {
1112 id: shell
15061507 anchors.right: parent.right
15071508 anchors.top: parent.top
15081509 anchors.margins: 10
1509 text: root.request.message || "A system change needs your password."
1510 text: PrivilegeUi.commandMarkdown(root.request.message || "A system change needs your password.")
15101511 color: root.theme ? root.theme.content.normal : "white"
1511 font.family: "IBM Plex Sans"
1512 font.pixelSize: 14
1512 textFormat: Text.RichText
15131513 wrapMode: Text.WordWrap
15141514 elide: Text.ElideNone