#!/bin/bash
# columnize.sh
# Take a list of values and output them in a nicely formatted column view.
# Author: Loïc Cattani "Arko" <loic cattani at gmail com>
# https://github.com/Arko/Columnize
values=($*)
longest_value=0
# Find the longest value
for value in ${values[@]}; do
if [[ ${#value} -gt $longest_value ]]; then
longest_value=${#value}
fi
done
# Compute column span
term_width=${COLUMNS:-$(tput cols)}
(( columns = $term_width / ($longest_value + 2) ))
# Print values with pretty column width
curr_col=0
for value in ${values[@]}; do
value_len=${#value}
echo -n $value
(( spaces_missing = $longest_value - $value_len + 2 ))
printf "%*s" $spaces_missing
(( curr_col++ ))
if [[ $curr_col == $columns ]]; then
echo
curr_col=0
fi
done
# Make sure there is a newline at the end
if [[ $curr_col != 0 ]]; then echo; fi
-
Michael Shigorin authored
The output was still somewhat ragged in 80x24 terminal window with fmt(1) which wasn't anticipating the word length difference subsequent column(1) would have to cope with later on. Thanks Loic Cattani for his shell columnizer implementation: https://github.com/Arko/Columnize
9fd0bc14