Backup
[bollux.git/.git] / fold.sh
1 #!/usr/bin/env bash
2
3 fold() {
4         shopt -s checkwinsize
5         (
6                 :
7                 :
8         )
9         width="${1:-$COLUMNS}"
10         while read -r line; do
11                 : "${line//     / }"
12                 IFS=$'\n' read -d "" -ra words <<<"${line// /$'\n'}"
13                 ll=0
14                 for word in "${words[@]}"; do
15                         wl="${#word}"
16                         if ((ll + wl > width)); then
17                                 printf '\n'
18                         else
19                                 ((ll += wl))
20                                 printf '%s ' "$word"
21                         fi
22                 done
23         done
24 }
25
26 fold "$@"