Menu

[69de4e]: / lib / columnize.sh  Maximize  Restore  History

Download this file

146 lines (138 with data), 4.2 kB

  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
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- 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=('<empty>')
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; i++ )) ; do
typeset -a colwidths=()
((nrows++))
((ncols=(list_size + nrows-1) / nrows))
typeset -i totwidth=-${#colsep}
typeset -i col
for (( col=0; col<=(ncols-1); col++ )); do
# get max column width for this column
colwidth=0
typeset -i row
for (( row=0; row<=(nrows-1); row++ )); do
typeset -i j
((j=row + nrows*col)) # [rows, cols]
if ((j >= 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<nrows; row++ )); do
typeset -i text_size=0
typeset -a texts=()
for ((col=0; col<ncols; col++)); do
((i=row + nrows*col))
if ((i >= 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<text_size; col++ )); do
fmt="%-${colwidths[col]}s"
text_cell=$(printf $fmt "${texts[col]}")
text_row+="${text_cell}"
((col != text_size-1)) && text_row+="${colsep}"
done
columnized+=("$text_row")
done
}
if [[ $0 == ${BASH_SOURCE[0]} ]] ; then
#
print_columns() {
unset columnized
typeset -a columnized
typeset -a list
list=($1)
columnize $2 $3
typeset -i i
echo '==============='
for ((i=0; i<${#columnized[@]}; i++)) ; do
printf "${columnized[$i]}\n"
done
}
print_columns
print_columns ''
print_columns oneitem
print_columns 'a 2 c' 10 ', '
print_columns \
' one two three
4ne 5wo 6hree
7ne 8wo 9hree
10e 11o 12ree' 18
print_columns \
' 1 two three
for 5 six
7 8' 12
print_columns \
' 1 two 3
for 5 six
7 8' 12
fi
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.