1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 04:19:03 -08:00
rc/tableize

135 lines
5.4 KiB
Bash
Executable file

#!/usr/bin/env dash
# TODO: keep track of which scripts require GNU versions of utilities,
# as opposed of plain ol' busybox. test all the awk scripts, etc.
tableize() {
local name="$1"
local rc="$2"
[ -n "$rc" ] || rc="$(readlink -f "$(dirname "$name")" )"
[ -d "$rc" ] || { printf "%s: %s\n" "$0" 'failed to determine rc directory' >&2; }
cd "$rc" || { printf "%s: %s\n" "$0" 'failed to change directory' >&2; }
# the script column has to be stupid wide because names are repeated twice for anchors.
# column lengths: 45, 10, 4, 4, 4, 4
printf '%s\n' '| script | preference | ash | bash | dash | ksh | mksh | oksh | osh | posh | yash | zsh |'
printf '%s\n' '| --------------------------------------------- | ---------- | --- | ---- | ---- | --- | ---- | ---- | --- | ---- | ---- | --- |'
local f=
for f in sh/*; do
[ ! -d "$f" ] || continue # do not recurse or anything
[ -f "$f" ] || continue # probably failed to glob
local fn="${f##*/}"
# ignore some stuff:
[ "$fn" = "${fn#_}" ] || continue # completion files
[ "$fn" = "${fn%.bak}" ] || continue # backup files
local i=0 ash= bash= dash= ksh= mksh= oksh= osh= posh= yash= zsh= pref=
while IFS= read -r line; do
: $((i+=1))
[ $i -le 10 ] || break # act like head -n10
#printf '%s\n' "$i: $line"
local code="$line"
code="${code#${code%%[! ]*}}" # ltrim
local decom="$code"
#decom="${decom#${decom%%[!#]*}}" # ltrim #s
decom="${decom#\#}"
[ "$code" != "$decom" ] || continue # only care about comments
decom="${decom#${decom%%[! ]*}}" # ltrim
decom="${decom%${decom##*[! ]}}" # rtrim
_pref=
case "$decom" in
('!/bin/sh') _pref=sh;;
('!/usr/bin/env ash') _pref=ash;;
('!/usr/bin/env bash') _pref=bash;;
('!/usr/bin/env dash') _pref=dash;;
('!/usr/bin/env false') _pref=false;;
('!/usr/bin/env perl') _pref=perl;;
('!/usr/bin/env sh') _pref=sh;;
('!/usr/bin/env zsh') _pref=zsh;;
('!/usr/bin/sh') _pref=sh;;
#*) printf '\033[1mno match:\033[0m [%s]\n' "$decom";;
esac
if [ -n "$_pref" ]; then
pref="$_pref"
continue
fi
check=0
set -f
IFS=' '
for flag in $decom; do case "$flag" in
(YES_ZSH) zsh=yes;;
(YES_BASH) bash=yes;;
(YES_DASH) dash=yes;;
(YES_ASH) ash=yes;;
(FAKE_COMPAT) zsh=yes; bash=yes; dash=yes; ash=yes;;
(NO_ZSH) zsh=no;;
(NO_BASH) bash=no;;
(NO_DASH) dash=no;;
(NO_ASH) ash=no;;
(compat:) check=1;;
(+ash) [ "$check" = 0 ] || ash=yes;;
(+bash) [ "$check" = 0 ] || bash=yes;;
(+dash) [ "$check" = 0 ] || dash=yes;;
(+ksh) [ "$check" = 0 ] || ksh=yes;;
(+mksh) [ "$check" = 0 ] || mksh=yes;;
(+oksh) [ "$check" = 0 ] || oksh=yes;;
(+osh) [ "$check" = 0 ] || osh=yes;;
(+posh) [ "$check" = 0 ] || posh=yes;;
(+yash) [ "$check" = 0 ] || yash=yes;;
(+zsh) [ "$check" = 0 ] || zsh=yes;;
(-ash) [ "$check" = 0 ] || ash=no;;
(-bash) [ "$check" = 0 ] || bash=no;;
(-dash) [ "$check" = 0 ] || dash=no;;
(-ksh) [ "$check" = 0 ] || ksh=no;;
(-mksh) [ "$check" = 0 ] || mksh=no;;
(-oksh) [ "$check" = 0 ] || oksh=no;;
(-osh) [ "$check" = 0 ] || osh=no;;
(-posh) [ "$check" = 0 ] || posh=no;;
(-yash) [ "$check" = 0 ] || yash=no;;
(-zsh) [ "$check" = 0 ] || zsh=no;;
('#'*) break;;
esac done
done < "$f"
#printf '%s' "script $fn has a preference for $pref and support for" >&2
#[ "$zsh" != yes ] || printf ' %s' zsh >&2
#[ "$bash" != yes ] || printf ' %s' bash >&2
#[ "$dash" != yes ] || printf ' %s' dash >&2
#[ "$ash" != yes ] || printf ' %s' ash >&2
#printf '\n' >&2
local yay='✔️' # yay='y'
local nay='⭕' # nay='✖️' # nay='n'
local huh='❔' # huh='?'
local etc='*n/a*'
# TODO: escape underscores in filenames.
printf '| [%s](#%s) ' "$fn" "$fn"
printf "%$(( 40 - 2 * ${#fn} ))s" '' # funky way to pad the rest of the column
local p="$pref"
[ "$pref" != false ] || p='**false**'
[ "$pref" != perl ] || p='**perl**'
[ "$pref" != sh ] || p='*sh*'
[ -n "$p" ] && printf '| %10s ' "$p" || printf '| %9s%s ' '' "$huh"
local t=
for t in "$ash" "$bash" "$dash" "$ksh" "$mksh" "$oksh" "$osh" "$posh" "$yash" "$zsh"; do
local w="$huh"
[ "$pref" != perl ] || w="$etc"
[ "$t" != yes ] || w="$yay"
[ "$t" != no ] || w="$nay"
# can't use '%4s' here because printf gets confused by UTF-8.
printf '| %s ' "$w"
done
printf '|\n'
done
}
tableize "$0" "$@"