LinkedIn Sourceforge Twitter

Vincent's Blog

Pleasure in the job puts perfection in the work (Aristote)

On OpenBSD, shell prompt to show battery's usage of your laptop

Posted on 2023-10-01 15:46:00 from Vincent in OpenBSD

For those using lot of terminal windows on your laptop, I've build a small script showing the battery's usage. His percentage, if it's charging or discharging. Color change to inform that battery is going bellow 20%.

Everythin without leaving your usual terminal window ;)


In a previous blog, I've shared a small script allowing you to see battery's usage in Tint2.

But, if like me, you are mainly working in several terminal windows. Time's fly, and I reach end of battery without noticing that my Tint2 icon is warning me.

What a disaster to see my laptop going shutdown without letting me to save my work.

It happens me some weird situation where I plug my charging cable in the laptop. But the outlet is not in the wall. So I do not look at the Tint2 icon informing me that I'm still pulling energy from the battery. And my Laptop stopped in the middle of my tasks.

To avoid all those situation I had the idea to have a battery's status in my prompt's terminal.

Adaptation of the prompt.

As documentated in the man pages, we can adapt the prompt by global variable called PS1

export PS1='$(battery)\h:\w \$'

In this case, I'm using some extra layout features:
"\h" will display the hostname of the laptop
"\w" will display your current working directory
"\$" will display the $ sign

Battery command

As stated here above, we will define a shell's function that will return the battery usage and status.

This script is based on the command apm which is present in "base"

    local bat=""
    local level=""
    local charg=""
    bat=$(apm -b 2>/dev/null)
    if [ -n "$bat" -a "$bat" -lt "4" ]; then
        level=$(apm -l 2>/dev/null)
        charg=$(apm -a 2>/dev/null)
        if [ -n "$charg" -a "$charg" -eq "1" ]; then
            #charging green
            printf "\033[01;32m$level%%\033[00m"
            if [ "$level" -lt "20" ]; then
                #print red
                print -- "\033[01;31m$level%\033[00m"
                #print blue
                print -- "\033[01;34m$level%\033[00m"
        #no battery
        print -- ""

This script is also using a color's features proposed by the termnial. At least it works for the default xterm and also in Alacritty (my preferred terminal ;))

So, the percentage of battery is displayed in blue when you battery is above 20%. It's displayed in red when bellow 20%. And it's displayed in green when charging.

The initial "if statement" is checking if your machine has a battery via the command "apm -b". Indeed, if this value is 4 or above, apm does not find any batteries. so we skip the script.

Integrate this in your profile

There are multiple options, but I share my own case.

I've added the export PS1 and the "battery" script in my own ~/.profile file.
That way the battery command is integrated in my shell sessions and "export PS1" will always be executed at each new sessions.

If you are not using that default session or shell, you have to put those 2 elements in their correct places.

How this will look like ?

Charging 81% from my home directory displayed in Xterm:

Discharging 80% from /tmp directory displayed in Alacritty

Dischargin 16% from /tmp directory displayed in Xterm

Charging 16% from home directory displayed in Alacritty


Thanks to this small trick, I've more visibility on my battery's status. I've not yet had a battery shortage. But, cross fingers.

7, 5
displayed: 3935

1. From neb_84 on Thu Oct 5 13:58:30 2023

Hi, I tried this from an earlier version of this guide, I think. Excellent idea, thank you! If I remember correctly the PS1 did not update after entering commands - I had to log out and back in again for that. This was in the virtual consoles, though - not in X!

2. From Vincent on Thu Oct 5 21:36:01 2023

Thanks for your encouraging reply neb_84. When you update you .profile file, you can reload it by doing: . ~/.profile

What is the first letter of the word Python?