Created
June 7, 2014 14:46
-
-
Save babarot/98ad714375f30ad8c7d8 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
[ "$ZSH_VERSION" ] || return 1 | |
declare -r zshmarkdir=~/.zshmarkdir | |
declare -r zshmarklist=$zshmarkdir/zshmarklist | |
declare -r zshmarklog=$zshmarkdir/zshmarklog | |
declare -i exit_usage=0 | |
function _zshmark_show() { | |
[ ! -e $zshmarklist ] && { echo "$(basezzname $zshmarklist): No exist"; return 1; } | |
[ ! -f $zshmarklist ] && touch $zshmarklist | |
[ ! -f $zshmarklog ] && touch $zshmarklog | |
[ ! -f $zshmarktemp ] && touch $zshmarktemp | |
local -i i= | |
local -a zname | |
local -a zpath | |
zname=( $(awk '{print $1}' $zshmarklist | sed "s $HOME ~ g") ) | |
zpath=( $(awk '{print $2}' $zshmarklist | sed "s $HOME ~ g") ) | |
for (( i=1; i <= ${#zname[*]}; i++ )); do | |
if grep -w ${zname[i]} $zshmarklog >/dev/null; then | |
printf "\033[31m%-15s\033[m%s\n" ${zname[i]} ${zpath[i]} | |
else | |
printf "\033[33m%-15s\033[m%s\n" ${zname[i]} ${zpath[i]} | |
fi | |
done | |
unset zname zpath i | |
} | |
function _zshmark_regist() { | |
[ ! -e $zshmarklist ] && touch $zshmarklist | |
local -i i= | |
local zname= | |
[ $# -eq 0 ] && zname=${PWD##*/} || zname="$1" | |
[ ${#zname} -gt 14 ] && { echo "Please input 14 characters or less."; return 1; } | |
if awk '{print $1}' $zshmarklist | command grep -w "$zname" >/dev/null; then | |
echo "$zname: Already exist" | |
return 1 | |
else | |
#[ "$option_t" ] && printf "%-15s%s\n" $zname $(pwd) >>$zshmarktemp | |
printf "%-15s%s\n" $zname $(pwd) >>$zshmarklist | |
return 0 | |
fi | |
unset limit i zname | |
} | |
function _zshmark_go() { | |
[ "$1" = "-h" ] || [ "$1" = "--help" ] && { _zshmark_usage ${FUNCzname##*_}; return $exit_usage; } | |
local zname | |
local zpath | |
zname=$( awk '{print $1}' $zshmarklist | command grep -w -E "^$1$" ) | |
zpath=$( awk '$1 ~ /'^$1'$/' $zshmarklist | awk '{print $2}' ) | |
if [ $# -eq 0 ]; then | |
echo "${FUNCzname##*_}: too few arguments" | |
echo "Try '${FUNCzname##*_} --help' for more information." | |
return 1 | |
else | |
# case of unregistered | |
if [ -z "$zname" ]; then | |
echo "$1: No such zpath in $(basename $zshmarklist)" | |
return 1 | |
# case of registered | |
else | |
if cd "$zpath" 2>/dev/null; then | |
echo "$(date '+%Y-%m-%d %H:%M:%S') $1" >>$zshmarklog | |
return 0 | |
else | |
# case of not existing | |
echo "$zpath: an invalid pass" | |
_zshmark_delete "$zname" | |
echo "$zname: Deleted now" | |
return 1 | |
fi | |
fi | |
fi | |
unset zname zpath | |
} | |
function _zshmark_delete() { | |
[ ! -e $zshmarklist ] && { echo "$(basename $zshmarklist): No exist"; return 1; } | |
[ "$1" = "-h" ] || [ "$1" = "--help" ] && { _zshmark_usage ${FUNCzname##*_}; return $exit_usage; } | |
[ ! -s $zshmarklist ] && { echo "$(basename $zshmarklist) is empty."; return 1; } | |
[ $# -eq 0 ] && { | |
echo "${FUNCzname##*_}: too few arguments" | |
echo "Try '${FUNCzname##*_} --help' for more information." | |
return 1 | |
} | |
local f= | |
local -i i= | |
for f do | |
local -a zname | |
local -a zpath | |
zname=( $( awk '{print $1}' $zshmarklist | command grep -ivw -E "^$f" ) ) | |
zpath=( $( command grep -ivw -E "^$f" $zshmarklist | awk '{print $2}' ) ) | |
if awk '{print $1}' $zshmarklist | command grep -w -E "^$f" >/dev/null; then | |
# (main) delete from list | |
for (( i=1; i <= ${#zname[*]}; i++ )); do | |
printf "%-15s%s\n" ${zname[i]} ${zpath[i]} | |
done >$zshmarklist | |
# delete the history | |
sed -i '' "/"$'\t'"$f$/d" $zshmarklog 2>/dev/null | |
else | |
echo "$1: No such zpath in $(basename $zshmarklist)" | |
return 1 | |
fi | |
unset f i zname zpath | |
done | |
} | |
[ ! -d $zshmarkdir ] && mkdir -p $zshmarkdir | |
_zshmark_complement() { | |
if (( CURRENT == 2 )); then | |
compadd `awk '{print $1}' $zshmarklist` | |
fi | |
} | |
compdef _zshmark_complement _zshmark_go | |
compdef _zshmark_complement _zshmark_delete | |
alias show='_zshmark_show' | |
alias reg='_zshmark_regist' | |
alias go='_zshmark_go' | |
alias del='_zshmark_delete' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment