1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-09 15:39:03 -08:00
rc/tableize

113 lines
4.1 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 | zsh | bash | dash | ash |'
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 zsh= bash= dash= ash= 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
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;;
('#'*) 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 "$zsh" "$bash" "$dash" "$ash"; 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" "$@"