12

I'd like to have a simple, calm status bar for Sway which I use with Arch Linux.

The configurations I found so far use a separate program like waybar or i3status. While they look great, I'd like to keep it simple and use status_command mentioned in man sway-bar directly.

Preferably, this status bar would work equally well with i3 which should be possible since Sway aims to have its configuration be compatible with i3.

6

I have this script at ~/.config/sway/status.sh:

# The Sway configuration file in ~/.config/sway/config calls this script.
# You should see changes to the status bar after saving this script.
# If not, do "killall swaybar" and $mod+Shift+c to reload the configuration.

# Produces "21 days", for example
uptime_formatted=$(uptime | cut -d ',' -f1  | cut -d ' ' -f4,5)

# The abbreviated weekday (e.g., "Sat"), followed by the ISO-formatted date
# like 2018-10-06 and the time (e.g., 14:01)
date_formatted=$(date "+%a %F %H:%M")

# Get the Linux version but remove the "-1-ARCH" part
linux_version=$(uname -r | cut -d '-' -f1)

# Returns the battery status: "Full", "Discharging", or "Charging".
battery_status=$(cat /sys/class/power_supply/BAT0/status)

# Emojis and characters for the status bar
# πŸ’Ž πŸ’» πŸ’‘ πŸ”Œ ⚑ πŸ“ \|
echo $uptime_formatted ↑ $linux_version 🐧 $battery_status πŸ”‹ $date_formatted

The part in ~/.config/sway/config that defines the status bar is this:

bar {
    position top
    # Keep in mind that the current directory of this config file is $HOME
    status_command while ~/.config/sway/status.sh; do sleep 1; done

    colors {
        # Text color of status bar
        statusline #ffffff
        # Background of status bar
        background #323232
    }
    font pango:DejaVu Sans Mono 10
}

That's how the bar looks using this configuration:

swaybar

The above settings works also in i3 with an identical result.

You need to have an appropriate font installed to render the emoji characters, for example:

pacman -S noto-fonts-emoji
| improve this answer | |
3

I love bash but I am using a Python script for this. Looks like Python's standard library has lot of utilities for these kind of things.

from datetime import datetime
from psutil import disk_usage, sensors_battery
from psutil._common import bytes2human
from socket import gethostname, gethostbyname
from subprocess import check_output
from sys import stdout
from time import sleep

def write(data):
    stdout.write('%s\n' % data)
    stdout.flush()

def refresh():
    disk = bytes2human(disk_usage('/').free)
    ip = gethostbyname(gethostname())
    try:
        ssid = check_output("iwgetid -r", shell=True).strip().decode("utf-8")
        ssid = "(%s)" % ssid
    except Exception:
        ssid = "None"
    battery = int(sensors_battery().percent)
    status = "Charging" if sensors_battery().power_plugged else "Discharging"
    date = datetime.now().strftime('%h %d %A %H:%M')
    format = "Space: %s | Internet: %s %s | Battery: %s%% %s | Date: %s"
    write(format % (disk, ip, ssid, battery, status, date))

while True:
    refresh()
    sleep(1)

Here's a screenshot of the bar:

status bar screenshot

| improve this answer | |
  • 1
    @Matthias, It is a very simple one since I like minimalism. I think the main advantage is that Python has lot of libraries for this + better data structures (no cut, awk etc) - so cleaner syntax :-) – Nishant May 25 '19 at 9:39
  • Recently I realized that psutils is not part of the standard library, it is very a common one though for cross-platform stuff :-) – Nishant Jul 1 '19 at 12:32
3

Here's my current status bar:

status bar screenshot sound on

When audio is muted:

status bar screenshot sound off

Content of status.sh which ~/.config/sway/config calls:

# The Sway configuration file in ~/.config/sway/config calls this script.
# You should see changes to the status bar after saving this script.
# If not, do "killall swaybar" and $mod+Shift+c to reload the configuration.

# The abbreviated weekday (e.g., "Sat"), followed by the ISO-formatted date
# like 2018-10-06 and the time (e.g., 14:01). Check `man date` on how to format
# time and date.
date_formatted=$(date "+%a %F %H:%M")

# "upower --enumerate | grep 'BAT'" gets the battery name (e.g.,
# "/org/freedesktop/UPower/devices/battery_BAT0") from all power devices.
# "upower --show-info" prints battery information from which we get
# the state (such as "charging" or "fully-charged") and the battery's
# charge percentage. With awk, we cut away the column containing
# identifiers. i3 and sway convert the newline between battery state and
# the charge percentage automatically to a space, producing a result like
# "charging 59%" or "fully-charged 100%".
battery_info=$(upower --show-info $(upower --enumerate |\
grep 'BAT') |\
egrep "state|percentage" |\
awk '{print $2}')

# "amixer -M" gets the mapped volume for evaluating the percentage which
# is more natural to the human ear according to "man amixer".
# Column number 4 contains the current volume percentage in brackets, e.g.,
# "[36%]". Column number 6 is "[off]" or "[on]" depending on whether sound
# is muted or not.
# "tr -d []" removes brackets around the volume.
# Adapted from https://bbs.archlinux.org/viewtopic.php?id=89648
audio_volume=$(amixer -M get Master |\
awk '/Mono.+/ {print $6=="[off]" ?\
$4" πŸ”‡": \
$4" πŸ”‰"}' |\
tr -d [])

# Additional emojis and characters for the status bar:
# Electricity: ⚑ β†― ⭍ πŸ”Œ
# Audio: πŸ”ˆ πŸ”Š 🎧 🎢 🎡 🎀
# Separators: \| ❘ ❙ ❚
# Misc: 🐧 πŸ’Ž πŸ’» πŸ’‘ ⭐ πŸ“ ↑ ↓ βœ‰ βœ… ❎
echo $audio_volume $battery_info πŸ”‹ $date_formatted

Here's the status bar part of ~/.config/sway/config:

bar {
    position top

    # Keep in mind that the current directory of this config file is $HOME
    status_command while ~/.config/sway/status.sh; do sleep 1; done

    # https://i3wm.org/docs/userguide.html#_colors
    colors {
        # Text color of status bar
        statusline #f8b500

        # Background color of status bar
        background #5e227f
    }
}

status.sh works also with i3 when called from /.config/i3/config using the same bar block shown above.

Here's a link to my current Sway configuration containing status.sh.

| improve this answer | |

Your Answer

By clicking β€œPost Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.