forked from HariSekhon/DevOps-Bash-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcenter.sh
executable file
·31 lines (27 loc) · 892 Bytes
/
center.sh
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
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2016-01-23 00:02:17 +0000 (Sat, 23 Jan 2016)
#
# https://github.com/harisekhon/bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback
#
# https://www.linkedin.com/in/harisekhon
#
# borrowed from here:
#
# http://codereview.stackexchange.com/questions/94449/text-centering-function-in-bash
# This is only for local use, there is a much better Python version in my DevOps Python Tools repo:
#
# https://github.com/harisekhon/devops-python-tools
set -euo pipefail
textsize="${#1}"
# I want this to only match my hr() function, not 27" iMac 5K screens
#width=$(tput cols)
width="${2:-${WIDTH:-80}}"
span="$(((width + textsize) / 2))"
printf "%${span}s\n" "${1:-NO_MESSAGE_GIVEN_TO_CENTER}"