From a4664d02915cd5db23ed28e3428b420e482747cf Mon Sep 17 00:00:00 2001 From: HiImKobeAnd Date: Wed, 29 Jul 2026 21:48:20 +0200 Subject: [PATCH] fix(audio-switcher): fix an error printed to logs and change widget color to default (#149) * fix(audio-switcher): remove json decode which parsed a table * fix(audio-switcher): use default widget coloring * chore(audio-switcher): bump version to 0.2.1 * fix(audio-switcher): parse pactl volume output safely --------- Co-authored-by: WojciechSulocki-Gif --- audio-switcher/plugin.toml | 2 +- audio-switcher/service.luau | 32 +++++++++++++++++++++++++------- audio-switcher/widget.luau | 2 +- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/audio-switcher/plugin.toml b/audio-switcher/plugin.toml index 028c848..2724ccb 100644 --- a/audio-switcher/plugin.toml +++ b/audio-switcher/plugin.toml @@ -1,6 +1,6 @@ id = "blackbartblues/audio-switcher" name = "Audio Switcher" -version = "0.2.0" +version = "0.2.1" plugin_api = 9 author = "blackbartblues" license = "MIT" diff --git a/audio-switcher/service.luau b/audio-switcher/service.luau index af95896..8fb4397 100644 --- a/audio-switcher/service.luau +++ b/audio-switcher/service.luau @@ -97,7 +97,7 @@ local function runCommand(args, callback, timeoutMs) end local function runPactl(args, callback, timeoutMs) - local command = { "pactl" } + local command = { "env", "LC_ALL=C", "pactl" } for _, value in ipairs(args) do table.insert(command, value) end return runCommand(command, callback, timeoutMs) end @@ -205,6 +205,25 @@ local function firstVolumePercent(volume) return 0 end +local function parseCurrentVolume(output) + local decoded = noctalia.json.decode(tostring(output or "")) + if type(decoded) == "table" and type(decoded.volume) == "table" then + return firstVolumePercent(decoded.volume) + end + return tonumber(tostring(output or ""):match("(%d+)%%")) +end + +local function parseCurrentMute(output) + local decoded = noctalia.json.decode(tostring(output or "")) + if type(decoded) == "table" and type(decoded.mute) == "boolean" then + return decoded.mute + end + local value = tostring(output or ""):match("Mute:%s*(%a+)") + if value == "yes" then return true end + if value == "no" then return false end + return nil +end + local function propertiesOf(item) return type(item.properties) == "table" and item.properties or {} end @@ -508,23 +527,22 @@ local function loadCurrentVolume(kind, callback) callback(false) return end - local volumeData = decodeJson(volumeResult.stdout) - if volumeData == nil or type(volumeData.volume) ~= "table" then + local volume = parseCurrentVolume(volumeResult.stdout) + if volume == nil then callback(false) return end - local volume = firstVolumePercent(volumeData.volume) runPactl({ "-f", "json", muteCommand, target }, function(muteResult) if muteResult.exitCode ~= 0 then callback(false) return end - local muteData = decodeJson(muteResult.stdout) - if muteData == nil then + local muted = parseCurrentMute(muteResult.stdout) + if muted == nil then callback(false) return end - callback(true, volume, muteData.mute == true) + callback(true, volume, muted) end) end) end diff --git a/audio-switcher/widget.luau b/audio-switcher/widget.luau index bcce3f4..04bcea4 100644 --- a/audio-switcher/widget.luau +++ b/audio-switcher/widget.luau @@ -45,7 +45,7 @@ local function render() if snapshot.busy == true then barWidget.setGlyphColor("secondary") elseif snapshot.available == true then - barWidget.setGlyphColor("primary") + barWidget.setGlyphColor("default") else barWidget.setGlyphColor("error") end