Skip to content

Instantly share code, notes, and snippets.

@trapped
Created June 4, 2014 21:58
Show Gist options
  • Save trapped/d4c261fa528aaff669ad to your computer and use it in GitHub Desktop.
Save trapped/d4c261fa528aaff669ad to your computer and use it in GitHub Desktop.
Create a status bar using LemonBoy/bar - disclaimer: terrible update checking
#|/bin/bash
# Folder containing other folders (git repositories) or symlinks to them
WORKFLDR="$HOME/Git"
# Count of updated repositories
updatedc=0
updnames=()
# Whether or not to stay quiet
QUIET="false"
# Where to output the data to
PIPE=""
# Temporary file to store last, unchecked and updated repo names
LUPS="/tmp/checkgits.sh.lups"
if ! [ -f "$LUPS" ]; then
touch "$LUPS"
fi
# Replace the echo command so it redirects to the pipe automatically
function echox() {
if [ "$PIPE" == "" ]; then
echo $@
else
echo $@ > "$PIPE"
fi
}
# Open a terminal window and display the last updated repositories
function showrups() {
urxvt -e bash -c "cat $LUPS | uniq && read -p \"Press enter to continue\"" &
sleep 1.5s
rm -rf "$LUPS"
touch "$LUPS"
}
OPTERR=0
while getopts f:qsp: opt; do
case $opt in
f) WORKFLDR="$OPTARG" ;;
q) QUIET="true" ;;
p) PIPE="$OPTARG" ;;
s) showrups; exit ;;
\?) echo -e "Invalid option: #$(( $OPTIND - 1))\n" \
"Usage: checkgits.sh [-q] [-p PIPE] [-f FOLDER]\n\n" \
"-q\tStays quiet, printing only the number of repositories\n" \
"\tupdated and their names separated by \" ,\"s.\n" \
"-f\tSets the folder to check for repositories into.\n" \
"-p\tSets the FIFO to pipe the result to.\n" \
"-s\tShows the last updated repositories and exits."
exit 1 ;;
esac
done
if ! [ -n $1 ]; then
WORKFLDR=$1
fi
# Check if the Git folder exists
if ! [ -d "${WORKFLDR}" ]; then
if [ "$QUIET" == "false" ]; then
echox -e "\e[0;31mThe \"Git\" folder doesn't exist.\e[0m"
else
echox "0"
fi
exit 1
fi
# Loop through all folders
for folder in ${WORKFLDR}/*; do
if [ -d "$folder" ]; then
cd "$folder"
# Check if the folder is a git repository
if [ -d "$(pwd)/.git" ]; then
bsnm=$(basename "$folder")
if [ "$QUIET" == "false" ]; then
echox -n "Updating $bsnm..."
if [[ ${#bsnm} -gt 16 ]]; then
echox -ne "\t"
elif [[ ${#bsnm} -gt 12 ]]; then
echox -ne "\t\t"
elif [[ ${#bsnm} -lt 4 ]]; then
echox -ne "\t\t\t\t"
else
echox -ne "\t\t\t"
fi
echox -ne "\e[0;31m"
fi
# Check if the folder is actually a repository (less obvious way)
git status > /dev/null 2>&1
if [ $? == 128 ]; then
if [ "$QUIET" == "false" ]; then
echox -e "Not a git repository."
fi
continue
fi
# Try to pull and store the result
updt=$(git pull 2> /dev/null)
case $? in
0*) if [ "$updt" == "Already up-to-date." ]; then
if [ "$QUIET" == "false" ]; then
echox -e "\e[0;32mAlready up-to-date.\e[0m"
fi
else
if [ "$QUIET" == "false" ]; then
echox -e "\e[0;32mUpdated.\e[0m";
fi
updnames[$updatedc]="$bsnm"
updatedc=$(( $updatedc + 1))
echo -e "$bsnm has been \e[0;32mupdated\e[0m." >> "$LUPS"
fi ;;
1*) if [ "$QUIET" == "false" ]; then
echox -e "\e[0;31mError.\e[0m"
fi ;;
esac
fi
fi
done
if [ "$QUIET" == "true" ]; then
res="$updatedc"
if [ "$updatedc" != "0" ]; then
res="$res ${updnames[@]}"
fi
echox "$res"
else
if [ $updatedc != 0 ]; then
echox -e "\n\e[0;32mUpdated $updatedc repositories.\e[0m"
else
echox -e "\n\e[0;32mNo repositories updated.\e[0m"
fi
fi
#!/bin/bash
SPSP=" "
function workspaces() {
num=$(xprop -root _NET_NUMBER_OF_DESKTOPS | cut -d ' ' -f3)
cur=$(xprop -root _NET_CURRENT_DESKTOP | cut -d ' ' -f3)
echo -n "$(( $cur + 1 ))/$num"
}
function volume() {
# ALSA volume
#echo -n "$(amixer sget Headphone | sed -n 's/.*\[\([0-9/]*%\)\].*/\1/p' | uniq)%"
# MPD volume
echo -n "$(mpc volume | cut -d ' ' -f2)%"
}
function clock() {
echo -n "$(date '+%d/%m %H:%M')"
}
function repos() {
res=$(cat /tmp/checkgits.sh.lups | uniq | wc -l)
if [ "$res" != "0" ]; then
echo -n "%{A:checkgits.sh -s:}$res repos to recompile$SPSP%{A}"
fi
}
function repoups() {
echo -n "$(cat "/tmp/checkgits.sh.msg")"
}
function checkrepos() {
repocount=0
reponames=()
while :; do
res="$(checkgits.sh -q)"
# Format: "([0-9]+) (whatever's needed for folder names)"
# Split by space and get first element (repo count)
repocount=$(echo "$res" | awk -F ' ' '{print $1}')
if [ "$repocount" != "0" ]; then
# Get the text after the first space and turn it into an array by splitting it by commas
_reponames=$(echo "$res" | awk -F ' ' '{print substr($0, length($1) + 2, length($0) - length($1) + 1)}')
oldIFS="$IFS"; IFS=","; read -a reponames <<< "${_reponames}"; IFS="$oldIFS"
# Add a notification on the bar
echo -n "%{A:checkgits.sh -s:}%{B#ff424242}%{F#edeade}Updated $repocount $(if [ "$repocount" == "1" ]; then echo -n "repo"; else echo -n "repos"; fi)%{F-}%{B-}%{A}" > /tmp/checkgits.sh.msg
sleep 20s
echo -n "" > /tmp/checkgits.sh.msg
fi
sleep 5m
done &
}
function fillbar() {
echo -n "%{l}${SPSP}$(workspaces)%{c}$(repos)%{r}$(volume) $(clock)${SPSP}"
}
if [ $(pgrep -cx bar) -gt 1 ]; then
echo "The bar is already running."
exit 1
fi
# Start repo checking worker
if [ -f "/tmp/checkgits.sh.msg" ]; then
rm -rf "/tmp/checkgits.sh.msg"
fi
touch "/tmp/checkgits.sh.msg"
checkrepos
while :; do
echo "$(fillbar)"
sleep 0.2s
done | bar \
-g "800x30+320+20" \
-B "#ffedeade" \
-F "#ff424242" \
-f "-*-terminus-medium-*-*--14-*-*-*-*-*-*-*" \
-p | while read action; do
bash -c "$action"
done &
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment