A couple polybar modules


#1

Wrote a couple scripts using FIFO pipes, stole the start of an idea from here in the xmonad module.

These will likely be included in the next release but for anyone wanting to give them a try, feel free.
I’ve obviously done basic testing with them and there shouldn’t be any issues (though you never know :P).

UPDATE: I made a change to the update script and added the use of inotifywait it’s much more efficient this way while also being able to update as soon as the system database is changed, it will still work fine without but I had to use $(stat) subshells to get a similar effect, so I recommend installing inotify-tools if you don’t already have it.

UPDATE 2: I added an empty echo to clear the pipe when no updates are present, otherwise polybar just keeps drawing whatever was there last. This is currently untested though.

UPDATE 3: Fixed refresh after running a package operation.


network

script (copy to ~/bin/check-network)

#!/bin/bash

# somewhat deamon network state icon
# written by Nathaniel Maia, 2019

wl=''
eth=''
dc='\|/-'

[ -p /tmp/.network_status ] || mkfifo /tmp/.network_status

# loop and dump info to the pipe
while true; do
	until { ping -c 1 'archlinux.org' || ping -c 1 'archlabslinux.com'; } >/dev/null 2>&1; do
		for (( i=0; i < ${#dc}; i++ )); do
			echo "${dc:$i:1}" >/tmp/.network_status # spinning character
			sleep 0.25
		done
	done

	if ip link | grep -q '[0-9]:\se.*:.*state UP'; then
		echo "$eth" >/tmp/.network_status
	else
		echo "$wl" >/tmp/.network_status
	fi

	sleep 300 # only recheck every 5m to save some cpu time
done

module (add to ~/.config/polybar/modules.conf)

[module/network]
type = custom/script
exec = tail -F /tmp/.network_status
exec-if = [ -p /tmp/.network_status ]
tail = true
click-left = networkmanager_dmenu &
click-right = networkmanager_dmenu &
label-padding = 1

updates

script (copy to ~/bin/check-update)

#!/bin/bash

# somewhat deamon update count + icon
# written by Nathaniel Maia, 2019

ICON=""

if ! hash checkupdates inotifywait >/dev/null 2>&1; then
	echo 'error: check-update: requires "pacman-contrib" and "inotify-tools" installed' >&2
	exit 1
fi

[ -p /tmp/.update_status ] || mkfifo /tmp/.update_status

# wait until were connected
until ping -c 1 'archlinux.org' >/dev/null 2>&1 || ping -c 1 'archlabslinux.com' >/dev/null 2>&1; do
	sleep 10
done

# loop and dump info to the pipe
while true; do
	if (( ( COUNT = $(checkupdates 2>/dev/null | wc -l) ) > 0 )); then
		echo "$COUNT $ICON" >/tmp/.update_status
		# recheck when the log is written to or after 1.5 hours
		inotifywait -q -q -t 5000 -e close_write /var/log/pacman.log
		sleep 3
	else
		echo "" >/tmp/.update_status
		sleep 5000 # only do a recheck every 1.5 hours to save some cpu time
	fi
done

module (add to ~/.config/polybar/modules.conf)

[module/pkg]
type = custom/script
exec = tail -F /tmp/.update_status
exec-if = [ -p /tmp/.update_status ]
label-padding = 1
tail = true

setup

Once you’ve got the scripts and modules setup the scripts need to actually be started (as a background process with &).

So two options:

  1. Install archlabs-scripts from the unstable repo, it looks for ~/bin/check-network and ~/bin/check-update and runs them if they are executable so you shouldn’t have to do anything but run al-polybar-session.
  2. Execute the scripts in your startup procedure. Because they just dump to a pipe they can be started whenever. Since startup varies I’ll just use basic shell syntax that can be used in
    ~/.xinitrc ~/.xprofile ~/.config/openbox/autostart ~/.config/bspwm/bspwmrc but this can be translated to others easily.
    $HOME/bin/check-update &
    $HOME/bin/check-network &
    

Lemme know if you have any issues,

Cheers


#2

hi @natemaia thank for the job.

When I do the pkg update it still shows updates.
edit
I restarted and pkg and returned to zero, now let’s see when new updates arrive if you reset or always rhyme with active updates


#3

Wow, top job again @natemaia


#4

Perhaps I have to empty the pipe when there are no updates, let me know if it happens again.

Make sure you have the most recent version of the script after I edited it, also make sure you don’t have multiple pkg modules in polybar modules.conf


#5

should all be ok, 9 updates have just arrived, I update and let you know.
the script is the one above in this post.
I copied it this morning.


#6

ok done the update, see photo

Edit
should i remove pkg from config polybar?


#7

Nice install @ector , sorry about behing offtopic.


#8

I added another edit, see above. Double check that yours is the same.

If you have multiple [module/pkg] in your modules.conf then remove the old or rename one to be different, [module/pkg2] for example. Make sure to load the right one though :stuck_out_tongue:


#9

I’m sorry @natemaia, but on three distro does not work.
Good night


#10

Ok I see what is going wrong.

Grab the fixed version from above, make sure your have inotify-tools installed.


#12

yes natemaia.
excuse me, now it works you see that I had to restart.


#13

I’ve added these seem to be all working well.