mirror of
https://github.com/fergalmoran/settings-webstorm.git
synced 2025-12-22 09:47:41 +00:00
Added back z.sh (bad Fergal)
This commit is contained in:
3
bashrc
3
bashrc
@@ -101,6 +101,9 @@ if [ -f ~/.bash_functions ]; then
|
|||||||
source ~/.bash_functions
|
source ~/.bash_functions
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
. ~/dotfiles/z.sh
|
||||||
|
|
||||||
|
|
||||||
export ANDROID_SDK=/opt/android-sdk-linux/
|
export ANDROID_SDK=/opt/android-sdk-linux/
|
||||||
export PATH=$PATH:/opt/android-sdk-linux/tools:/opt/android-sdk-linux/platform-tools/:/opt/genymotion
|
export PATH=$PATH:/opt/android-sdk-linux/tools:/opt/android-sdk-linux/platform-tools/:/opt/genymotion
|
||||||
export SENCHA_CMD_3_0_0="/home/fergalm/dev/sencha/Sencha/Cmd/4.0.0.203"
|
export SENCHA_CMD_3_0_0="/home/fergalm/dev/sencha/Sencha/Cmd/4.0.0.203"
|
||||||
|
|||||||
83
z.sh
83
z.sh
@@ -13,6 +13,7 @@
|
|||||||
# set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
|
# set $_Z_NO_RESOLVE_SYMLINKS to prevent symlink resolution.
|
||||||
# set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
|
# set $_Z_NO_PROMPT_COMMAND if you're handling PROMPT_COMMAND yourself.
|
||||||
# set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
|
# set $_Z_EXCLUDE_DIRS to an array of directories to exclude.
|
||||||
|
# set $_Z_OWNER to your username if you want use z while sudo with $HOME kept
|
||||||
#
|
#
|
||||||
# USE:
|
# USE:
|
||||||
# * z foo # cd to most frecent dir matching foo
|
# * z foo # cd to most frecent dir matching foo
|
||||||
@@ -30,8 +31,16 @@ _z() {
|
|||||||
|
|
||||||
local datafile="${_Z_DATA:-$HOME/.z}"
|
local datafile="${_Z_DATA:-$HOME/.z}"
|
||||||
|
|
||||||
# bail if we don't own ~/.z (we're another user but our ENV is still set)
|
# bail if we don't own ~/.z and $_Z_OWNER not set
|
||||||
[ -f "$datafile" -a ! -O "$datafile" ] && return
|
[ -z "$_Z_OWNER" -a -f "$datafile" -a ! -O "$datafile" ] && return
|
||||||
|
|
||||||
|
_z_dirs () {
|
||||||
|
while read line; do
|
||||||
|
# only count directories
|
||||||
|
[ -d "${line%%\|*}" ] && echo $line
|
||||||
|
done < "$datafile"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
# add entries
|
# add entries
|
||||||
if [ "$1" = "--add" ]; then
|
if [ "$1" = "--add" ]; then
|
||||||
@@ -40,18 +49,15 @@ _z() {
|
|||||||
# $HOME isn't worth matching
|
# $HOME isn't worth matching
|
||||||
[ "$*" = "$HOME" ] && return
|
[ "$*" = "$HOME" ] && return
|
||||||
|
|
||||||
# don't track excluded dirs
|
# don't track excluded directory trees
|
||||||
local exclude
|
local exclude
|
||||||
for exclude in "${_Z_EXCLUDE_DIRS[@]}"; do
|
for exclude in "${_Z_EXCLUDE_DIRS[@]}"; do
|
||||||
[ "$*" = "$exclude" ] && return
|
case "$*" in "$exclude*") return;; esac
|
||||||
done
|
done
|
||||||
|
|
||||||
# maintain the data file
|
# maintain the data file
|
||||||
local tempfile="$datafile.$RANDOM"
|
local tempfile="$datafile.$RANDOM"
|
||||||
while read line; do
|
awk < <(_z_dirs 2>/dev/null) -v path="$*" -v now="$(date +%s)" -F"|" '
|
||||||
# only count directories
|
|
||||||
[ -d "${line%%\|*}" ] && echo $line
|
|
||||||
done < "$datafile" | awk -v path="$*" -v now="$(date +%s)" -F"|" '
|
|
||||||
BEGIN {
|
BEGIN {
|
||||||
rank[path] = 1
|
rank[path] = 1
|
||||||
time[path] = now
|
time[path] = now
|
||||||
@@ -68,35 +74,34 @@ _z() {
|
|||||||
count += $2
|
count += $2
|
||||||
}
|
}
|
||||||
END {
|
END {
|
||||||
if( count > 6000 ) {
|
if( count > 9000 ) {
|
||||||
# aging
|
# aging
|
||||||
for( x in rank ) print x "|" 0.99*rank[x] "|" time[x]
|
for( x in rank ) print x "|" 0.99*rank[x] "|" time[x]
|
||||||
} else for( x in rank ) print x "|" rank[x] "|" time[x]
|
} else for( x in rank ) print x "|" rank[x] "|" time[x]
|
||||||
}
|
}
|
||||||
' 2>/dev/null >| "$tempfile"
|
' 2>/dev/null >| "$tempfile"
|
||||||
# do our best to avoid clobbering the datafile in a race condition
|
# do our best to avoid clobbering the datafile in a race condition.
|
||||||
if [ $? -ne 0 -a -f "$datafile" ]; then
|
if [ $? -ne 0 -a -f "$datafile" ]; then
|
||||||
env rm -f "$tempfile"
|
env rm -f "$tempfile"
|
||||||
else
|
else
|
||||||
|
[ "$_Z_OWNER" ] && chown $_Z_OWNER:$(id -ng $_Z_OWNER) "$tempfile"
|
||||||
env mv -f "$tempfile" "$datafile" || env rm -f "$tempfile"
|
env mv -f "$tempfile" "$datafile" || env rm -f "$tempfile"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# tab completion
|
# tab completion
|
||||||
elif [ "$1" = "--complete" ]; then
|
elif [ "$1" = "--complete" -a -s "$datafile" ]; then
|
||||||
while read line; do
|
while read line; do
|
||||||
[ -d "${line%%\|*}" ] && echo $line
|
[ -d "${line%%\|*}" ] && echo $line
|
||||||
done < "$datafile" | awk -v q="$2" -F"|" '
|
done < "$datafile" | awk -v q="$2" -F"|" '
|
||||||
BEGIN {
|
BEGIN {
|
||||||
if( q == tolower(q) ) imatch = 1
|
if( q == tolower(q) ) imatch = 1
|
||||||
split(substr(q, 3), fnd, " ")
|
q = substr(q, 3)
|
||||||
|
gsub(" ", ".*", q)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
if( imatch ) {
|
if( imatch ) {
|
||||||
for( x in fnd ) tolower($1) !~ tolower(fnd[x]) && $1 = ""
|
if( tolower($1) ~ tolower(q) ) print $1
|
||||||
} else {
|
} else if( $1 ~ q ) print $1
|
||||||
for( x in fnd ) $1 !~ fnd[x] && $1 = ""
|
|
||||||
}
|
|
||||||
if( $1 ) print $1
|
|
||||||
}
|
}
|
||||||
' 2>/dev/null
|
' 2>/dev/null
|
||||||
|
|
||||||
@@ -106,29 +111,28 @@ _z() {
|
|||||||
--) while [ "$1" ]; do shift; local fnd="$fnd${fnd:+ }$1";done;;
|
--) while [ "$1" ]; do shift; local fnd="$fnd${fnd:+ }$1";done;;
|
||||||
-*) local opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in
|
-*) local opt=${1:1}; while [ "$opt" ]; do case ${opt:0:1} in
|
||||||
c) local fnd="^$PWD $fnd";;
|
c) local fnd="^$PWD $fnd";;
|
||||||
h) echo "${_Z_CMD:-z} [-chlrtx] args" >&2; return;;
|
e) local echo=echo;;
|
||||||
x) sed -i "\:^${PWD}|.*:d" "$datafile";;
|
h) echo "${_Z_CMD:-z} [-cehlrtx] args" >&2; return;;
|
||||||
l) local list=1;;
|
l) local list=1;;
|
||||||
r) local typ="rank";;
|
r) local typ="rank";;
|
||||||
t) local typ="recent";;
|
t) local typ="recent";;
|
||||||
|
x) sed -i -e "\:^${PWD}|.*:d" "$datafile";;
|
||||||
esac; opt=${opt:1}; done;;
|
esac; opt=${opt:1}; done;;
|
||||||
*) local fnd="$fnd${fnd:+ }$1";;
|
*) local fnd="$fnd${fnd:+ }$1";;
|
||||||
esac; local last=$1; shift; done
|
esac; local last=$1; [ "$#" -gt 0 ] && shift; done
|
||||||
[ "$fnd" -a "$fnd" != "^$PWD " ] || local list=1
|
[ "$fnd" -a "$fnd" != "^$PWD " ] || local list=1
|
||||||
|
|
||||||
# if we hit enter on a completion just go there
|
# if we hit enter on a completion just go there
|
||||||
case "$last" in
|
case "$last" in
|
||||||
# completions will always start with /
|
# completions will always start with /
|
||||||
/*) [ -z "$list" -a -d "$last" ] && cd "$last" && return;;
|
/*) [ -z "$list" -a -d "$last" ] && builtin cd "$last" && return;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# no file yet
|
# no file yet
|
||||||
[ -f "$datafile" ] || return
|
[ -f "$datafile" ] || return
|
||||||
|
|
||||||
local cd
|
local cd
|
||||||
cd="$(while read line; do
|
cd="$( < <( _z_dirs ) awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
|
||||||
[ -d "${line%%\|*}" ] && echo $line
|
|
||||||
done < "$datafile" | awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
|
|
||||||
function frecent(rank, time) {
|
function frecent(rank, time) {
|
||||||
# relate frequency and time
|
# relate frequency and time
|
||||||
dx = t - time
|
dx = t - time
|
||||||
@@ -163,22 +167,23 @@ _z() {
|
|||||||
# use a copy to escape special characters, as we want to return
|
# use a copy to escape special characters, as we want to return
|
||||||
# the original. yeah, this escaping is awful.
|
# the original. yeah, this escaping is awful.
|
||||||
clean_short = short
|
clean_short = short
|
||||||
gsub(/[\(\)\[\]\|]/, "\\\\&", clean_short)
|
gsub(/\[\(\)\[\]\|\]/, "\\\\&", clean_short)
|
||||||
for( x in matches ) if( matches[x] && x !~ clean_short ) return
|
for( x in matches ) if( matches[x] && x !~ clean_short ) return
|
||||||
return short
|
return short
|
||||||
}
|
}
|
||||||
BEGIN { split(q, words, " "); hi_rank = ihi_rank = -9999999999 }
|
BEGIN {
|
||||||
|
gsub(" ", ".*", q)
|
||||||
|
hi_rank = ihi_rank = -9999999999
|
||||||
|
}
|
||||||
{
|
{
|
||||||
if( typ == "rank" ) {
|
if( typ == "rank" ) {
|
||||||
rank = $2
|
rank = $2
|
||||||
} else if( typ == "recent" ) {
|
} else if( typ == "recent" ) {
|
||||||
rank = $3 - t
|
rank = $3 - t
|
||||||
} else rank = frecent($2, $3)
|
} else rank = frecent($2, $3)
|
||||||
matches[$1] = imatches[$1] = rank
|
if( $1 ~ q ) {
|
||||||
for( x in words ) {
|
matches[$1] = rank
|
||||||
if( $1 !~ words[x] ) delete matches[$1]
|
} else if( tolower($1) ~ tolower(q) ) imatches[$1] = rank
|
||||||
if( tolower($1) !~ tolower(words[x]) ) delete imatches[$1]
|
|
||||||
}
|
|
||||||
if( matches[$1] && matches[$1] > hi_rank ) {
|
if( matches[$1] && matches[$1] > hi_rank ) {
|
||||||
best_match = $1
|
best_match = $1
|
||||||
hi_rank = matches[$1]
|
hi_rank = matches[$1]
|
||||||
@@ -196,8 +201,10 @@ _z() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
')"
|
')"
|
||||||
[ $? -gt 0 ] && return
|
|
||||||
[ "$cd" ] && cd "$cd"
|
[ $? -eq 0 ] && [ "$cd" ] && {
|
||||||
|
if [ "$echo" ]; then echo "$cd"; else builtin cd "$cd"; fi
|
||||||
|
}
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,17 +212,17 @@ alias ${_Z_CMD:-z}='_z 2>&1'
|
|||||||
|
|
||||||
[ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P"
|
[ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P"
|
||||||
|
|
||||||
if compctl >/dev/null 2>&1; then
|
if type compctl >/dev/null 2>&1; then
|
||||||
# zsh
|
# zsh
|
||||||
[ "$_Z_NO_PROMPT_COMMAND" ] || {
|
[ "$_Z_NO_PROMPT_COMMAND" ] || {
|
||||||
# populate directory list, avoid clobbering any other precmds.
|
# populate directory list, avoid clobbering any other precmds.
|
||||||
if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
|
if [ "$_Z_NO_RESOLVE_SYMLINKS" ]; then
|
||||||
_z_precmd() {
|
_z_precmd() {
|
||||||
_z --add "${PWD:a}"
|
(_z --add "${PWD:a}" &)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_z_precmd() {
|
_z_precmd() {
|
||||||
_z --add "${PWD:A}"
|
(_z --add "${PWD:A}" &)
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
[[ -n "${precmd_functions[(r)_z_precmd]}" ]] || {
|
[[ -n "${precmd_functions[(r)_z_precmd]}" ]] || {
|
||||||
@@ -229,14 +236,14 @@ if compctl >/dev/null 2>&1; then
|
|||||||
reply=(${(f)"$(_z --complete "$compl")"})
|
reply=(${(f)"$(_z --complete "$compl")"})
|
||||||
}
|
}
|
||||||
compctl -U -K _z_zsh_tab_completion _z
|
compctl -U -K _z_zsh_tab_completion _z
|
||||||
elif complete >/dev/null 2>&1; then
|
elif type complete >/dev/null 2>&1; then
|
||||||
# bash
|
# bash
|
||||||
# tab completion
|
# tab completion
|
||||||
complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z}
|
complete -o filenames -C '_z --complete "$COMP_LINE"' ${_Z_CMD:-z}
|
||||||
[ "$_Z_NO_PROMPT_COMMAND" ] || {
|
[ "$_Z_NO_PROMPT_COMMAND" ] || {
|
||||||
# populate directory list. avoid clobbering other PROMPT_COMMANDs.
|
# populate directory list. avoid clobbering other PROMPT_COMMANDs.
|
||||||
grep "_z --add" <<< "$PROMPT_COMMAND" >/dev/null || {
|
grep "_z --add" <<< "$PROMPT_COMMAND" >/dev/null || {
|
||||||
PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''_z --add "$(pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null;'
|
PROMPT_COMMAND="$PROMPT_COMMAND"$'\n''(_z --add "$(command pwd '$_Z_RESOLVE_SYMLINKS' 2>/dev/null)" 2>/dev/null &);'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user