~jcc/swaybar-commander

82ccfc3e28ac1fea068f0847735ad69679ac1060 — Jason Cox 1 year, 8 months ago 4c1f68a
Add sample blocks
2 files changed, 249 insertions(+), 0 deletions(-)

M README.md
A sample-blocks.md
M README.md => README.md +4 -0
@@ 25,6 25,10 @@ And install it as root:
# make install
```

## Sample Blocks

Sample blocks (sections of the bar) can be found in [sample-blocks.md](https://git.sr.ht/~jcc/swaybar-commander/tree/master/sample-blocks.md)

## Contributing

Contributions are welcome! Email patches and bug reports to [~jcc/public-inbox@lists.sr.ht](mailto:~jcc/public-inbox@lists.sr.ht). (If you aren't accustomed to the email-based workflow, check out [git-send-email.io](https://git-send-email.io/), or send me an email at [dev@jasoncarloscox.com](mailto:dev@jasoncarloscox.com) and I'll be happy to help.)

A sample-blocks.md => sample-blocks.md +245 -0
@@ 0,0 1,245 @@
# Sample Blocks

Below are various sample blocks that can be displayed using swaybar-commander.

## Next Calendar Event

Shows the next calendar event for the day. If the event is happening in the next 10 minutes, it is marked as urgent. Requires `khal`.

### Config

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

```toml
[[Blocks]]
Name = "calendar"
PollSecs = 300
Cmd = ["/PATH/TO/SCRIPT"]
```

### `Cmd` Script

Save the following script and make it executable.

```sh
#!/bin/sh

get_event() {
    khal list \
        --notstarted \
        --format "{start-time}-{end-time} {title}" \
        now "$1" \
    | grep -E '^[0-9]+:[0-9]+' \
    | head -n 1
}

event="$(get_event eod)"

if [ -n "$event" ]; then
    event_10m="$(get_event 10m)"

    if [ "$event" = "$event_10m" ]; then
        printf '{"full_text": "%s", "urgent": true}' "@$event"
    else
        printf '%s' "@$event"
    fi
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`.

### Config

```toml
[[Blocks]]
Name = "battery"
PollSecs = 10
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.

```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 '+')"

text="B $percent%$charging"

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

## Volume

Shows the volume of the default output sink. Requires `wpctl`.

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.

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

### `volume` Script

Save the following as a script named `volume`, make it executable, and put it in your `PATH`.

```sh
#!/bin/sh
#
# volume - easy way to manipulate pipewire volume
#
# Requirements
# - wpctl

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

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

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

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

            swaybar-commander update volume
            ;;

        down)
            wpctl set-volume @DEFAULT_SINK@ ${2:-5}%-
            swaybar-commander update volume
            ;;
        mute)
            if ! is_muted; then
                wpctl set-mute @DEFAULT_SINK@ 1
                swaybar-commander update volume
            fi
            ;;
        unmute)
            if is_muted; then
                wpctl set-mute @DEFAULT_SINK@ 0
                swaybar-commander update volume
            fi
            ;;
        toggle-mute)
            wpctl set-mute @DEFAULT_SINK@ toggle
            swaybar-commander update volume
            ;;
        is-muted)
            is_muted
            ;;
        *)
            echo "USAGE: $0 [up <number> | down <number> | mute | unmute | toggle-mute | is-muted]"
            exit 1
            ;;
    esac
fi
```

## Network

Shows currently connected networks. Requires `nmcli`.

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


### Config

Add the following to the swaybar-commander config, replacing `/PATH/TO/SCRIPT` with the paths to the `Cmd` and `UpdateRequesterCmd` scripts below.

```toml
[[Blocks]]
Name = "network"
PollSecs = 60
Cmd = ["/PATH/TO/SCRIPT"]
UpdateRequesterCmd = ["/PATH/TO/SCRIPT"]
```

### `Cmd` Script

Save the following scriPt and make it executable.

```sh
#!/bin/sh

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

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

### `UpdateRequesterCmd` Script

Save the following script and make it executable.

```sh
#!/bin/sh

ppid="$1"
signal="$2"

# for some reason this script spawns a child process that never gets killed,
# even when this script is killed -- fix that
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
done
```

## Date & Time

Shows the date and time.

### Config

Add the following to the swaybar-commander config.

```toml
[[Blocks]]
Name = "datetime"
PollSecs = 1
Cmd = ["date", "+%a, %-d %b  %-H:%M:%S"]
```