From Bulky Baboon, 3 Months ago, written in Plain Text.
  1.  
  2. :#(){ return 0; }
  3. @D#(){ [[ -z "$DEBUG" ]] || return 1; }
  4.  
  5. msg(){ printf -- "%s\n" "$@"; }
  6. emsg(){ printf -- "%s\n" "$@" >&2; }
  7. bail(){ emsg "$@"; usage; exit 1; }
  8. usage(){ awk -F '# ' 'NR>2{print $2};$0==""{exit}' <"$0"; }
  9. #funchelp(){ declare -f "${FUNCNAME[-1]}" | awk -F "^ +:# " '$0~FS{print $NF}'; }
  10.  
  11. assert(){ [[ "$#" -ge "2" ]] || return -1; local type var t; eval "var=\$$#"; [[ -z "$var" ]] && return 1; for ((type=($#-1);type>0;type--)); do eval "t=\$$type"; command awk -v t="^[[:$t:]]+\$" '$0!~t{exit 1}' <<<"$var" || return 1; done; }
  12.  
  13. bat(){ printf '%s\n' "$(</dev/fd/0)"; }
  14. match(){ printf -- "%s\n" "$2" | awk -v m="$1" '$0~m{print $0}'; }
  15. idle(){ local freq __fdlock_idle; [[ ! -z "${__fdlock_idle:-}" ]] || exec {__fdlock_idle}<> <(:); read ${1:+-t "${1%[^0-9]*}"} -u "$__fdlock_idle" || :; exec <&"${__fdlock_idle}"-; }
  16.  
  17.  
  18. # /* LONG DEFINITIONS */ -----------------------------------------------------
  19.  
  20.  
  21. :#(){ return 0; }
  22. :#(){
  23.   :# desc: comment lines in the same manner as `:`. prevents strpping
  24.   :# usage: :# COMMENT
  25.   :# example: :# cat </dev/null >$HOME/*; !! || `exit X`` (( ) '
  26.   return 0
  27. }
  28.  
  29.  
  30. @D#(){ [[ -z "$DEBUG" ]] || return 1; }
  31. @D#(){
  32.   desc=("'logic gaurd to toggle debug'")
  33.   usage=("'@D# || { true && printf -- \"%s\" \"${debug[data]}\"; }'")
  34.   example=("'bash-5$ cat ./myscript'"
  35.            "'#!/bin/bash'"
  36.            "'@D# || { let x++ && printf -- 'DEBUG ENABLED!' >&2; }'"
  37.            "'bash-5$ ./myscript'"
  38.            "'bash-5$ DEBUG=1 ./myscript'"
  39.            "'DEBUG ENABLED!'")
  40.   [[ -z "$DEBUG" ]] || return 1
  41. }
  42.  
  43.  
  44. msg(){ printf -- "%s\n" "$@"; }
  45. msg(){
  46.   # desc: print a message to stdout
  47.   # usage: msg "the blue crow oinks at midnight"
  48.   printf -- "%s\n" "$@"
  49. }
  50.  
  51.  
  52. emsg(){ printf -- "%s\n" "$@" >&2; }
  53. emsg(){
  54.   # desc: print a message to sterr
  55.   # usage: msg "and the sandwich shop has moved to saturn"
  56.   printf -- "%s\n" "$@" >&2
  57. }
  58.  
  59.  
  60. bail(){ emsg "$@"; usage; exit 1; }
  61. bail(){
  62.   # desc: print an error message and exit nonzero
  63.   # usage: bail "E!: missing argument \`$1\`"
  64.   emsg "$@"; usage; exit 1
  65. }
  66.  
  67.  
  68. usage(){ awk -F '# ' 'NR>2{print $2};$0==""{exit}' <"$0"; }
  69. usage(){
  70.   # desc: print help infro from literal text in the head of the current file
  71.   # usage: usage
  72.   # example: bash-5$ cat ./myscript
  73.   #        : #!/bin/sh
  74.   #        : # desc: a script to ...
  75.   #        :
  76.   #        : # previous line left blank for demo
  77.   #        : [[ -z "$1" ]] && usage && exit 1
  78.   #        : printf -- "hello world!"
  79.   #        : bash-5$ ./myscript
  80.   #        : # desc: a script to ...
  81.   awk -F '# ' 'NR>2{print $2};$0==""{exit}' <"$0"
  82. }
  83.  
  84.  
  85. # TODO: a name! funchelp? help? funcusage? funcinfo?
  86. # TODO: maybe its better to use `: -- abc`?
  87. #funchelp(){ declare -f "${FUNCNAME[-1]}" | awk -F "^ +: -- " '$0~FS{print $NF}'; }
  88. funchelp(){ declare -f "${FUNCNAME[-1]}" | awk -F "^ +:# " '$0~FS{print $NF}'; }
  89. funchelp(){
  90.   :# desc: print help info from literal text in function definition
  91.   :# usage: funchelp
  92.   :# example: [[ ! -z "$var" ]] || funchelp
  93.   :# note: requires either `:` builtin, or something like `:#(){ return 0; }`
  94.   declare -f "${FUNCNAME[-1]}" \
  95.     | awk -F "^ +:# " '$0~FS{print $NF}'
  96. }
  97.  
  98. assert(){ [[ "$#" -ge "2" ]] || return -1; local type var t; eval "var=\$$#"; [[ -z "$var" ]] && return 1; for ((type=($#-1);type>0;type--)); do eval "t=\$$type"; command awk -v t="^[[:$t:]]+\$" '$0!~t{exit 1}' <<<"$var" || return 1; done; }
  99. assert(){
  100.   # desc: assert the $type of $var
  101.   # usage: assert TYPE [...] VAR
  102.   # note: uses posix char classes for types, they are as follows,
  103.   #     : alnum alpha blank cntrl digit graph lower print punct upper xdigit
  104.   [[ "$#" -ge "2" ]] || return -1
  105.   local type var t
  106.  
  107.   eval "var=\$$#"
  108.   [[ -z "$var" ]] && return 1
  109.  
  110.   for ((type=($#-1);type>0;type--)); do
  111.     eval "t=\$$type"
  112.     command awk -v t="^[[:$t:]]+\$" '$0!~t{exit 1}' <<<"$var" || return 1
  113.   done
  114. }
  115.  
  116.  
  117. bat(){ printf '%s\n' "$(</dev/fd/0)"; }
  118. bat(){
  119.   # desc: builtins only version of cat(1)
  120.   # usage: bat < FILE
  121.   # example: bat </etc/hosts; pwd | bat
  122.   printf '%s\n' "$(</dev/fd/0)"
  123. }
  124. #scat(){ 1>&2 bashcat; exit 1; }
  125.  
  126.  
  127. match(){ printf -- "%s\n" "$2" | awk -v m="$1" '$0~m{print $0}'; }
  128. match(){
  129.   printf -- "%s\n" "$2" \
  130.     | awk -v m="$1" '$0~m{print $0}'
  131. }
  132.  
  133.  
  134. idle(){ local freq __fdlock_idle; [[ ! -z "${__fdlock_idle:-}" ]] || exec {__fdlock_idle}<> <(:); read ${1:+-t "${1%[^0-9]*}"} -u "$__fdlock_idle" || :; exec <&"${__fdlock_idle}"-; }
  135. idle(){
  136.   # desc: builtins only version of sleep(1). uses read -t and fd as file lock
  137.   # usage: idle 2s
  138.   # example: idle 2s && echo "hail eris!"
  139.   local freq __fdlock_idle
  140.   [[ ! -z "${__fdlock_idle:-}" ]] || exec {__fdlock_idle}<> <(:)
  141.   read ${1:+-t "${1%[^0-9]*}"} -u "$__fdlock_idle" || :
  142.   exec <&"${__fdlock_idle}"-
  143. }
  144.  
  145.  
  146. shutils(){ nawk '{R[NR]=$0}; $0!~/^ +:/ {if($1~/^[^# ]+().*({ +|{)$/) print R[NR-1]}' <"$libpath"; }
  147. shutils(){
  148.     nawk '
  149.       {R[NR]=$0}
  150.       $0!~/^ +:/{if($1~/^[^# ]+().*({ +|{)$/)
  151.           print R[NR-1]
  152.         }' <"${1:-$HOME/.bash/lib/util}"
  153. }
  154.  
captcha