Skip to content

Instantly share code, notes, and snippets.

@auipga
Last active March 13, 2017 23:25
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 auipga/d8e71f3e8411c0b39d54955dcf648c27 to your computer and use it in GitHub Desktop.
Save auipga/d8e71f3e8411c0b39d54955dcf648c27 to your computer and use it in GitHub Desktop.
Set nice emblems for folders containing .git, .idea or .sync folders
#!/bin/bash
# set some emblems
# currently supported:
# * phpstorm
# * git
# * rslsync (Resilio Sync / Bittorrent Sync / btsync)
# check parameters
if [ -z "$1" ]; then
echo "Missing parameter <type>.";
echo "Usage: update-emblems <type> [path]";
exit 1;
fi
# some configuration
CONFIG_FILE=$HOME/.emblems-store-$type
# available types with containing folder name as condition
declare -A types
types[phpstorm]=".idea"
types[git]=".git"
types[rslsync]=".sync"
type=$1
if [ ! ${types[$type]+muhaha} ]; then
echo "Argument 1 is no valid type";
echo -n "Valid types are:";
for i in "${!types[@]}"; do
echo -n " $i"
done
echo
exit 1;
fi
if [ -z "$2" ]; then
path=$(realpath $PWD)
else
path=$(realpath $2);
if [ ! -d "$path" ]; then
echo "Argument 2 is no valid path";
exit 1;
fi
fi
addEmblem() {
if [ -z "$1" ] || [ -z "$2" ]; then return 1; fi
getEmblems $1
#echo $emblems; # exit;
if [ -z $emblems ]; then
emblems=$2
elif [[ "$emblems" == *"$2"* ]]; then
echo -n "";
else
emblems="$emblems $2"
fi
# set new emblem(s)
eval "gvfs-set-attribute '$1' -t stringv metadata::emblems $emblems"
return 0;
}
removeEmblem() {
if [ -z "$1" ] || [ -z "$2" ]; then return 1; fi
getEmblems $1
emblems=${emblems//$2/}
if [ -z "${emblems// }" ]; then emblems="none"; fi
#echo -n gvfs-set-attribute $1 -t stringv metadata::emblems "$emblems"
eval "gvfs-set-attribute '$1' -t stringv metadata::emblems $emblems"
return 0;
}
getEmblems() {
if [ -z "$1" ]; then return 1; fi
emblems=$(gvfs-info -a metadata::emblems $1 | grep metadata::emblems | cut -d'[' -f2 | cut -d']' -f1 | cut -d',' -f1- --output-delimiter=' ')
emblems=${emblems/none/}
}
if [ ! -f $CONFIG_FILE ]; then
echo "Creating config file $CONFIG_FILE"
touch $CONFIG_FILE
fi
IFS=$'\n' # make newlines the only separator (to handle paths with spaces correctly)
saved_folders=$(grep "^$path" $CONFIG_FILE)
found_folders=$(find $path -type d -name ${types[$type]} | sed -r 's|/[^/]+$||' | sort)
add_folders=$(comm -13 <(echo "$saved_folders") <(echo "$found_folders"))
del_folders=$(comm -23 <(echo "$saved_folders") <(echo "$found_folders"))
# remove emblems
for f in $del_folders; do
if [ -d $f ]; then
echo Removing $f
removeEmblem "$f" "$type"
else
echo Folder has been deleted: $f
fi
done
# add emblems
for f in $add_folders; do
echo Adding $f
addEmblem $f $type
done
# add new folders
echo -n "$add_folders" >> "$CONFIG_FILE"
# sort file
sort -u -o $CONFIG_FILE $CONFIG_FILE
#remove old folders
echo "$(comm -23 $CONFIG_FILE <(echo "$del_folders"))" > "$CONFIG_FILE"
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment