Waybar : Cycle output devices on sound click.

This commit is contained in:
Dmitry Voronin 2024-05-04 13:54:42 +03:00
parent e4070818aa
commit 984f217835
Signed by: voronind
SSH key fingerprint: SHA256:3kBb4iV2ahufEBNq+vFbUe4QYfHt98DHQjN7QaptY9k
3 changed files with 37 additions and 3 deletions

View file

@ -4,5 +4,19 @@
function sound() { function sound() {
pavucontrol pavucontrol
} }
# Cycle output devices.
function sound_output_cycle() {
local IFS=$'\n'
local current=$(pactl get-default-sink)
local all=($(pactl list short sinks | cut -f2))
local i_current=$(_index_of ''${current} ''${all})
local i_total=''${#all[@]}; ((i_total--))
local i_target=0
[[ "''${i_current}" -lt "''${i_total}" ]] && i_target=$((i_current+1))
pactl set-default-sink ''${all[''${i_target}]}
}
''; '';
} }

View file

@ -119,6 +119,21 @@
return 1 return 1
} }
# Find an index of an element in array.
# Usage: _index_of <ELEMENT> <ARRAY>
function _index_of() {
local element="''${1}"
local array="''${@:2}"
local index=0
for item in ''${array[@]}; do
[[ "''${item}" = "''${element}" ]] && break
((index++))
done
echo "''${index}"
}
# Check if inside Tmux. # Check if inside Tmux.
function _is_tmux() { function _is_tmux() {
[[ "''${TERM_PROGRAM}" = "tmux" ]] [[ "''${TERM_PROGRAM}" = "tmux" ]]

View file

@ -61,7 +61,8 @@
"format-icons": { "format-icons": {
"default": ["", "", ""] "default": ["", "", ""]
}, },
"on-click": "pavucontrol" "on-click": "sound_output_cycle",
"on-click-right": "pavucontrol"
}, },
"mpris": { "mpris": {
"format": "{player_icon} {artist} - {title}", "format": "{player_icon} {artist} - {title}",
@ -74,18 +75,22 @@
"status-icons": { "status-icons": {
"paused": "" "paused": ""
}, },
"on-click-middle": "playerctl stop"
// "ignored-players": ["firefox"] // "ignored-players": ["firefox"]
}, },
"cpu": { "cpu": {
"format": "{usage}% ({load})", "format": "{usage}% ({load})",
"tooltip": false "tooltip": false,
"on-click": "btop"
}, },
"memory": { "memory": {
"format": "{percentage}%" "format": "{percentage}%",
"on-click": "btop"
}, },
"temperature": { "temperature": {
"hwmon-path": "/sys/class/hwmon/hwmon1/temp1_input", "hwmon-path": "/sys/class/hwmon/hwmon1/temp1_input",
"format": "{temperatureC}°C", "format": "{temperatureC}°C",
"on-click": "btop"
} }
} }
''; '';