From 3afa78d52a1803da848b6e90f399ba45b903b189 Mon Sep 17 00:00:00 2001 From: winston Date: Thu, 20 Jun 2024 08:24:14 +0200 Subject: [PATCH] feat: sketchybar tweaks --- home/apps/sketchybar/plugins/colordummy.nu | 9 +- home/apps/sketchybar/plugins/music.nu | 114 +++++++++++++++---- home/apps/sketchybar/plugins/music_jump.nu | 12 ++ home/apps/sketchybar/plugins/window_title.nu | 58 ---------- home/apps/sketchybar/sketchybarrc | 94 ++++++++------- 5 files changed, 160 insertions(+), 127 deletions(-) create mode 100755 home/apps/sketchybar/plugins/music_jump.nu delete mode 100755 home/apps/sketchybar/plugins/window_title.nu diff --git a/home/apps/sketchybar/plugins/colordummy.nu b/home/apps/sketchybar/plugins/colordummy.nu index 01a2f62..a21b9a8 100755 --- a/home/apps/sketchybar/plugins/colordummy.nu +++ b/home/apps/sketchybar/plugins/colordummy.nu @@ -90,11 +90,14 @@ def color [name: string, alpha: float = 1.0] { $"icon.color=(color fg)" $"label.color=(color fg)" --set /space/ - $"icon.color=(color gray 0.5)" + $"icon.color=(color bgGray)" $"icon.highlight_color=(color blue)" --set music - $"icon.color=(color violet)" - $"label.color=(color violet)" + $"icon.color=(color fgGray)" + $"label.color=(color fgGray)" + --set music_progress + $"slider.highlight_color=(color blue)" + $"slider.background.color=(color bgGray)" --set "Mullvad VPN" $"alias.color=(color orange)" --set "Control Center,Battery" diff --git a/home/apps/sketchybar/plugins/music.nu b/home/apps/sketchybar/plugins/music.nu index 2c1f77c..c5f4fae 100755 --- a/home/apps/sketchybar/plugins/music.nu +++ b/home/apps/sketchybar/plugins/music.nu @@ -1,25 +1,95 @@ #!/usr/bin/env nu -if (pgrep -x Music | is-not-empty) { - let result = osascript -l JavaScript -e " - var music = Application("Music"); - JSON.stringify({ - albumArtist: music.currentTrack.albumArtist(), - artist: music.currentTrack.artist(), - state: music.playerState(), - track: music.currentTrack.name(), - });" | from json - - let artist = if (($result.albumArtist | is-empty) or ($result.albumArtist == "Various Artists")) { - $result.artist - } else $result.albumArtist - - let label = if ($result.state == "playing") { - $"($artist) - ($result.track)" - } else "" - +if (pgrep -x Music | is-empty) { (sketchybar - --set $env.NAME $"label=($label)" - icon.font="Symbols Nerd Font:2048-em:18.0" - label.font="Berkeley Mono:Bold:16.0" y_offset="3") -} else (sketchybar --set $env.NAME label="") + --set $env.NAME + label="" + --set $"($env.NAME)-progress" + slider.percentage=0 + slider.width=0 + ) + return +} + +let appleScript = ' +tell application "Music" + -- set to a fixed string + if player state is playing then + set _state to "playing" + else + set _state to "paused" + end if + + set _albumArtist to "" + set _trackArtist to "" + set _name to "" + set _duration to "" + set _progress to "0,0" + + -- allow this to fail when Music hasnt played a track yet + try + set _albumArtist to album artist of current track + set _trackArtist to artist of current track + set _name to name of current track + set _duration to duration of current track + end try + + if _albumArtist is "" or _albumArtist is "Various Artists" then + set _artist to _trackArtist + else + set _artist to _albumArtist + end if + + -- wrap the progress in a try to handle division by zero + try + set _progress to player position / _duration + end try + + + set songData to {_state, _artist, _name, _progress} + + set output to "" + repeat with _item in songData + set output to output & _item & "\n" + end repeat +end tell' + +let cachePath = ($env.XDG_CACHE_HOME? | default $"($env.HOME)/.cache") | path join "apple-music-status.scpt" + +# for debugging +let alwaysCompile = false + +if (not ($cachePath | path exists) or $alwaysCompile) { + osacompile -o $cachePath -e $appleScript +} + +let result = osascript $cachePath + | lines + | { + state: ($in.0?), + artist: ($in.1?) + track: ($in.2?) + progress: ( + $in.3? + | str replace "," "." + | into float + | $in * 100.0 + ) + } + +let label = if ($result.state == "playing") { + $"($result.artist) - ($result.track)" +} else "" + +sketchybar --set $env.NAME $"label=($label)" + +let width = if ($result.state == "playing") { + (sketchybar --query $env.NAME | from json).bounding_rects?.display-1?.size?.0? | default 0 +} else 0 + +(sketchybar + --set $"($env.NAME)_progress" + $"padding_left=-($width - 7)" + $"slider.percentage=($result.progress)" + $"slider.width=($width - 20)" +) diff --git a/home/apps/sketchybar/plugins/music_jump.nu b/home/apps/sketchybar/plugins/music_jump.nu new file mode 100755 index 0000000..f8c7bf4 --- /dev/null +++ b/home/apps/sketchybar/plugins/music_jump.nu @@ -0,0 +1,12 @@ +#!/usr/bin/env nu + +let appleScript = {|it| ' +tell application "Music" + set _duration to duration of current track + + set player position to (' + ($it | into string) + ' * _duration) +end tell'} + +if ($env.PERCENTAGE? != null) { + osascript -e (do $appleScript (($env.PERCENTAGE | into int) / 100.0)) +} diff --git a/home/apps/sketchybar/plugins/window_title.nu b/home/apps/sketchybar/plugins/window_title.nu deleted file mode 100755 index c5cc410..0000000 --- a/home/apps/sketchybar/plugins/window_title.nu +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env nu - -let icon = match ($env.INFO?) { - # IDEs - "CLion" | "GoLand" | "PhpStorm" | "PyCharm" | RustRover | "WebStorm" | "Xcode" => " ", - "Neovide" => " ", - - # developer - "Docker" => " ", - - # terminals - "Kitty" | "iTerm" | "Terminal" | "WezTerm" => " ", - - # browsers - "Chromium" | "Firefox" | "Safari" | "qutebrowser" => " ", - - # system - "App Store" => " ", - "Books" => " ", - "Finder" => "󰀶 ", - "Music" => "󰝚 ", - "Podcasts" => " ", - "Photos" => " ", - "Preview" => " ", - "System Preferences" => " ", - - # chat - "Discord" => "󰙯 ", - "Mattermost" => " ", - "Slack" => " ", - - # creativity - "Affinity Designer" => "󰃣 ", - "Affinity Photo" => "󰃣 ", - "Affinity Publisher" => "󰈙 ", - "Blender" => "󰂫 ", - "Final Cut" => " ", - "Garageband" => "󰋄 ", - "Logic Pro X" => "󰋄 ", - "iMovie" => " ", - - # password managers - "GPG Keychain" | "KeePassXC" | "LastPass" | "1Password" => "󰌋 ", - - # other - "Audible" => "󰋋 ", - "Pocket Casts" => " ", - "Setapp" => " ", - "Spotify" => " ", - "Steam" => " ", - - _ => "󰘔 ", -} - -(sketchybar - --set $env.NAME $"label=($env.INFO? | default '')" $"icon=($icon)" - icon.font="Symbols Nerd Font:2048-em:18.0" - label.font="Berkeley Mono:Bold:16.0" y_offset="3") diff --git a/home/apps/sketchybar/sketchybarrc b/home/apps/sketchybar/sketchybarrc index d87bc4e..d433c20 100755 --- a/home/apps/sketchybar/sketchybarrc +++ b/home/apps/sketchybar/sketchybarrc @@ -1,32 +1,51 @@ #!/usr/bin/env nu # vim:fileencoding=utf-8:foldmethod=marker -let pluginDir = $env.HOME | path join ".config/sketchybar/plugins" +let pluginDir = $env.XDG_CONFIG_HOME? + | default $"($env.HOME)/.config" + | path join "sketchybar/plugins" -## bar appearance {{{ -(sketchybar --bar - height=32 - position=top - padding_left=5 - padding_right=5 - margin=5 - corner_radius=10 - border_width=0 - hidden=off - topmost=off - y_offset=2) +let spacesCmd = ["Ⅰ" "Ⅱ" "Ⅲ" "Ⅳ" "Ⅴ" "Ⅵ" "Ⅶ" "Ⅷ" "Ⅸ" "Ⅹ"] + | enumerate + | reduce --fold [] {|it, acc| + let i = $it.index + 1 + $acc | append [ + "--add" "space" $"space.($i)" "left" + "--set" $"space.($i)" + $"associated_space=($i)" + $"icon=($it.item)" + "icon.padding_left=8" + "icon.padding_right=8" + "background.padding_left=1" + "background.padding_right=1" + "background.height=32" + "label.drawing=off" + $"script=($pluginDir)/space.nu" + $"click_script=yabai -m space --focus ($i)" + ] + } -# colors are managed separately in the colordummy. -# listens to theme_changed events, to swap between dark and light mode -sketchybar -m --add event theme_changed AppleInterfaceThemeChangedNotification (sketchybar + --bar + height=32 + position=top + padding_left=5 + padding_right=5 + margin=5 + corner_radius=10 + border_width=0 + hidden=off + topmost=off + y_offset=2 + + --add event theme_changed AppleInterfaceThemeChangedNotification --add item colordummy center --set colordummy drawing=off $"script=($pluginDir)/colordummy.nu" - --subscribe colordummy theme_changed) + --subscribe colordummy theme_changed + + ...$spacesCmd -##### Changing Defaults ##### -(sketchybar --default updates=when_shown drawing=on @@ -35,36 +54,23 @@ sketchybar -m --add event theme_changed AppleInterfaceThemeChangedNotification label.padding_left=2 label.padding_right=2 icon.padding_left=2 - icon.padding_right=2) -# }}} + icon.padding_right=2 -## left side of the bar {{{ -# spaces for yabai wm -for $el in ["Ⅰ" "Ⅱ" "Ⅲ" "Ⅳ" "Ⅴ" "Ⅵ" "Ⅶ" "Ⅷ" "Ⅸ" "Ⅹ"] --numbered { - let i = $el.index + 1 - - (sketchybar - --add space $"space.($i)" left - --set $"space.($i)" $"associated_space=($i)" - $"icon=($el.item)" - icon.padding_left=8 - icon.padding_right=8 - background.padding_left=1 - background.padding_right=1 - background.height=32 - label.drawing=off - $"script=($pluginDir)/space.nu" - $"click_script=yabai -m space --focus ($i)") -} - -sketchybar -m --add event music_changed com.apple.Music.playerInfo - -(sketchybar + --add event music_changed + com.apple.Music.playerInfo --add item music left --set music $"script=($pluginDir)/music.nu" - label.y_offset=-1 + update_freq=5 --subscribe music music_changed + --add slider music_progress left 100 + --set music_progress + slider.background.height=2 + slider.background.corner_radius=10 + y_offset=-10 + $"script=($pluginDir)/music_jump.nu" + --subscribe music_progress mouse.clicked + --add item clock right --set clock $"script=($pluginDir)/clock.nu"