~jcc/swaybar-commander

1d33afddc2da86fec21b0009fd4fd84ff97d3f9a — Jason Cox 1 year, 24 days ago 1eaa763
Update samples to what I currently use
1 files changed, 53 insertions(+), 35 deletions(-)

M sample-blocks.md
M sample-blocks.md => sample-blocks.md +53 -35
@@ 48,7 48,9 @@ fi

## Battery

Shows the battery percentage and whether it's charging. If the battery is at 20% or lower and not charging, it is marked as urgent. Requires `acpi`.
Shows the battery percentage, whether it's charging, and the remaining time until empty/full. If the battery is at 20% or lower and not charging, it is marked as urgent. Requires `acpi`.

Add the following to the swaybar-commander config, replacing `/PATH/TO/SCRIPT` with the path to the `Cmd` script below.

### Config



@@ 61,45 63,49 @@ Cmd = ["/PATH/TO/SCRIPT"]

### `Cmd` Script

Add the following to the swaybar-commander config, replacing `/PATH/TO/SCRIPT` with the path to the `Cmd` script below.
Save the following script and make it executable. (Note that you may need to replace `Battery 0` with your battery's name, as found with `acpi -b`.)

```sh
#!/bin/sh

# parse percent from battery status info
battery_line="$(acpi -i | grep capacity)"
battery="${battery_line%%:*}"
status="$(acpi -b | grep "$battery")"
percent="${status#*, }"
percent="${percent%%\%*}"

# determine if the battery is charging
charging="$(acpi -a | grep -q 'on-line' && echo -n '+')"
acpi -b | awk '/Battery 0/{
    percent = match($0, /[0-9]+%/) ? substr($0, RSTART, RLENGTH) : "??%"
    time = match($0, /[0-9]{2}:[0-9]{2}/) ? substr($0, RSTART, RLENGTH) : "-"
    charging = index($0, "Charging") ? "+" : ""
    urgent = !charging && int(substr(percent, 1, length(percent) - 1)) <= 20 ? "true" : "false"

text="B $percent%$charging"

if [ "$percent" -le 20 ] && [ -z "$charging" ]; then
    printf '{"full_text": "%s", "urgent": true}' "$text"
else
    printf "%s" "$text"
fi
    printf("{\"full_text\": \"B %s%s (%s)\", \"urgent\": %s}", percent, charging, time, urgent)
}'
```

## Volume

Shows the volume of the default output sink. Requires `wpctl`.
Shows the volume of the default output sink and input source. Requires `wpctl` and the included `volume` script.

When the volume is adjusted using the included `volume` script, it triggers an immediate update of the swaybar.

### Config

Add the following to the swaybar-commander config.
Add the following to the swaybar-commander config, replacing `/PATH/TO/SCRIPT` with the path to the `Cmd` script below.

```toml
[[Blocks]]
Name = "volume"
PollSecs = 10
Cmd = ["sh", "-c", 'volume is-muted && printf "V mute" || printf "%s" "V $(volume)%"']
Cmd = ["/PATH/TO/SCRIPT"]
```

### `Cmd` Script

Save the following script and make it executable.

```sh
#!/bin/sh

out="$(volume is-muted && printf "mute" || printf "%s%%" "$(volume)")"
in="$(volume in is-muted && printf "mute" || printf "%s%%" "$(volume in)")"

printf "V %s / %s" "${out:-??}" "${in:-??}"
```

### `volume` Script


@@ 114,46 120,53 @@ Save the following as a script named `volume`, make it executable, and put it in
# Requirements
# - wpctl

id=@DEFAULT_SINK@

get_volume() {
    wpctl get-volume @DEFAULT_SINK@ | grep -E -o '[0-9.]*' | sed -e 's/\.//' -e 's/^0*//'
    wpctl get-volume "$id" | grep -E -o '[0-9.]*' | sed -e 's/\.//' -e 's/^0*//'
}

is_muted() {
    wpctl get-volume @DEFAULT_SINK@ | grep -q MUTED
    wpctl get-volume "$id" | grep -q MUTED
}

if [ "$1" = in ]; then
    id=@DEFAULT_SOURCE@
    shift
fi

if [ -z "$1" ]; then
    get_volume
else
    case "$1" in
        up)
            wpctl set-volume @DEFAULT_SINK@ ${2:-5}%+
            wpctl set-volume "$id" ${2:-5}%+

            if [ "$(get_volume)" -gt 100 ]; then
                wpctl set-volume @DEFAULT_SINK@ 1
                wpctl set-volume "$id" 1
            fi

            swaybar-commander update volume
            ;;

        down)
            wpctl set-volume @DEFAULT_SINK@ ${2:-5}%-
            wpctl set-volume "$id" ${2:-5}%-
            swaybar-commander update volume
            ;;
        mute)
            if ! is_muted; then
                wpctl set-mute @DEFAULT_SINK@ 1
                wpctl set-mute "$id" 1
                swaybar-commander update volume
            fi
            ;;
        unmute)
            if is_muted; then
                wpctl set-mute @DEFAULT_SINK@ 0
                wpctl set-mute "$id" 0
                swaybar-commander update volume
            fi
            ;;
        toggle-mute)
            wpctl set-mute @DEFAULT_SINK@ toggle
            wpctl set-mute "$id" toggle
            swaybar-commander update volume
            ;;
        is-muted)


@@ 169,7 182,7 @@ fi

## Network

Shows currently connected networks. Requires `nmcli`.
Shows currently connected networks, including whether the connection is limited or has a portal. Requires `nmcli`.

When a network connection change is detected, an immediate update of the swaybar is triggered.



@@ 195,7 208,7 @@ Save the following scriPt and make it executable.

connections="$(
    nmcli --get-values name,type connection show --active \
        | grep -v :bridge \
        | grep -v -e :bridge -e :loopback \
        | awk -F : '{
            if (NR == 1) {
                printf "%s", $1


@@ 205,7 218,14 @@ connections="$(
        }'
)"

printf "N ${connections:-[none]}"
connectivity="$(nmcli networking connectivity)"
case "$connectivity" in
    portal|limited) connectivity="[$connectivity] " ;;
    unknown) connectivity="[?] " ;;
    *) connectivity="" ;;
esac

printf "N $connectivity${connections:-[none]}"
```

### `UpdateRequesterCmd` Script


@@ 223,9 243,7 @@ signal="$2"
trap "kill \$(pgrep --parent $$)" EXIT

nmcli monitor | while read line; do
    if echo "$line" | grep -q -e connected -e removed; then
        kill -s "$signal" "$ppid"
    fi
    kill -s "$signal" "$ppid"
done
```