about summary refs log tree commit diff stats
path: root/wrap
blob: 011aa58443d0e048f015c29e3e85e655009acac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env bash

wrap() {
	local width="$1"
	local len=0

	while read -r -a line; do
		for word in "${line[@]}"; do
			((len += "${#word}" + 1))
			#printf '%s' "$len"
			if ((len >= width)); then
				printf '\n'
				# ruler "$width"
				len=${#word}
			fi
			printf '%s ' "$word"
		done
	done
	printf '\n'
}

ruler() {
	for ((i = 0; i < $1; i++)); do
		printf '%s' "${2:--}"
	done
	printf '\n'
}

if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
	wrap "$@"
fi