alinear textos en columnas

· osiux's blog

.org | .md | .gmi | .html

# tengo mis TOCs

Soy fan del texto plano, en gran parte por su simpleza, pero también por el monoespaciado y esto me genera algunos TOCs, el principal puede ser intentar alinear o "encolumnar" cada vez que puedo todo texto :P

# footnotes desordenadas

En mi blog hace tiempo que en lugar de utilizar links en línea, uso footnotes o notas al pie de página por varios motivos, en principio a medida que las voy definiendo no necesito la URL definitiva, solo un texto que me permita identificarlas y al final del texto puedo definir el link de cada footnote, pero sucede que casi siempre me quedan desalineadas!

Veamos un ejemplo:

 1grep -h '^\[fn:' 2014-08-12-clonar-pendrives.org
 2
 3[fn:openwrt] http://openwrt.org
 4[fn:mr3020] http://www.tp-link.com/en/products/details/?model=TL-MR3020
 5[fn:lviajeras] https://github.com/gcoop-libre/letras_viajeras
 6[fn:clonezilla] http://www.clonezilla.org/
 7[fn:ddtee] https://superuser.com/questions/145516/cloning-single-disk-drive-to-multiple-drives-simultaneously
 8[fn:eta] https://es.wikipedia.org/wiki/Tiempo_estimado_de_llegada
 9[fn:pssh] https://code.google.com/p/parallel-ssh/
10[fn:autoconfig] https://github.com/gcoop-libre/letras_viajeras/blob/master/config_ap/root/autoconfig.sh
11[fn:deploy] https://github.com/gcoop-libre/letras_viajeras/blob/master/config_ap/root/deploy.sh
12[fn:nginx] http://nginx.org
13

# align2col

Si bien para alinear, justificar y hasta jugar con párrafos utilizo el comando par 1, tenía ganas de resolver el problema con un script bash que fue muy fácil de generar:

 1#!/bin/bash
 2
 3# This script comes with ABSOLUTELY NO WARRANTY, use at own risk
 4# Copyright (C) 2021 Osiris Alejandro Gomez <osiux@osiux.com>
 5#
 6# This program is free software: you can redistribute it and/or modify
 7# it under the terms of the GNU General Public License as published by
 8# the Free Software Foundation, either version 3 of the License, or
 9# any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19if [[ ! -t 0 ]]
20then
21TMPI="$(mktemp)"
22
23true > "$TMPI"
24
25while IFS= read -r PIPED_INPUT
26do
27echo "$PIPED_INPUT" >> "$TMPI"
28done
29
30fi
31
32[[ ! -z "$1"    ]] && TMPI="$1"
33[[ ! -e "$TMPI" ]] && exit 1
34
35TOTA="$(awk '{print $1}' "$TMPI" | wc -L)"
36TOTB="$(awk '{print $2}' "$TMPI" | wc -L)"
37TOTC="$(awk '{print $3}' "$TMPI" | wc -L)"
38TOTD="$(awk '{print $4}' "$TMPI" | wc -L)"
39COLA=$((TOTA+1))
40COLB=$((TOTB+1))
41COLC=$((TOTC+1))
42COLD=$((TOTD+1))
43
44while read -r A B C D
45do
46printf "%-*s %-*s %-*s %-*s\\n" \
47"$COLA" "$A"             \
48"$COLB" "$B"             \
49"$COLC" "$C"             \
50"$COLD" "$D"
51done < "$TMPI"
52

# alinear footnotes en 2 columnas

Básicamente align2col obtiene máximo de caracteres de la 1er columna y se ocupa de alinear completando con espacios en blanco, esto facilita la lectura y pone un poco orden al caos reinante! :)

 1grep -h '^\[fn:' 2014-08-12-clonar-pendrives.org | align2col
 2
 3[fn:openwrt]     http://openwrt.org
 4[fn:mr3020]      http://www.tp-link.com/en/products/details/?model=TL-MR3020
 5[fn:lviajeras]   https://github.com/gcoop-libre/letras_viajeras
 6[fn:clonezilla]  http://www.clonezilla.org/
 7[fn:ddtee]       https://superuser.com/questions/145516/cloning-single-disk-drive-to-multiple-drives-simultaneously
 8[fn:eta]         https://es.wikipedia.org/wiki/Tiempo_estimado_de_llegada
 9[fn:pssh]        https://code.google.com/p/parallel-ssh/
10[fn:autoconfig]  https://github.com/gcoop-libre/letras_viajeras/blob/master/config_ap/root/autoconfig.sh
11[fn:deploy]      https://github.com/gcoop-libre/letras_viajeras/blob/master/config_ap/root/deploy.sh
12[fn:nginx]       http://nginx.org
13

# alinear desde vim

En vim es posible seleccionar un rango de líneas presionando shift + v, seleccionando las líneas mediante j o k para luego pasarlas como stdin a un comando externo escribiendo !align2col (en este caso) y obtendremos el resultado dentro del editor sin salir de él.

# alinear más columnas!

De momento el script soporta hasta 4 columnas, pero eso lo voy a mejorar en algún momento, por ejemplo:

acijado balazo cesarino despuesto
adulterio balume copista diserta
auspiciar brasilada canillero deportoso
ambular blondo caceo duc
algadara bisabuela cargar debelador

El resultado es mucho mas legible:

acijado    balazo     cesarino   despuesto
adulterio  balume     copista    diserta
auspiciar  brasilada  canillero  deportoso
ambular    blondo     caceo      duc
algadara   bisabuela  cargar     debelador

Seguro hay mil maneras de hacer esto, pero es muy gratificante poder idear y resolver un problema con tus propias líneas de código!

# ChangeLog