#!/bin/sh # The MIT License (MIT) # # Copyright © 2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # realign [filenames...] # # Realign all detected columns, right-aligning any detected numbers in any # column. ANSI style-codes are also kept as given. # # The only option available is to show this help message, using any of # `-h`, `--h`, `-help`, or `--help`, without the quotes. case "$1" in -h|--h|-help|--help) awk '/^# +realign /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; esac [ "$1" = "--" ] && shift command='awk' if [ -e /usr/bin/gawk ]; then command='gawk' fi # show all non-existing files given failed=0 for arg in "$@"; do if [ "${arg}" = "-" ]; then continue fi if [ ! -e "${arg}" ]; then printf "no file named \"%s\"\n" "${arg}" > /dev/stderr failed=1 fi done if [ "${failed}" -gt 0 ]; then exit 2 fi ${command} ' # always ignore trailing carriage-returns { gsub(/\r$/, "") } # first non-empty line auto-detects whether input is SSV or TSV width == 0 && /\t/ { FS = "\t"; $0 = $0 } # first non-empty line auto-detects number of table columns width == 0 { width = NF } width > 0 { nrows++ for (i = 1; i <= NF; i++) { data[nrows][i] = $i plain = $i gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain) w = length(plain) if (widths[i] < w) widths[i] = w # handle non-numbers if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) { continue } # see if number has decimals if (match(plain, /\./)) { dd = w - (RSTART - 1) if (dot_decs[i] < dd) dot_decs[i] = dd } } } END { for (i = 1; i <= nrows; i++) { due = 0 for (j = 1; j <= width; j++) { v = data[i][j] # put 2-space gaps between columns if (1 < j) due += 2 if (v ~ /^ *$/) { due += widths[j] continue } plain = v gsub(/\x1b\[[0-9;]*[A-Za-z]/, "", plain) w = length(plain) # handle non-numbers if (!match(plain, /^[+-]?[0-9]+(\.[0-9]+)?$/)) { printf "%*s%s", due, "", v due = widths[j] - w continue } # count `dot-decimals` trail in the number dd = match(plain, /\./) ? w - (RSTART - 1) : 0 rpad = dot_decs[j] - dd lpad = widths[j] - (w + rpad) + due printf "%*s%s", lpad, "", v due = rpad } # treat extra columns as part of the last one last = length(data[i]) for (j = width + 1; j <= last; j++) printf " %s", data[i][j] printf "\n" } } ' "$@"