From f55b6310bddef55921ede2a775ab578265d82b05 Mon Sep 17 00:00:00 2001 From: Rob Muhlestein Date: Tue, 8 Aug 2023 03:29:48 -0400 Subject: [PATCH] add aln for better alignment vim filter --- scripts/aln | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 scripts/aln diff --git a/scripts/aln b/scripts/aln new file mode 100644 index 0000000..84e1455 --- /dev/null +++ b/scripts/aln @@ -0,0 +1,59 @@ +#!/usr/bin/bash +shopt -s extglob + +# (ripped from deyloop) + +# Aligns input lines so that the occurrence of given character(s) matches up +# among the lines +# +# example: +# +# get this_variable = whatever +# get whatever= this_thing +# get this_other_thing = whatever this is +# +# becomes +# +# get this_variable = whatever +# get whatever = this_thing +# get this_other_thing = whatever this is +# +# if the alignment point is the '=' + +# ------------------------------------------------------------------------ + +_aln() { + local char i max_len + local -A lines_pre lines_post + char="$1" + max_len=0 + i=0 + + while IFS= read -r line; do + # characters preceding the alignment point + lines_pre["$i"]="${line%%$char*}" + + # Trim trailing white space + lines_pre["$i"]="${lines_pre[$i]%%*( )}" + + pre_len="${#lines_pre["$i"]}" + + if [[ $max_len < $pre_len ]]; then + max_len="$pre_len" + fi + + # characters proceeding the alignment point + lines_post["$i"]="${line#*$char}" + + i=$(( i + 1 )) + done + + max_len=$(( max_len + 1 )) + + for (( j=0; j < i; j++ )); do + printf "%-*s%s%s\n" "$max_len" "${lines_pre[$j]}" "$char" "${lines_post[$j]}" + done + +} + +_aln "$@"