-
Notifications
You must be signed in to change notification settings - Fork 2
/
center
executable file
·77 lines (64 loc) · 1.29 KB
/
center
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash
# author="Nikhil Sing"
# email="[email protected]"
main() {
all=false
# ArgumentParser
while (( $# > 0 )); do
case "$1" in
-h | --help)
help
exit 0
;;
-a | --all)
all=true
shift
;;
*)
echo "Invalid argument $1"
help
exit 1
;;
esac
done
IFS=$'\n'$'\r'
content=()
while read -r line; do
content+=("$line")
done
# Call "cat" to initialize $LINES and $COLUMNS
cat /dev/null
(( upper_padding = (LINES - ${#content[@]}) / 2 ))
(( upper_padding < 1 )) && upper_padding=0
printf -v upper_padding '%*s' $upper_padding ''
printf '%b' "${upper_padding// /\\n}"
if $all; then
for i in "${content[@]}"; do
printf "%*s\n" $(((COLUMNS + ${#i}) / 2)) "$i"
done
else
getMaxlen "${content[@]}"
printf -v left_padding '%*s' $(((COLUMNS - max) / 2 )) ''
printf "$left_padding%s\n" "${content[@]}"
fi
printf '%b' "${upper_padding// /\\n}"
}
getMaxlen() {
for i in "$@"; do
if (( ${#i} > max )); then
max=${#i}
fi
done
}
help() {
cat <<EOF
This command will take input from pipe (stdout) and will center that data in terminal
flags:
-h, --help: show this help
-a, --all: will center every line individually (Horizontal center)
usage:
command | center-align
command | center-align -a
EOF
}
main "$@"