pfetch: dynamic ascii width

This commit is contained in:
Dylan Araps 2019-09-24 14:33:15 +03:00
parent 4a0e7df041
commit a7f18a1e3b

43
pfetch
View File

@ -69,8 +69,16 @@ log() {
# #
# '\033[6C': Move cursor 6 characters to the right. # '\033[6C': Move cursor 6 characters to the right.
# This aligns the info. # 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. # Keep track of the number of times 'log()' has been run.
info_height=$((info_height + 1)) info_height=$((info_height + 1))
@ -241,11 +249,26 @@ ${c5}\/${c4}-____${c5}\/
;; ;;
esac esac
# Store the height of the ascii art for cursor positioning. # Store the "width" (longest line) and "height" (number of lines)
# This script prints to the screen *almost* like a TUI does. # of the ascii art for positioning. This script prints to the screen
# It uses escape sequences to allow dynamic printing of the # *almost* like a TUI does. It uses escape sequences to allow dynamic
# information through user configuration. # printing of the information through user configuration.
ascii_height=$(printf %s "$ascii" | wc -l) #
# 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 # Print the ascii art and position the cursor back where we
# started prior to printing it. # started prior to printing it.
@ -275,9 +298,9 @@ main() {
# Store the output of 'uname' to avoid calling it multiple times # Store the output of 'uname' to avoid calling it multiple times
# throughout the script. 'read <<EOF' is the simplest way of reading # throughout the script. 'read <<EOF' is the simplest way of reading
# a command into a list of variables. # a command into a list of variables.
read -r os kernel <<EOF read -r os kernel <<-EOF
$(uname -sr) $(uname -sr)
EOF EOF
# Allow the user to specify the order and inclusion of information # Allow the user to specify the order and inclusion of information
# functions through the 'PF_INFO' environment variable. # functions through the 'PF_INFO' environment variable.