Skip to content

Commit

Permalink
sketchybar: use sbar.exec callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
khaneliman committed Feb 25, 2024
1 parent e1aa18e commit dd3f231
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,48 +52,48 @@ local function low_battery_label(percent)
end

local function battery_update()
local file = assert(io.popen("pmset -g batt"))
local batt_info = assert(file:read("a"))
local icon = "!"
local color = colors.green
local charging = string.find(batt_info, 'AC Power')
sbar.exec("pmset -g batt", function(batt_info)
local icon = "!"
local color = colors.green
local charging = string.find(batt_info, 'AC Power')

local thresholds = {
{ percent = 100, charging_icon = icons.battery.charging._100, non_charging_icon = icons.battery.non_charging._100, color = colors.green },
{ percent = 90, charging_icon = icons.battery.charging._90, non_charging_icon = icons.battery.non_charging._90, color = colors.green },
{ percent = 80, charging_icon = icons.battery.charging._80, non_charging_icon = icons.battery.non_charging._80, color = colors.green },
{ percent = 70, charging_icon = icons.battery.charging._70, non_charging_icon = icons.battery.non_charging._70, color = colors.green },
{ percent = 60, charging_icon = icons.battery.charging._60, non_charging_icon = icons.battery.non_charging._60, color = colors.yellow },
{ percent = 50, charging_icon = icons.battery.charging._50, non_charging_icon = icons.battery.non_charging._50, color = colors.yellow },
{ percent = 40, charging_icon = icons.battery.charging._40, non_charging_icon = icons.battery.non_charging._40, color = colors.peach },
{ percent = 30, charging_icon = icons.battery.charging._30, non_charging_icon = icons.battery.non_charging._30, color = colors.peach },
{ percent = 20, charging_icon = icons.battery.charging._20, non_charging_icon = icons.battery.non_charging._20, color = colors.red },
{ percent = 10, charging_icon = icons.battery.charging._10, non_charging_icon = icons.battery.non_charging._10, color = colors.red },
{ percent = 0, charging_icon = icons.battery.charging._0, non_charging_icon = icons.battery.non_charging._0, color = colors.red }
}
local thresholds = {
{ percent = 100, charging_icon = icons.battery.charging._100, non_charging_icon = icons.battery.non_charging._100, color = colors.green },
{ percent = 90, charging_icon = icons.battery.charging._90, non_charging_icon = icons.battery.non_charging._90, color = colors.green },
{ percent = 80, charging_icon = icons.battery.charging._80, non_charging_icon = icons.battery.non_charging._80, color = colors.green },
{ percent = 70, charging_icon = icons.battery.charging._70, non_charging_icon = icons.battery.non_charging._70, color = colors.green },
{ percent = 60, charging_icon = icons.battery.charging._60, non_charging_icon = icons.battery.non_charging._60, color = colors.yellow },
{ percent = 50, charging_icon = icons.battery.charging._50, non_charging_icon = icons.battery.non_charging._50, color = colors.yellow },
{ percent = 40, charging_icon = icons.battery.charging._40, non_charging_icon = icons.battery.non_charging._40, color = colors.peach },
{ percent = 30, charging_icon = icons.battery.charging._30, non_charging_icon = icons.battery.non_charging._30, color = colors.peach },
{ percent = 20, charging_icon = icons.battery.charging._20, non_charging_icon = icons.battery.non_charging._20, color = colors.red },
{ percent = 10, charging_icon = icons.battery.charging._10, non_charging_icon = icons.battery.non_charging._10, color = colors.red },
{ percent = 0, charging_icon = icons.battery.charging._0, non_charging_icon = icons.battery.non_charging._0, color = colors.red }
}

local found, _, charge = batt_info:find("(%d+)%%")
if found then
percent = tonumber(charge)
end
local found, _, charge = batt_info:find("(%d+)%%")
if found then
percent = tonumber(charge)
end

for _, threshold in ipairs(thresholds) do
if percent >= threshold.percent then
icon = charging and threshold.charging_icon or threshold.non_charging_icon
color = threshold.color
break
for _, threshold in ipairs(thresholds) do
if percent >= threshold.percent then
icon = charging and threshold.charging_icon or threshold.non_charging_icon
color = threshold.color
break
end
end
end

battery:set({
icon = {
string = icon,
color = color
},
label = percent .. "%"
})
battery:set({
icon = {
string = icon,
color = color
},
label = percent .. "%"
})

low_battery_label(percent)
low_battery_label(percent)
end)
end

battery:subscribe({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,102 +45,102 @@ bluetooth:subscribe("mouse.clicked", function()
end)

bluetooth:subscribe({ "routine", "forced" }, function()
local state = io.popen("blueutil -p"):read("*a")

if tonumber(state) == 0 then
bluetooth:set({ icon = icons.bluetooth_off })
else
bluetooth:set({ icon = icons.bluetooth })
end
sbar.exec("blueutil -p", function(state)
if tonumber(state) == 0 then
bluetooth:set({ icon = icons.bluetooth_off })
else
bluetooth:set({ icon = icons.bluetooth })
end

-- Get paired and connected devices
local paired = io.popen("blueutil --paired"):read("*a")
local connected = io.popen("blueutil --connected"):read("*a")
-- Get paired and connected devices
local paired = io.popen("blueutil --paired"):read("*a")
local connected = io.popen("blueutil --connected"):read("*a")

-- Count paired and connected devices
local countPaired = select(2, paired:gsub("\n", "")) -- Count the number of newlines in the paired string
local countConnected = select(2, connected:gsub("\n", "")) -- Count the number of newlines in the connected string
-- Count paired and connected devices
local countPaired = select(2, paired:gsub("\n", "")) -- Count the number of newlines in the paired string
local countConnected = select(2, connected:gsub("\n", "")) -- Count the number of newlines in the connected string

-- bluetooth:set({ label = countPaired })
-- bluetooth:set({ label = countPaired })

-- Clear existing devices in tooltip
local existingEvents = bluetooth:query()
if existingEvents.popup and next(existingEvents.popup.items) ~= nil then
for _, item in pairs(existingEvents.popup.items) do
sbar.remove(item)
-- Clear existing devices in tooltip
local existingEvents = bluetooth:query()
if existingEvents.popup and next(existingEvents.popup.items) ~= nil then
for _, item in pairs(existingEvents.popup.items) do
sbar.remove(item)
end
end
end

local bluetooth_paired_header = sbar.add("item", "bluetooth_paired_header", {
icon = {
drawing = false
},
label = {
string = "Paired Devices",
font = {
family = settings.font,
size = 14.0,
style = "Bold"
},
},
position = "popup." .. bluetooth.name,
click_script = "sketchybar --set $NAME popup.drawing=off",
})

-- Iterate over the list of paired devices
for device in paired:gmatch("[^\n]+") do
local label = device:match('"(.*)"')
local bluetooth_paired_device = sbar.add("item", "bluetooth_paired_device_" .. label, {
local bluetooth_paired_header = sbar.add("item", "bluetooth_paired_header", {
icon = {
drawing = false
},
label = {
string = label,
string = "Paired Devices",
font = {
family = settings.font,
size = 13.0,
style = "Regular"
size = 14.0,
style = "Bold"
},
},
position = "popup." .. bluetooth.name,
click_script = "sketchybar --set $NAME popup.drawing=off",
})
end

local bluetooth_connected_header = sbar.add("item", "bluetooth_connected_header", {
icon = {
drawing = false
},
label = {
string = "Connected Devices",
font = {
family = settings.font,
size = 14.0,
style = "Bold"
},
},
position = "popup." .. bluetooth.name,
click_script = "sketchybar --set $NAME popup.drawing=off",
})
-- Iterate over the list of paired devices
for device in paired:gmatch("[^\n]+") do
local label = device:match('"(.*)"')
local bluetooth_paired_device = sbar.add("item", "bluetooth_paired_device_" .. label, {
icon = {
drawing = false
},
label = {
string = label,
font = {
family = settings.font,
size = 13.0,
style = "Regular"
},
},
position = "popup." .. bluetooth.name,
click_script = "sketchybar --set $NAME popup.drawing=off",
})
end

for device in connected:gmatch("[^\n]+") do
local label = device:match('"(.*)"')
local bluetooth_connected_device = sbar.add("item", "bluetooth_connected_device_" .. label, {
local bluetooth_connected_header = sbar.add("item", "bluetooth_connected_header", {
icon = {
drawing = false
},
label = {
string = label,
string = "Connected Devices",
font = {
family = settings.font,
size = 13.0,
style = "Regular"
size = 14.0,
style = "Bold"
},
},
position = "popup." .. bluetooth.name,
click_script = "sketchybar --set $NAME popup.drawing=off",
})
end

for device in connected:gmatch("[^\n]+") do
local label = device:match('"(.*)"')
local bluetooth_connected_device = sbar.add("item", "bluetooth_connected_device_" .. label, {
icon = {
drawing = false
},
label = {
string = label,
font = {
family = settings.font,
size = 13.0,
style = "Regular"
},
},
position = "popup." .. bluetooth.name,
click_script = "sketchybar --set $NAME popup.drawing=off",
})
end
end)
end)

return bluetooth
Original file line number Diff line number Diff line change
Expand Up @@ -64,53 +64,53 @@ brew:subscribe({
}

-- fetch new information
os.execute("command brew update")
local outdated = io.popen("command brew outdated")
:read("*a")
local count = 0
for _ in outdated:gmatch("\n") do
count = count + 1
end

-- Clear existing packages
local existingPackages = brew:query()
if existingPackages.popup and next(existingPackages.popup.items) ~= nil then
for _, item in pairs(existingPackages.popup.items) do
-- os.execute("sketchybar --remove " .. item)
sbar.remove(item)
sbar.exec("command brew update")
sbar.exec("command brew outdated", function(outdated)
local count = 0
for _ in outdated:gmatch("\n") do
count = count + 1
end
end

-- Add packages to popup
for package in outdated:gmatch("[^\n]+") do
local brew_package = sbar.add("item", "brew_" .. package, {
label = {
string = tostring(package),
align = "right",
padding_right = 20,
padding_left = 20
},
icon = {
string = tostring(package),
drawing = false
},
click_script = popup_off,
position = "popup." .. brew.name
})
end
-- Clear existing packages
local existingPackages = brew:query()
if existingPackages.popup and next(existingPackages.popup.items) ~= nil then
for _, item in pairs(existingPackages.popup.items) do
-- os.execute("sketchybar --remove " .. item)
sbar.remove(item)
end
end

-- Change icon and color depending on packages
for _, threshold in ipairs(thresholds) do
if count >= threshold.count then
brew:set({
-- Add packages to popup
for package in outdated:gmatch("[^\n]+") do
local brew_package = sbar.add("item", "brew_" .. package, {
label = {
string = tostring(package),
align = "right",
padding_right = 20,
padding_left = 20
},
icon = {
color = threshold.color
string = tostring(package),
drawing = false
},
label = count
click_script = popup_off,
position = "popup." .. brew.name
})
break
end
end

-- Change icon and color depending on packages
for _, threshold in ipairs(thresholds) do
if count >= threshold.count then
brew:set({
icon = {
color = threshold.color
},
label = count
})
break
end
end
end)
end)

return brew
Loading

0 comments on commit dd3f231

Please sign in to comment.