Skip to content

Instantly share code, notes, and snippets.

@yordanoweb
Last active November 2, 2022 17:47
Show Gist options
  • Save yordanoweb/21d35e014c604e30093bc277f5cb6545 to your computer and use it in GitHub Desktop.
Save yordanoweb/21d35e014c604e30093bc277f5cb6545 to your computer and use it in GitHub Desktop.
A brief i3 status bar config with JSON

Prepare i3 for the status script output

In the $HOME/.config/i3/config set the bar section to this:

bar {
        # comment this line as it is the default value
        # status_command i3status
        status_command /path/to/your/script.sh
}

Create the formatted status output line

Create the script /path/to/your/script.sh and give it execution permissions.

#!/bin/sh

echo '{ "version": 1 }'

# Begin the endless array.
echo '['

# We send an empty first array of blocks to make the loop simpler:
echo '[]'

while :;
do
  hour=$(date "+%H:%M:%S")
  today=$(date "+%b/%d")

  ip_addr=$(nmcli | grep inet4 | awk '{print $2}')
  if [ $(ip link show | grep --color=never -c "state UP") -eq 0 ];
  then
    ip_addr="offline"
  fi

  used_mem=$(free -h | grep Mem | awk '{print $3}' | sed "s/i//")
  free_disk=$(df -h | egrep "\/$" | awk '{print $4}')
  cpu_load=$(top -b -d1 -n1|grep -i "Cpu(s)"|head -c21|cut -d ' ' -f3|cut -d '%' -f1 | sed "s/us,/0.0/")
  
  # comment this if you are not in a laptop and remove the corresponding JSON line 
  battery=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "percentage" | awk '{print $2}')

  JSON=$(cat <<EOF
  ,[{
    "full_text": " 🇨🇺 ", "color": "#ffffff", "separator": false, "border_right": 0, "border_left": 0, "separator_block_width": 0
  },{
    "full_text": "", "color": "#ea3d3d", "separator": false, "border_right": 0, "border_left": 0, "border_bottom": 4, "border_top": 0, "separator_block_width": 0, "background": "#000000"
  },{
    "full_text": " CPU: 👽 $cpu_load ", "color": "#ffffff", "separator": false, "border_right": 0, "border_left": 0, "separator_block_width": 0, "background": "#ea3d3d"
  },{
    "full_text": "", "color": "#3b47aa", "separator": false, "border_right": 0, "border_left": 0, "border_bottom": 4, "border_top": 0, "separator_block_width": 0, "background": "#ea3d3d"
  },{
    "full_text": " Used mem: 📚 $used_mem ", "color": "#ffffff", "separator": false, "border_right": 0, "border_left": 0, "separator_block_width": 0, "background": "#3b47aa"
  },{
    "full_text": "", "color": "#5f00af", "separator": false, "border_right": 0, "border_left": 0, "border_bottom": 4, "border_top": 0, "separator_block_width": 0, "background": "#3b47aa"
  },{
    "full_text": " Free disk: ⛁ $free_disk ", "color": "#ffffff", "separator": false, "border_right": 0, "border_left": 0, "separator_block_width": 0, "background": "#5f00af"
  },{
    "full_text": "", "color": "#2c7b39", "separator": false, "border_right": 0, "border_left": 0, "border_bottom": 4, "border_top": 0, "separator_block_width": 0, "background": "#5f00af"
  },{
    "full_text": " 💻 $ip_addr ", "color": "#ffffff", "background": "#2c7b39", "separator": false, "border_right": 0, "border_left": 0, "separator_block_width": 0
  },{
    "full_text": "", "color": "#875fd7", "separator": false, "border_right": 0, "border_left": 0, "border_bottom": 4, "border_top": 0, "separator_block_width": 0, "background": "#2c7b39"
  },{
    "full_text": " 🗓️ $today ", "color": "#ffffff", "background": "#875fd7", "separator": false, "border_right": 0, "border_left": 0, "separator_block_width": 0
  },{
    "full_text": "", "color": "#5f00af", "separator": false, "border_right": 0, "border_left": 0, "border_bottom": 4, "border_top": 0, "separator_block_width": 0, "background": "#875fd7"
  },{
    "full_text": " 🕢 $hour ", "color": "#ffffff", "background": "#5f00af", "separator": false, "border_right    ": 0, "border_left": 0, "separator_block_width": 0
  }]
EOF
)

  echo $JSON
  sleep 1

done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment