aboutsummaryrefslogtreecommitdiff
path: root/scripts/dot-local/bin/aligncol
blob: 5e7e37574b3e1d4c6ec10af48d55ad6ace6cd095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env bash

set -euo pipefail

usage() {
    echo "Usage: $0 [-s specialchar] [-h] colmark [colmark ...]"
}

sflag=
sep=

while getopts "hs:" opt; do
    case $opt in
    s)
        sflag=1
        sep="$OPTARG"
        ;;
    h)
        usage
        exit 0
        ;;
    ?)
        usage
        exit 1
        ;;
    esac
done
if [ -z "$sflag" ]; then
    sep='%'
fi
shift $((OPTIND - 1))

sep_escaped=$(printf '%s\n' "$sep" | sed 's/[\/&]/\\&/g')

sed_args=()
for arg in "$@"; do
    arg_escaped=$(printf '%s\n' "$arg" | sed 's/[\/&]/\\&/g')
    sed_args+=(-e "s/$arg_escaped/$sep_escaped&/g")
done

sed "${sed_args[@]}" | column -t -s"$sep_escaped" -o' '