Macbook Battery Stats in Your ZSH Terminal Prompt

As a power user of my Macbook, I’ve found that I often overlook the small battery icon on my menu bar, especially when I’m immersed in a fun project. This minor inconvenience sparked a thought: why not incorporate the battery status directly into my terminal prompt? Thus, I embarked on a fun exploration into ZSH scripting. With a bit of coding magic, I was able to enhance my terminal prompt to dynamically display my Macbook’s battery percentage. Let me guide you through the process.

We start by loading the ZSH hook module. This module allows us to add functions that run before and/or after each command, giving us the ability to update our battery status prompt in real-time.

autoload -U add-zsh-hookCode language: Bash (bash)

The Battery Status Function

Next, I crafted a function, terminal_battery_stats, that retrieves battery information and displays it in the terminal prompt. Here’s how it works:

function terminal_battery_stats {
    # Retrieve battery statistics using the pmset command. This is a macOS
    # command that allows power management and battery status retrieval.
    # The awk command is used to format the output into a useful string.
    bat_info=$(pmset -g batt | awk 'NR==2 {gsub(/;/,""); print $3 " " $4}')
    
    # Extract the battery percentage and state from the bat_info string.
    bat_percent=$(echo $bat_info | cut -d' ' -f1 | tr -d '%')
    bat_state=$(echo $bat_info | cut -d' ' -f2)

    # Check if the battery is charging or on AC power.
    if [ $bat_state = 'charging' ] || [ $bat_state = 'AC' ] || [ $bat_state = 'charged' ] || [ $bat_state = 'finishing' ]; then
        # If the battery is over 66%, don't display a battery prompt.
        if [ $bat_percent -gt 66 ]; then
            bat_prompt=""
        else
            # Otherwise, set the battery icon to a plug, and the color to green.
            bat_icon='🔌'
            bat_color='%F{green}'
            # Format the prompt with the battery color, percentage, and icon.
            bat_prompt="〔$bat_color$bat_percent%% $bat_icon%f〕"
        fi
    else
        # If the battery is discharging, choose a battery icon and color based on the battery level.
        if [ $bat_percent -le 33 ]; then
            bat_icon='🪫'
            bat_color='%F{red}'
        elif [ $bat_percent -gt 66 ]; then
            bat_icon='🔋'
            bat_color='%F{green}'
        else
            bat_icon='🔋'
            bat_color='%F{yellow}'
        fi
        # Format the prompt with the battery color, percentage, and icon.
        bat_prompt="〔$bat_color$bat_percent%% $bat_icon%f〕"
    fi

    # Check if the current prompt already contains a battery status.
    if [[ "$PROMPT" == *"〔"* ]]; then
        # If it does, remove the existing battery status from the prompt.
        PROMPT=${PROMPT#*"〕"}
    fi

    # Add the new battery status to the prompt.
    PROMPT="${bat_prompt}${PROMPT}"
}Code language: Bash (bash)

To get some basic understanding of the magic behind ZSH scripting and manipulating the terminal prompt, check this helpful resource.

Applying the Battery Status Function

After creating the function, I added terminal_battery_stats to the command prompt via ZSH’s pre-command hook. Now, my function runs before each command entered in the terminal, keeping the battery stats up-to-date.

add-zsh-hook precmd terminal_battery_statsCode language: Bash (bash)

All the above code is added to my ~/.zshrc file, turning my terminal prompt into a dynamic display of my Macbook’s battery status. The resulting terminal prompt looks like this:

Conclusion

Through the power of ZSH scripting magic, my terminal now offers real-time updates of my Macbook’s battery status after every command. I set it to disappear once the battery reaches 67%, a level I consider to be within the safe zone. This is an excellent example of how minor inconveniences can lead to innovative solutions that enhance productivity.

Here’s the full script ready to drop in to your own ~/.zshrc file:


# Load the zsh hook module. This is a module that allows adding functions
# that get run before and/or after each command.
autoload -U add-zsh-hook

# Function to retrieve and display battery statistics in the terminal prompt.
# Uses the pmset command to retrieve battery information, and awk to format
# it into a useful string. Depending on the battery's state and level, 
# different icons and colors will be displayed in the terminal prompt.
# 
# @return void
function terminal_battery_stats {
    # Retrieve battery statistics using the pmset command. This is a macOS
    # command that allows power management and battery status retrieval.
    # The awk command is used to format the output into a useful string.
    bat_info=$(pmset -g batt | awk 'NR==2 {gsub(/;/,""); print $3 " " $4}')
    
    # Extract the battery percentage and state from the bat_info string.
    bat_percent=$(echo $bat_info | cut -d' ' -f1 | tr -d '%')
    bat_state=$(echo $bat_info | cut -d' ' -f2)

    # Check if the battery is charging or on AC power.
    if [ $bat_state = 'charging' ] || [ $bat_state = 'AC' ] || [ $bat_state = 'charged' ] || [ $bat_state = 'finishing' ]; then
        # If the battery is over 66%, don't display a battery prompt.
        if [ $bat_percent -gt 66 ]; then
            bat_prompt=""
        else
            # Otherwise, set the battery icon to a plug, and the color to green.
            bat_icon='🔌'
            bat_color='%F{green}'
            # Format the prompt with the battery color, percentage, and icon.
            bat_prompt="〔$bat_color$bat_percent%% $bat_icon%f〕"
        fi
    else
        # If the battery is discharging, choose a battery icon and color based on the battery level.
        if [ $bat_percent -le 33 ]; then
            bat_icon='🪫'
            bat_color='%F{red}'
        elif [ $bat_percent -gt 66 ]; then
            bat_icon='🔋'
            bat_color='%F{green}'
        else
            bat_icon='🔋'
            bat_color='%F{yellow}'
        fi
        # Format the prompt with the battery color, percentage, and icon.
        bat_prompt="〔$bat_color$bat_percent%% $bat_icon%f〕"
    fi

    # Check if the current prompt already contains a battery status.
    if [[ "$PROMPT" == *"〔"* ]]; then
        # If it does, remove the existing battery status from the prompt.
        PROMPT=${PROMPT#*"〕"}
    fi

    # Add the new battery status to the prompt.
    PROMPT="${bat_prompt}${PROMPT}"
}

# Adds the function terminal_battery_stats to the command prompt 
# meaning it will be run before each command entered in the terminal. 
add-zsh-hook precmd terminal_battery_stats

Code language: Bash (bash)

Good luck!

Other Posts Not Worth Reading

Hey, You!

Like this kind of garbage? Subscribe for more! I post like once a month or so, unless I found something interesting to write about.