A Terminal Status Bar updated

       708 words, 4 minutes

In 2020, I stole @gonzalo’s idea for a status bar using only XTerm and stock OpenBSD tools. I wrote about it there .

In 2024, I was notified (Hi Fox) of a flickering issue in the rendering process. So here’s an updated version of the script.

In fact, I don’t use it anymore but I still have the script. And it seems I already noticed about the flickering issue and wrote another proper script. I just forgot to post about it…

The termbar resources

Using the Xresources file is still my way to go. The window geometry has to be changed as I write a bit more things with the new script.

! ~/scripts/termbar inside xterm(1) 
termbar*faceName: RobotoMono NF:style=Bold:size=13
termbar*geometry: 210x1+0+0
termbar*internalBorder: 6
termbar*saveLines: 0
termbar*scrollBar: false
termbar*title: termbar
termbar*foreground: gray
termbar*background: black
termbar*color1: red
termbar*color3: orange

Quoting the X(7) manpage:

XENVIRONMENT
This must point to a file containing X resources. The default is
$HOME/.Xdefaults-. Unlike $HOME/.Xresources, it is
consulted each time an X application starts.

This is why I use .Xresources rather than .Xdefaults.

The termbar script

I’m still using ksh(1). You still may have to ajust to your preferred shell.

#!/bin/ksh
#
# Some kind of status bar for XTerm
# ./termbar
# xterm -e "./termbar" &

PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin"

# Restart it-self if it get a SIGHUP
trap 'echo ""; exec $0' HUP

# Reset the terminal on exit
trap 'tput cnorm; exit 1' INT QUIT TERM

# Get the number of columns
_cols=$(tput columns)

# Use the terminal colors
_rset="\033[0m"  # Reset / Normal formatting
_hide="\033[2m"  # Decrease intensity
_alrt="\033[31m" # Red foreground
_warn="\033[33m" # Yellow foreground
_norm="\033[39m" # Default foreground

# (Nerd Fonts) icons and colors used in functions
set -A _bat "${_norm}" "${_warn}" "${_alrt}"
set -A _pwr "${_norm}"
set -A _net "" "直"
#set -A _nic "em0" "iwm0"
set -A _nic "ure0" "iwx0"
set -A _vol "奄" "奔" "墳"

# Refresh delay used in functions (in secs)
_rf=10          # default refresh time
_rf_bat=$_rf    # bat() refresh time
_rf_cal=$_rf    # cal() refresh time
_rf_cpu=2       # cpu() refresh time
_rf_kbd=5       # kbd() refresh time
_rf_net=5       # net() refresh time
_rf_vol=$_rf    # vol() refresh time
_rf_left=.3     # left part refresh time
_rf_right=2     # right part refresh time

# Functions that gather and print things
function bat {
	[[ $(apm -a) -eq 1 ]] \
	  && echo -n "${_pwr[0]} " \
	  || echo -n "${_bat[$(apm -b)]} "
	echo -n "$(apm -l)%${_norm}"
}

function cal {
	[[ $(date "+%H") -ge 6 && $(date "+%H") -le 22 ]] \
	  && echo -n "${_norm}" \
	  || echo -n "${_warn}"
	echo -n $(date '+%a. %d %b. %H:%M')${_norm}
}

function cpu {
	echo -n " $(sysctl -n hw.sensors.cpu0.temp0 | cut -d '.' -f 1)°C"
}

function kbd {
	#echo -n "  $(setxkbmap -query | awk '/^layout:/ { print $2 }')"
	setxkbmap -query | awk '/^layout:/ { printf "%s  %s", "", $2 }'
}

function net {
	[[ -z "$(ifconfig ${_nic[0]} | grep 'status: no carrier')" ]] \
		&& (echo -n ${_net[0]} ; return)
	echo -n $(ifconfig ${_nic[1]} | \
	  awk -v icon=${_net[1]} '/ieee80211:/ { printf "%s %s %4s", icon, $3, $8 }')
}

function vol {
	_v=$(sndioctl -n output.level | awk '{ print int($0*100) '})
	[[ $(sndioctl -n input.mute) -eq 1 ]] \
		&& echo -n "${_norm}${_norm} " \
		|| echo -n "${_warn}${_norm} "
	[[ $(sndioctl -n output.mute) -eq 1 ]] \
		&& echo -n "婢" \
		|| echo -n "${_vol[$(($_v*3/101))]}"
	echo -n "$_v%"
}

function win {
	_grp=$(xprop -root 32c '\t$0' _NET_CURRENT_DESKTOP | cut -f 2)
	_wid=$(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2)
	[[ "$_wid" == "0x0" ]] && _win="" || \
	_win=$(xprop -id $_wid '\t$0' _NET_WM_NAME | awk -F '"' '{ print $2 }')
	printf "${_hide}[ $_grp ]${_rset}  %s" "${_win}"
}

tput civis # Hide cursor

while true; do
	tput cup 0 106
	printf "%146.146s" "$(cpu)  $(net)  $(kbd)  $(bat)  $(vol)  $(cal)"

	for i in $(jot 15 1); do
		tput cup 0 0
		printf "%-112.112s" "$(win)"
		sleep .3
	done
done

tput cnorm # Show cursor

#EOF

Launch termbar

The way the termbar is launched has not changed and depends on your environment. I’d go for

# xterm -name termbar -class termbar -e ~/scripts/termbar &

Comment, Like, click the Bell and hit the Subscribe button!
Nah, just kidding. Have fun!