pfetch: dynamic info alignment

This commit is contained in:
Dylan Araps 2019-09-24 14:41:20 +03:00
parent a7f18a1e3b
commit 37e651d4dc

25
pfetch
View File

@ -71,14 +71,15 @@ log() {
# This aligns the info. # This aligns the info.
# #
# This is ugly, I know! # 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' \ printf '\033[%sC\033[3%s;1m%s\033[m%s\033[3%sm\033[%sD\033[%sC%s\033[m\n' \
"${ascii_width:--1}" \ "${ascii_width:--1}" \
"${PF_COL1:-5}" \ "${PF_COL1:-5}" \
"$1" \ "$1" \
"${PF_SEP:- }" \ "${PF_SEP:- }" \
"${PF_COL2:-7}" \ "${PF_COL2:-7}" \
"${#1}" \ "${#1}" \
"${2:-?}" "${PF_ALIGN:-$info_length}" \
"$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))
@ -103,13 +104,10 @@ get_title() {
# to the OS specific detection above and finally an additional fallback # to the OS specific detection above and finally an additional fallback
# to the 'hostname' command. # to the 'hostname' command.
# #
# PF_SEP and PF_COL{1,2} change printing options in the 'log()' function.
#
# Disable the warning about '$HOSTNAME' being undefined in POSIX sh as # Disable the warning about '$HOSTNAME' being undefined in POSIX sh as
# it is intended for allowing the user to overwrite the value on invocation. # it is intended for allowing the user to overwrite the value on invocation.
# shellcheck disable=SC2039 # shellcheck disable=SC2039
PF_SEP=@ PF_COL1=1 PF_COL2='3;1' \ log "$c4${USER:-$(whoami)}$c7@$c4${HOSTNAME:-${hostname:-$(hostname)}}"
log "${USER:-$(whoami)}" "${HOSTNAME:-${hostname:-$(hostname)}}"
} }
get_distro() { get_distro() {
@ -166,7 +164,7 @@ get_uptime() {
log uptime "${uptime:-0m}" log uptime "${uptime:-0m}"
} }
get_packages() { get_pkgs() {
# Simple function to avoid '>/dev/null' spam. # Simple function to avoid '>/dev/null' spam.
# This checks to see if a command is in '$PATH'. # This checks to see if a command is in '$PATH'.
has() { command -v "$1" >/dev/null; } has() { command -v "$1" >/dev/null; }
@ -309,10 +307,19 @@ main() {
# Disable globbing and set the positional parameters to the # Disable globbing and set the positional parameters to the
# contents of 'PF_INFO'. # contents of 'PF_INFO'.
set -f set -f
set +f ${PF_INFO-ascii title distro host kernel uptime packages memory} set +f ${PF_INFO-ascii title distro host kernel uptime pkgs memory}
# Iterate over the info functions to determine the lengths of the
# "info names" for output alignment. The option names and subtitles
# match 1:1 so this is thankfully simple.
for info; do
info_length=$((${#info} > info_length ? ${#info} : info_length))
done
# Iterate over the above list and run any existing "get_" functions. # Iterate over the above list and run any existing "get_" functions.
for info; do "get_$info"; done for info; do
"get_$info"
done
} }
# Position the cursor below both the ascii art and information lines # Position the cursor below both the ascii art and information lines