From 482e2659aef19691fd0196e6744bc7448e908757 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sat, 30 May 2020 13:35:32 -0500 Subject: Backup --- fold.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 fold.sh (limited to 'fold.sh') diff --git a/fold.sh b/fold.sh new file mode 100644 index 0000000..ee88c6e --- /dev/null +++ b/fold.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +fold() { + shopt -s checkwinsize + ( + : + : + ) + width="${1:-$COLUMNS}" + while read -r line; do + : "${line// / }" + IFS=$'\n' read -d "" -ra words <<<"${line// /$'\n'}" + ll=0 + for word in "${words[@]}"; do + wl="${#word}" + if ((ll + wl > width)); then + printf '\n' + else + ((ll += wl)) + printf '%s ' "$word" + fi + done + done +} + +fold "$@" -- cgit 1.4.1-21-gabe81