improved centering
Akshay nerdypepper@tuta.io
Sun, 02 Feb 2020 17:52:46 +0530
1 files changed,
21 insertions(+),
5 deletions(-)
jump to
M
shlide
→
shlide
@@ -19,8 +19,20 @@ # See: https://github.com/dylanaraps/pure-bash-bible#strip-pattern-from-start-of-string
printf '%s\n' "${1##$2}" } -get_term_size() { +lines() { + mapfile -tn 0 lines < "$1" + printf '%s\n' "${#lines[@]}" +} + +longest_line() { + max=0 IFS= + while read -r line; do + if [ "${#line}" -gt "$max" ]; then max="${#line}"; fi + done < "$1" + printf '%s\n' "$max" +} +get_term_size() { # POSIX alternative to 'checkwinsize'. read -r LINES COLUMNS < <(stty -F /dev/tty size)@@ -45,11 +57,15 @@ # Clear the screen.
printf '\e[2J' # Move the cursor to the center. - get_term_size + read -r LINES COLUMNS < <(stty -F /dev/tty size) + height=$(lines "$2") + width=$(longest_line "$2") + echo "$height $width" >> log.txt # Rough estimates for the true center. - ((l=0)) - ((c=0)) + ((l=$LINES/2 - $height/2)) + ((c=$COLUMNS/2 - $width/2)) + printf '\e[%s;%sH' "$l" "$c" while IFS= read -r line; do@@ -73,7 +89,7 @@ for f in "$slides_dir"/[0-9]*.txt; do
f_contents="$(<$f)" display "$f_contents" "$f" done - + # Return the cursor. printf '\e[?25h'