Created
December 3, 2018 19:22
-
-
Save RaghavRao/52cabb213ef010ad76a5ddf252600b4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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