# -*- shell-script -*- # Copyright (C) 2008, 2010, 2011 Rocky Bernstein rocky@gnu.org # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; either version 2, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; see the file COPYING. If not, write to # the Free Software Foundation, 59 Temple Place, Suite 330, Boston, # MA 02111 USA. # # Code ported from my Ruby code which is in turn ported from a routine # from Python. # columnize a blank-delmited string $1 with maximum column width $2, # separate columns with $3. The column width defaults to 80 and the # column separator is two spaces. However if we are using ksh93t or # greater, then $1 is where the return value is to go and the other # parameters are shifted by 1: $2 contains the list to columnize and # $3 the maximum width, etc. columnize() { typeset -i displaywidth=${1:-80} (($# < 2)) && typeset colsep=' ' || typeset colsep="$2" typeset -i list_size=${#list[@]} if ((list_size == 0)) ; then columnized=('') return fi if ((1 == list_size)); then columnized=("${list[0]}") return fi # Consider arranging list in 1 rows total, then 2 rows... # Stop when at the smallest number of rows which # can be arranged less than the display width. typeset -i nrows=0 typeset -i ncols=0 typeset -a colwidths=() typeset -i i=0 for (( i=0; i= list_size)); then break fi typeset item="${list[j]}" ((colwidth < ${#item})) && colwidth=${#item} done colwidths+=($colwidth) ((totwidth+=colwidth + ${#colsep})) if ((totwidth > displaywidth)); then break fi done if ((totwidth <= displaywidth)); then break fi done # The smallest number of rows computed and the # max widths for each column has been obtained. # Now we just have to format each of the # rows. for (( row=0; row= list_size)); then item='' else item="${list[i]}" fi texts[$text_size]="$item" ((text_size++)) done while (( text_size > 0 )) && [[ ${texts[text_size-1]} == '' ]] ; do ((text_size--)) unset texts[$text_size] done text_row='' text_cell='' for (( col=0; col