Skip to content

Instantly share code, notes, and snippets.

@RaghavRao
Created December 3, 2018 19:22
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 RaghavRao/52cabb213ef010ad76a5ddf252600b4f to your computer and use it in GitHub Desktop.
Save RaghavRao/52cabb213ef010ad76a5ddf252600b4f to your computer and use it in GitHub Desktop.
relpath(){ python -c "import os.path; print os.path.relpath('$1','${2:-$PWD}')" ; }
show_latest () {
if [ -z "$1" ]
then
local dir=$PWD
else
local dir="$1"
fi
ls --sort=time --reverse "$dir" | tail -1 ;
}
add_comment () {
if [ -z "$1" ]
then
echo "Usage: add_comment [comment] [filename]"
else
cur_com=$( getfattr -d -m - "$2" | grep user.comment )
if [ -z "$cur_com" ]
then
setfattr -n user.comment -v "$1" "$2" && printf "Added comment "$1" to file "$2"\n"
else
printf "There is already a comment found:\n$( getfattr -d -m - "$2" )\n"
read -p "Overwrite? [y/n] " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
setfattr -n user.comment -v "$1" "$2" && printf "Added comment "$1" to file "$2"\n"
fi
fi
fi
}
view_comment () {
if [ -z "$1" ]
then
echo "You must provide a file"
else
getfattr -d -m - "$@"
fi
}
remove_comment () {
if [ -z "$1" ]
then
echo "You must provide a file"
else
setfattr -x user.comment "$1"
fi
}
list_comments () {
shopt -s dotglob
if [ -z "$1" ]
then
local d=$PWD
else
[[ -d "$1" ]] || local d="$1"
fi
for f in $d/*
do
local col1=$( ls --color=always $( relpath $f $d ) )
local col2=$( view_comment $( relpath $f $d ) | tail -n +2 )
echo "$col1" "${col2#*=}" | column -c 2 -t
done
shopt -u dotglob #This is probably unnecessary as the subshell is separate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment