initalize

This commit is contained in:
xenomxrph
2025-07-16 15:00:51 +02:00
commit 8d16baf6f1
40 changed files with 2353 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
local constants = require("constants")
local settings = require("config.settings")
local isCharging = false
local battery = sbar.add("item", constants.items.battery, {
position = "right",
update_freq = 60,
})
local batteryPopup = sbar.add("item", {
position = "popup." .. battery.name,
width = "dynamic",
label = {
padding_right = settings.dimens.padding.label,
padding_left = settings.dimens.padding.label,
},
icon = {
padding_left = 0,
padding_right = 0,
},
})
battery:subscribe({ "routine", "power_source_change", "system_woke" }, function()
sbar.exec("pmset -g batt", function(batteryInfo)
local icon = "!"
local label = "?"
local found, _, charge = batteryInfo:find("(%d+)%%")
if found then
charge = tonumber(charge)
label = charge .. "%"
end
local color = settings.colors.green
local charging, _, _ = batteryInfo:find("AC Power")
isCharging = charging
if charging then
icon = settings.icons.text.battery.charging
else
if found and charge > 80 then
icon = settings.icons.text.battery._100
elseif found and charge > 60 then
icon = settings.icons.text.battery._75
elseif found and charge > 40 then
icon = settings.icons.text.battery._50
elseif found and charge > 30 then
icon = settings.icons.text.battery._50
color = settings.colors.yellow
elseif found and charge > 20 then
icon = settings.icons.text.battery._25
color = settings.colors.orange
else
icon = settings.icons.text.battery._0
color = settings.colors.red
end
end
local lead = ""
if found and charge < 10 then
lead = "0"
end
battery:set({
icon = {
string = icon,
color = color
},
label = {
string = lead .. label,
padding_left = 0,
},
})
end)
end)
battery:subscribe("mouse.clicked", function(env)
local drawing = battery:query().popup.drawing
battery:set({ popup = { drawing = "toggle" } })
if drawing == "off" then
sbar.exec("pmset -g batt", function(batteryInfo)
local found, _, remaining = batteryInfo:find("(%d+:%d+) remaining")
local label = found and ("Time remaining: " .. remaining .. "h") or (isCharging and "Charging" or "No estimate")
batteryPopup:set({ label = label })
end)
end
end)

View File

@@ -0,0 +1,17 @@
local constants = require("constants")
local calendar = sbar.add("item", constants.items.CALENDAR, {
position = "right",
update_freq = 1,
icon = { padding_left = 0, padding_right = 0 }
})
calendar:subscribe({ "forced", "routine", "system_woke" }, function(env)
calendar:set({
label = os.date("%a %d %b, %H:%M"),
})
end)
calendar:subscribe("mouse.clicked", function(env)
sbar.exec("open -a 'Calendar'")
end)

View File

@@ -0,0 +1,4 @@
require("items.widgets.calendar")
require("items.widgets.battery")
require("items.widgets.volume")
require("items.widgets.wifi")

View File

@@ -0,0 +1,128 @@
local constants = require("constants")
local settings = require("config.settings")
local currentAudioDevice = "None"
local volumeValue = sbar.add("item", constants.items.VOLUME .. ".value", {
position = "right",
label = {
string = "??%",
padding_left = 0,
},
})
local volumeBracket = sbar.add("bracket", constants.items.VOLUME .. ".bracket", { volumeValue.name }, {
popup = {
align = "center"
},
})
local volumeSlider = sbar.add("slider", constants.items.VOLUME .. ".slider", settings.dimens.graphics.popup.width, {
position = "popup." .. volumeBracket.name,
click_script = 'osascript -e "set volume output volume $PERCENTAGE"'
})
volumeValue:subscribe("volume_change", function(env)
local icon = settings.icons.text.volume._0
local volume = tonumber(env.INFO)
sbar.exec("SwitchAudioSource -t output -c", function(result)
-- local currentOutputDevice = result:sub(1, -2)
-- if currentOutputDevice == "AirPods Max" then
-- icon = "􀺹"
-- elseif currentOutputDevice == "AirPods von Longdong Silver" or currentOutputDevice == "AirPods von Anna" then
-- icon = "􀟥"
-- elseif currentOutputDevice == "Arctis Nova Pro Wireless" then
-- icon = "􀑈"
-- elseif currentOutputDevice == "Ear (2)" then
-- icon = "􀪷"
-- elseif currentOutputDevice == "iD4" then
-- icon = "􀝎"
-- else
if volume > 60 then
icon = settings.icons.text.volume._100
elseif volume > 30 then
icon = settings.icons.text.volume._66
elseif volume > 10 then
icon = settings.icons.text.volume._33
elseif volume > 0 then
icon = settings.icons.text.volume._10
end
-- end
local lead = ""
if volume < 10 then
lead = "0"
end
-- volumeIcon:set({ label = icon })
volumeSlider:set({ slider = { percentage = volume } })
local hasVolume = volume ~= 0
volumeValue:set({
icon = icon,
label = {
string = hasVolume and lead .. volume .. "%" or "",
padding_right = hasVolume and 8 or 0,
},
})
end)
end)
local function hideVolumeDetails()
local drawing = volumeBracket:query().popup.drawing == "on"
if not drawing then return end
volumeBracket:set({ popup = { drawing = false } })
sbar.remove("/" .. constants.items.VOLUME .. ".device\\.*/")
end
local function toggleVolumeDetails(env)
if env.BUTTON == "right" then
sbar.exec("open /System/Library/PreferencePanes/Sound.prefpane")
return
end
local shouldDraw = volumeBracket:query().popup.drawing == "off"
if shouldDraw then
volumeBracket:set({ popup = { drawing = true } })
sbar.exec("SwitchAudioSource -t output -c", function(result)
currentAudioDevice = result:sub(1, -2)
sbar.exec("SwitchAudioSource -a -t output", function(available)
local current = currentAudioDevice
local counter = 0
for device in string.gmatch(available, '[^\r\n]+') do
local color = settings.colors.grey
if current == device then
color = settings.colors.white
end
sbar.add("item", constants.items.VOLUME .. ".device." .. counter, {
position = "popup." .. volumeBracket.name,
align = "center",
label = { string = device, color = color },
click_script = 'SwitchAudioSource -s "' ..
device ..
'" && sketchybar --set /' .. constants.items.VOLUME .. '.device\\.*/ label.color=' ..
settings.colors.grey .. ' --set $NAME label.color=' .. settings.colors.white
})
counter = counter + 1
end
end)
end)
else
hideVolumeDetails()
end
end
local function changeVolume(env)
local delta = env.SCROLL_DELTA
sbar.exec('osascript -e "set volume output volume (output volume of (get volume settings) + ' .. delta .. ')"')
end
volumeValue:subscribe("mouse.clicked", toggleVolumeDetails)
volumeValue:subscribe("mouse.scrolled", changeVolume)
-- volumeValue:subscribe("mouse.exited.global", hideVolumeDetails)

View File

@@ -0,0 +1,261 @@
local constants = require("constants")
local settings = require("config.settings")
local popupWidth <const> = settings.dimens.graphics.popup.width + 20
sbar.exec(
"killall network_load >/dev/null; $CONFIG_DIR/bridge/network_load/bin/network_load en0 network_update 2.0"
)
local wifiUp = sbar.add("item", constants.items.WIFI .. ".up", {
position = "right",
width = 0,
icon = {
padding_left = 0,
padding_right = 0,
font = {
style = settings.fonts.styles.bold,
size = 10.0,
},
string = settings.icons.text.wifi.upload,
},
label = {
font = {
family = settings.fonts.numbers,
style = settings.fonts.styles.bold,
size = 10.0,
},
color = settings.colors.orange,
string = "??? Bps",
},
y_offset = 4,
})
local wifiDown = sbar.add("item", constants.items.WIFI .. ".down", {
position = "right",
icon = {
padding_left = 0,
padding_right = 0,
font = {
style = settings.fonts.styles.bold,
size = 10.0,
},
string = settings.icons.text.wifi.download,
},
label = {
font = {
family = settings.fonts.numbers,
style = settings.fonts.styles.bold,
size = 10,
},
color = settings.colors.blue,
string = "??? Bps",
},
y_offset = -4,
})
local wifi = sbar.add("item", constants.items.WIFI .. ".padding", {
position = "right",
label = { drawing = false },
padding_right = 0,
})
local wifiBracket = sbar.add("bracket", constants.items.WIFI .. ".bracket", {
wifi.name,
wifiUp.name,
wifiDown.name
}, {
popup = { align = "center" }
})
local ssid = sbar.add("item", {
align = "center",
position = "popup." .. wifiBracket.name,
width = popupWidth,
height = 16,
icon = {
string = settings.icons.text.wifi.router,
font = {
style = settings.fonts.styles.bold
},
},
label = {
font = {
style = settings.fonts.styles.bold,
size = settings.dimens.text.label,
},
max_chars = 18,
string = "????????????",
},
})
local hostname = sbar.add("item", {
position = "popup." .. wifiBracket.name,
background = {
height = 16,
},
icon = {
align = "left",
string = "Hostname:",
width = popupWidth / 2,
font = {
size = settings.dimens.text.label
},
},
label = {
max_chars = 20,
string = "????????????",
width = popupWidth / 2,
align = "right",
}
})
local ip = sbar.add("item", {
position = "popup." .. wifiBracket.name,
background = {
height = 16,
},
icon = {
align = "left",
string = "IP:",
width = popupWidth / 2,
font = {
size = settings.dimens.text.label
},
},
label = {
align = "right",
string = "???.???.???.???",
width = popupWidth / 2,
}
})
local router = sbar.add("item", {
position = "popup." .. wifiBracket.name,
background = {
height = 16,
},
icon = {
align = "left",
string = "Router:",
width = popupWidth / 2,
font = {
size = settings.dimens.text.label
},
},
label = {
align = "right",
string = "???.???.???.???",
width = popupWidth / 2,
},
})
sbar.add("item", { position = "right", width = settings.dimens.padding.item })
wifiUp:subscribe("network_update", function(env)
local upColor = (env.upload == "000 Bps") and settings.colors.grey or settings.colors.orange
local downColor = (env.download == "000 Bps") and settings.colors.grey or settings.colors.blue
wifiUp:set({
icon = { color = upColor },
label = {
string = env.upload,
color = upColor
}
})
wifiDown:set({
icon = { color = downColor },
label = {
string = env.download,
color = downColor
}
})
end)
wifi:subscribe({ "wifi_change", "system_woke", "forced" }, function(env)
wifi:set({
icon = {
string = settings.icons.text.wifi.disconnected,
color = settings.colors.magenta,
}
})
sbar.exec([[ipconfig getifaddr en0]], function(ip)
local ipConnected = not (ip == "")
local wifiIcon
local wifiColor
if ipConnected then
wifiIcon = settings.icons.text.wifi.connected
wifiColor = settings.colors.magenta
end
wifi:set({
icon = {
string = wifiIcon,
color = wifiColor,
}
})
sbar.exec([[sleep 2; scutil --nwi | grep -m1 'utun' | awk '{ print $1 }']], function(vpn)
local isVPNConnected = not (vpn == "")
if isVPNConnected then
wifiIcon = settings.icons.text.wifi.vpn
wifiColor = settings.colors.green
end
wifi:set({
icon = {
string = wifiIcon,
color = wifiColor,
}
})
end)
end)
end)
local function hideDetails()
wifiBracket:set({ popup = { drawing = false } })
end
local function toggleDetails()
local shouldDrawDetails = wifiBracket:query().popup.drawing == "off"
if shouldDrawDetails then
wifiBracket:set({ popup = { drawing = true } })
sbar.exec("networksetup -getcomputername", function(result)
hostname:set({ label = result })
end)
sbar.exec("ipconfig getifaddr en0", function(result)
ip:set({ label = result })
end)
sbar.exec("ipconfig getsummary en0 | awk -F ' SSID : ' '/ SSID : / {print $2}'", function(result)
ssid:set({ label = result })
end)
sbar.exec("networksetup -getinfo Wi-Fi | awk -F 'Router: ' '/^Router: / {print $2}'", function(result)
router:set({ label = result })
end)
else
hideDetails()
end
end
local function copyLabelToClipboard(env)
local label = sbar.query(env.NAME).label.value
sbar.exec("echo \"" .. label .. "\" | pbcopy")
sbar.set(env.NAME, { label = { string = settings.icons.text.clipboard, align = "center" } })
sbar.delay(1, function()
sbar.set(env.NAME, { label = { string = label, align = "right" } })
end)
end
wifiUp:subscribe("mouse.clicked", toggleDetails)
wifiDown:subscribe("mouse.clicked", toggleDetails)
wifi:subscribe("mouse.clicked", toggleDetails)
ssid:subscribe("mouse.clicked", copyLabelToClipboard)
hostname:subscribe("mouse.clicked", copyLabelToClipboard)
ip:subscribe("mouse.clicked", copyLabelToClipboard)
router:subscribe("mouse.clicked", copyLabelToClipboard)