From a7f18a1e3b3e98192a83c4eb9f9c08d1552cf547 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Tue, 24 Sep 2019 14:33:15 +0300 Subject: [PATCH] pfetch: dynamic ascii width --- pfetch | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/pfetch b/pfetch index 0644ee9..2e15cf0 100755 --- a/pfetch +++ b/pfetch @@ -69,8 +69,16 @@ log() { # # '\033[6C': Move cursor 6 characters to the right. # This aligns the info. - printf '\033[14C\033[3%s;1m%s\033[m%s\033[3%sm\033[%sD\033[6C%s\033[m\n' \ - "${PF_COL1:-5}" "$1" "${PF_SEP:- }" "${PF_COL2:-7}" "${#1}" "${2:-?}" + # + # This is ugly, I know! + printf '\033[%sC\033[3%s;1m%s\033[m%s\033[3%sm\033[%sD\033[6C%s\033[m\n' \ + "${ascii_width:--1}" \ + "${PF_COL1:-5}" \ + "$1" \ + "${PF_SEP:- }" \ + "${PF_COL2:-7}" \ + "${#1}" \ + "${2:-?}" # Keep track of the number of times 'log()' has been run. info_height=$((info_height + 1)) @@ -241,11 +249,26 @@ ${c5}\/${c4}-____${c5}\/ ;; esac - # Store the height of the ascii art for cursor positioning. - # This script prints to the screen *almost* like a TUI does. - # It uses escape sequences to allow dynamic printing of the - # information through user configuration. - ascii_height=$(printf %s "$ascii" | wc -l) + # Store the "width" (longest line) and "height" (number of lines) + # of the ascii art for positioning. This script prints to the screen + # *almost* like a TUI does. It uses escape sequences to allow dynamic + # printing of the information through user configuration. + # + # Iterate over each line of the ascii art to retrieve the above + # information. The 'sed' is used to strip '\033[3Xm' color codes from + # the ascii art so they don't affect the width variable. + # + # The " " acts as the padding between the ascii art and the text as + # it appends 3 spaces to the end of each line. + while read -r line || [ -n "$line" ]; do + ascii_height=$((ascii_height + 1)) + ascii_width=$((${#line} > ascii_width ? ${#line} : ascii_width)) + done <<-EOF + $(printf %s "$ascii" | sed 's/\[3.m//g') + EOF + + # Add a gap between the ascii art and the information. + ascii_width=$((ascii_width + 4)) # Print the ascii art and position the cursor back where we # started prior to printing it. @@ -275,9 +298,9 @@ main() { # Store the output of 'uname' to avoid calling it multiple times # throughout the script. 'read <