I needed the ability to generate a menu dynamically in Fluxbox for various things that change on a regular basis.
Unfortunately, there doesn’t appear to be a facility built into fluxbox to allow for this. So I spent some time and built a partially dynamic menu that updates with the click of an ‘update’ menu item at the root menu.
I was trying to come up with a good way for the menu to update on the fly. Since fluxbox simply reads a menu file (when you use the [include]
function), I needed a file that when read, it returns a dynamic response and begins the process again. It’s not completely in time, but it at least refreshes on the fly. So what is a file that provides these properties?
A named pipe, or a FIFO is a file in which one process writes to the file and another process can then read from the file. The best part is that the write is blocked until the reader reads the file. Once the contents are read, the process starts over again.
And I give you:
EXEC_PATH=`dirname ${0}`
if [ ! -p ${EXEC_PATH}/.menupipe ] ; then
mkfifo ${EXEC_PATH}/.menupipe
fi
function buildmenu {
RESULT=""
CURRENT_PATH=${1}
for ITEM in `ls ${CURRENT_PATH}` ; do
if [ -d "${CURRENT_PATH}/${ITEM}" ] ; then
echo "[submenu] (${ITEM})"
LAST_IFS=$IFS
IFS=$'\n'
for LINE in `buildmenu ${CURRENT_PATH}/${ITEM}` ; do
echo " $LINE"
done
IFS=$LAST_IFS
echo "[end]"
fi
if [ -x "${CURRENT_PATH}/${ITEM}" ] ; then
echo "[exec] (${ITEM}) {${CURRENT_PATH}/${ITEM}}"
fi
done
}
while :
do
printf "$( buildmenu ${EXEC_PATH} )\n\n" > ${EXEC_PATH}/.menupipe
printf "fbpipemenu: output menu ${0}" 1>&2
done