Skip to content

Instantly share code, notes, and snippets.

@mralusw
Last active September 9, 2021 00:36
Show Gist options
  • Save mralusw/cd6f59f119313191d798a3039cb3831b to your computer and use it in GitHub Desktop.
Save mralusw/cd6f59f119313191d798a3039cb3831b to your computer and use it in GitHub Desktop.
#!/bin/sh
set -ue
# Copyright 2021 Alin Mr. <almr.oss@outlook.com>. Licensed under the MIT license (https://opensource.org/licenses/MIT).
eval "${HYF_WR7PPER_ENV:-}"
set -- "${XDG_CONFIG_HOME:-$HOME/.config}"/hyperfine/hyf_wr7pper.env "$@"
! [ -r "$1" ] || . "$1"
shift
eval set -- "${HYF_WR7PPER_OPTS:-}" '"$@"'
# temp file infrastructure; uses globals $tmp*
__mkt() { # args: name
# TODO: trap
[ "$tmpd" ] || tmpd=$(mktemp -d "${TMPDIR:-/tmp}"/hyf_wr7pper.XXXXXX)
tmpf="$tmpd"/"$1"
tmpcnt=$(( tmpcnt + 1 ))
}
__mkt_shebang() { # args: name shebang
__mkt "$1"; shift
printf '#!%s\n' "$1" >"$tmpf"
chmod a+x "$tmpf"
}
__mkt_shexecwrap() { # args: name cmd
__mkt_shebang "$1" '/bin/sh'; shift
printf 'exec %s "$@"\n' "$1" >>"$tmpf"
}
__script_concat() { # args: script.. # will be separated by ';'
if [ $# -gt 2 ]; then
__script_concat "$1" "$(shift; __script_concat "$@")"
return 0
fi
case "$1" in (''|*[![:space:]]*) ;; (*) set -- '' "$2" ;; esac
case "$2" in (''|*[![:space:]]*) ;; (*) set -- "$1" '' ;; esac
if [ -z "$1" ]; then printf '%s' "$2"
elif [ -z "$2" ]; then printf '%s' "$1"
else
# safe way: $1$nl$2, but screws up benchmark output; so go with this hack
# fooled by e.g. `( echo $'ls \\ \n' )`, `( echo $'ls \\ \n \\ \n' )` etc
printf '%s' "$1" |
# adds final '; ' unless exists or line empty
sed -e '${ s/[[:space:]]*$//; /[^;]$/s/$/; / }'
printf '%s' "$2"
fi
}
__main() {
local i n arg val var
local tmpd= tmpcnt=0 tmpf # dynamic vars used by callees
local pre= post= scripts=false nl='
'
set -- "$@" ---wr7pper--- # marker -- separate original and processed args
while :; do # consumes args upto marker, appends them
if [ $# = 0 ]; then unset Error && : "${Error?internal}"; fi
val= arg=$1; shift
if [ "$arg" = ---wr7pper--- ]; then break; fi
if "$scripts"; then
set -- "$@" "$(__script_concat "$pre$arg" "$post")"
continue
fi
case "$arg" in (--*=*) val=${arg#*=} ;; (-?*) val=${arg#-?} ;; esac
case "$arg" in
(--) scripts=true; set -- "$@" -- ;;
(---v) set -x ;;
(---pre=*) pre=${arg#*=}$pre ;;
(---post=*) post=$( __script_concat "$post" "${arg#*=}" ) ;;
(---cd=*) set -- ---pre='cd -- '"$val"' || exit 1; ' "$@" ;;
(---n=*) var=${val%%=*}; val=${val#*=}; set -- \
---pre="$var=$val; while [ \"\$$var\" != 0 ]; do $var=\$(( $var - 1 )); " \
---post=done "$@"
;;
(-S?*)
case "$val" in (*' '*)
__mkt_shexecwrap "$tmpcnt.wrap.sh" "$val" && val=$tmpf ;;
esac; set -- "$@" -S "$val"
;;
(-r?*)
case "$val" in (1) val=2; set -- "$@" --show-output ;; esac
set -- "$@" -r "$val"
;; # TODO: r=0 -> run scripts directly and exit
(*) set -- "$@" "$arg" ;;
esac
done
set +e; hyperfine "$@"; local rc=$?
case "$tmpd" in (*'hyf_wr7pper'*) rm -rf "$tmpd" ;; esac
return "$rc"
}
__main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment