diff options
Diffstat (limited to 'fold.sh')
-rw-r--r-- | fold.sh | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/fold.sh b/fold.sh new file mode 100644 index 0000000..ee88c6e --- /dev/null +++ b/fold.sh | |||
@@ -0,0 +1,26 @@ | |||
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 "$@" | ||