From 38dbee33de8d165a9ec600373efbd4540c4e74ac Mon Sep 17 00:00:00 2001 From: Thomas Becker Date: Sat, 27 Apr 2024 09:14:49 +0200 Subject: [PATCH 1/2] add brew widget 'cause I missed it in the lua config. Thx Felix for all the awesome work you do! --- .config/sketchybar/items/widgets/brew.lua | 79 +++++++++++++++++++++++ .config/sketchybar/items/widgets/init.lua | 1 + 2 files changed, 80 insertions(+) create mode 100644 .config/sketchybar/items/widgets/brew.lua diff --git a/.config/sketchybar/items/widgets/brew.lua b/.config/sketchybar/items/widgets/brew.lua new file mode 100644 index 0000000..941c853 --- /dev/null +++ b/.config/sketchybar/items/widgets/brew.lua @@ -0,0 +1,79 @@ +local icons = require("icons") +local colors = require("colors") +local settings = require("settings") + +local thresholds = { + -- default color green will be returned if count is less than the first threshold + [3] = colors.yellow, + [5] = colors.orange, + [10] = colors.red, +} + +local brew = sbar.add("item", "widgets.brew", { + position = "right", + drawing = "off", + icon = { + string = icons.brew.empty, + color = colors.green, + align = "left", + }, + label = { + string = "0", + font = { family = settings.font.numbers }, + }, + updates = "on", + update_freq = 10, +}) + +function GetThresholdColor(count) + local thresholdKeys = {} + for key in pairs(thresholds) do + table.insert(thresholdKeys, key) + end + + table.sort(thresholdKeys, function(a, b) + return a > b + end) + + for _, threshold in ipairs(thresholdKeys) do + if tonumber(count) >= threshold then + return thresholds[threshold] + end + end + return colors.green +end + +brew:subscribe({ "routine", "brew_update" }, function() + sbar.exec("brew outdated | wc -l | tr -d ' '", function(brew_outdated) + local icon = icons.brew.empty + local color = colors.green + local label = "0" + local count = brew_outdated + local drawing = "off" + + if tonumber(count) > 0 then + icon = icons.brew.full + label = count + color = GetThresholdColor(count) + drawing = "on" + end + --print("color: ", color, "count: ", count, "icon: ", icon, "label: ", label, "drawing: ", drawing) + brew:set({ + icon = { + string = icon, + color = color, + }, + drawing = drawing, + label = { string = label }, + }) + end) +end) + +sbar.add("bracket", "widgets.brew.bracket", { brew.name }, { + background = { color = colors.bg1 }, +}) + +sbar.add("item", "widgets.brew.padding", { + position = "right", + width = settings.group_paddings, +}) diff --git a/.config/sketchybar/items/widgets/init.lua b/.config/sketchybar/items/widgets/init.lua index c919c76..0826bd8 100644 --- a/.config/sketchybar/items/widgets/init.lua +++ b/.config/sketchybar/items/widgets/init.lua @@ -2,3 +2,4 @@ require("items.widgets.battery") require("items.widgets.volume") require("items.widgets.wifi") require("items.widgets.cpu") +require("items.widgets.brew") From 40cc1bb07f1b087969f90fa03c3bb2de8d1e3b0a Mon Sep 17 00:00:00 2001 From: Thomas Becker Date: Sun, 28 Apr 2024 07:50:50 +0200 Subject: [PATCH 2/2] refactor GetThresholdColor to a local function --- .config/sketchybar/items/widgets/brew.lua | 34 +++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/.config/sketchybar/items/widgets/brew.lua b/.config/sketchybar/items/widgets/brew.lua index 941c853..c73a22a 100644 --- a/.config/sketchybar/items/widgets/brew.lua +++ b/.config/sketchybar/items/widgets/brew.lua @@ -25,26 +25,26 @@ local brew = sbar.add("item", "widgets.brew", { update_freq = 10, }) -function GetThresholdColor(count) - local thresholdKeys = {} - for key in pairs(thresholds) do - table.insert(thresholdKeys, key) - end +brew:subscribe({ "routine", "brew_update" }, function() + sbar.exec("brew outdated | wc -l | tr -d ' '", function(brew_outdated) + local function getThresholdColor(count) + local thresholdKeys = {} + for key in pairs(thresholds) do + table.insert(thresholdKeys, key) + end - table.sort(thresholdKeys, function(a, b) - return a > b - end) + table.sort(thresholdKeys, function(a, b) + return a > b + end) - for _, threshold in ipairs(thresholdKeys) do - if tonumber(count) >= threshold then - return thresholds[threshold] + for _, threshold in ipairs(thresholdKeys) do + if tonumber(count) >= threshold then + return thresholds[threshold] + end + end + return colors.green end - end - return colors.green -end -brew:subscribe({ "routine", "brew_update" }, function() - sbar.exec("brew outdated | wc -l | tr -d ' '", function(brew_outdated) local icon = icons.brew.empty local color = colors.green local label = "0" @@ -54,7 +54,7 @@ brew:subscribe({ "routine", "brew_update" }, function() if tonumber(count) > 0 then icon = icons.brew.full label = count - color = GetThresholdColor(count) + color = getThresholdColor(count) drawing = "on" end --print("color: ", color, "count: ", count, "icon: ", icon, "label: ", label, "drawing: ", drawing)