-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivider
executable file
·47 lines (36 loc) · 1.27 KB
/
divider
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
function divider {
local w b="$1" m="${2:--}" e="$3"
w=$(tput cols 2>/dev/null)
if [[ ! "$w" =~ ^[0-9]+$ ]]; then w=80; fi # just in case
if [[ "${#b}" -ge "$w" || "${#e}" -ge "$w" || "$((${#b}+${#e}))" -ge "$w" ]]; then
printf '%s%s\n' "$b" "$e"
return
fi
until [[ "${#m}" -ge "$w" ]]; do m+="$m"; done
printf '%s%s%s\n' "$b" "${m:0:$(( w - ${#b} - ${#e} ))}" "$e"
}
##### usage and testing ########################################################
# NOTE: for testing, adjust your terminal width to 80 (normally the default)
# testing boundaries
divider 1234567890123456789012345678901234567890123456789012345678901234567890123456789
divider 12345678901234567890123456789012345678901234567890123456789012345678901234567890
divider 123456789012345678901234567890123456789012345678901234567890123456789012345678901
# more examples
divider '-----[ file: /this/is/a/test.cfg ]-----' # force minimum header
divider 'Once more with feeling!'
divider \
"_.-'\"\"'-._[ Weeeeee! ]_.-'\"\"'-._" \
"_.-'\"\"'-._"
# right aligned
divider - - '[ test ]-----'
# both
divider '--[ left ]' =- '[ right ]--'
# fancy multiline examples
divider '' ' .-.-. '
divider '' '/ / \ \ '
divider '' "-' '-'"
divider '' " .-."
divider '' "'._.' "
# plain divider
divider