Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Last active September 24, 2022 07:37
Show Gist options
  • Save oguz-ismail/a4ec7601c6a867b2dd5089b0f6ba01b5 to your computer and use it in GitHub Desktop.
Save oguz-ismail/a4ec7601c6a867b2dd5089b0f6ba01b5 to your computer and use it in GitHub Desktop.
columNATe
{
item[NR] = $0
itemw[NR] = length
}
END {
if (NR == 0)
exit
if (width == 0)
width = 80
for (rows = 1; rows < NR; rows++) {
w = -padding
col = 1
for (i = 1; i <= NR && w <= width; i += rows) {
colw[col] = maxw(i, i + rows)
w += colw[col] + padding
col++
}
if (w <= width)
break
}
for (row = 1; row <= rows; row++) {
p = 0
col = 1
for (i = row; i <= NR; i += rows) {
printf "%*s", p, ""
printf "%s", item[i]
p = colw[col] - itemw[i] + padding
col++
}
printf "\n"
}
}
function maxw(i, j, w) {
for (; i < j; i++)
if (w < itemw[i])
w = itemw[i]
return w
}
{
item[NR] = $0
itemw[NR] = length
}
END {
if (NR == 0)
exit
if (width == 0)
width = 80
if (padding == 0 || (cols = (width / padding) + 1) > NR)
cols = NR
for (; cols > 1; cols--) {
w = (cols - 1) * padding
for (col = 1; col <= cols && w <= width; col++)
w += colw[col] = maxw(col)
if (w <= width)
break
}
col = 1
for (i = 1; i <= NR; i++) {
if (col != 1)
printf "%*s", padding, ""
if (col == cols || i == NR) {
printf "%s\n", item[i]
col = 1
}
else {
printf "%-*s", colw[col], item[i]
col++
}
}
}
function maxw(col, i, w) {
for (i = col; i <= NR; i += cols)
if (w < itemw[i])
w = itemw[i]
return w
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment