Skip to content

Instantly share code, notes, and snippets.

@grubernd
Last active April 12, 2021 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grubernd/2dc17c0608bc576b711bdee443577001 to your computer and use it in GitHub Desktop.
Save grubernd/2dc17c0608bc576b711bdee443577001 to your computer and use it in GitHub Desktop.
#!/bin/bash
# -----------------------------------------------------------------------------
# ## Create dynamic drive list for conky
#
# > GRUBERND 2021 <http://grubernd.at> :: MIT License
#
# _Revision 2021-04-12_
#
# + 21.04 initial version
#
# -----------------------------------------------------------------------------
#
# Folder defaults work well with debian 10 based distros.
# No other distros were tested.
mediahome="/media/$USER"
nfshome="/mnt"
-------------------------------------------------------------------------------
_conky_drive_info() {
parent="$1"
# ## Conky drive info
#
# Find mounted drives and create a list ready for conky.
# Avoids the dreaded `${if_mounted (path)}` commands.
# Especially helpful in highly dynamic setups.
# Contains protection against empty folders like unmounted NFS-mountpoints.
#
# Usage:
#
# + Copy script to your conky scripts folder, e.g. `~/.config/conky/scripts/
# + Make executable
# + Add an `execp` command to `.conkyrc`
# + e.g. `${execp $HOME/.config/conky/scripts/media-info.sh}`
# + In the shellscript
# + Call this function with the folder that contains mounted drives
# + Works both for dynamically and statically mounted drives
drivecolor='${color3}'
textcolor='${color0}'
if [[ -d "$parent" ]]; then
pushd "$parent" >/dev/null
folders=$(find . -maxdepth 1 -type d |grep -v "^.$" |sed 's/^.\///' |sort)
if [[ $folders != "" ]]; then
for this_dir in $folders; do
pushd "$parent/$this_dir/" >/dev/null
subfolders=$(find . -maxdepth 1 |grep -v "^.$")
popd >/dev/null
if [[ $subfolders != "" ]]; then
# Trunc the name to a certain length but pad shorter ones
# Mind the space after the s in the printf command
length=11
showname=${this_dir:0:$length}$(printf '%.s ' $(seq $length))
showname=${showname:0:$length}
echo -n "$drivecolor"
echo -n "$showname "
echo -n "$textcolor"
echo -n "\${fs_used_perc $parent/$this_dir}% "
echo -n "[\${fs_free $parent/$this_dir} free]"
echo -n "\${fs_bar $parent/$this_dir}"
echo ""
fi
done
fi
popd >/dev/null
fi
}
-------------------------------------------------------------------------------
_conky_drive_info "$nfshome"
echo "\${hr}"
_conky_drive_info "$mediahome"
-------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment