Skip to content

Commit

Permalink
Pick an awk flavor and assign it to $_Z_AWK
Browse files Browse the repository at this point in the history
  • Loading branch information
agkozak committed Feb 23, 2018
1 parent 9e41a50 commit 6843d28
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions z.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
# * z -l foo # list matches instead of cd
# * z -c foo # restrict matches to subdirs of $PWD

case $(uname -a) in
SunOS*) awk() { nawk "$@"; } ;;
esac

[ -d "${_Z_DATA:-$HOME/.z}" ] && {
echo "ERROR: z.sh's datafile (${_Z_DATA:-$HOME/.z}) is a directory."
}

_z_pick_awk_flavor() {
if [ -z "$_Z_AWK" ]; then
local awk; for awk in mawk gawk original-awk nawk awk; do
command -v $awk > /dev/null 2>&1 && _Z_AWK=$awk && break
done
fi
}

_z() {

local datafile="${_Z_DATA:-$HOME/.z}"
Expand Down Expand Up @@ -65,7 +69,7 @@ _z() {

# maintain the data file
local tempfile="$datafile.$RANDOM"
_z_dirs | awk -v path="$*" -v now="$(date +%s)" -F"|" '
_z_dirs | $_Z_AWK -v path="$*" -v now="$(date +%s)" -F"|" '
BEGIN {
rank[path] = 1
time[path] = now
Expand Down Expand Up @@ -98,7 +102,7 @@ _z() {

# tab completion
elif [ "$1" = "--complete" -a -s "$datafile" ]; then
_z_dirs | awk -v q="$2" -F"|" '
_z_dirs | $_Z_AWK -v q="$2" -F"|" '
BEGIN {
q = substr(q, 3)
if( q == tolower(q) ) imatch = 1
Expand Down Expand Up @@ -138,7 +142,7 @@ _z() {
[ -f "$datafile" ] || return

local cd
cd="$( < <( _z_dirs ) awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
cd="$( < <( _z_dirs ) $_Z_AWK -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" '
function frecent(rank, time) {
# relate frequency and time
dx = t - time
Expand Down Expand Up @@ -214,6 +218,8 @@ _z() {
fi
}

_z_pick_awk_flavor

alias ${_Z_CMD:-z}='_z 2>&1'

[ "$_Z_NO_RESOLVE_SYMLINKS" ] || _Z_RESOLVE_SYMLINKS="-P"
Expand Down

0 comments on commit 6843d28

Please sign in to comment.