pfetch: Revert to larger but more reliable print method. Closes #46

This commit is contained in:
Dylan Araps 2020-03-11 22:39:16 +02:00
parent 2ef3584f5b
commit 675814f33e
No known key found for this signature in database
GPG Key ID: 46D62DD9F1DE636E

162
pfetch
View File

@ -3,6 +3,59 @@
# pfetch - Simple POSIX sh fetch script. # pfetch - Simple POSIX sh fetch script.
log() { log() {
# The 'log()' function handles the printing of information.
# In 'pfetch' (and 'neofetch'!) the printing of the ascii art and info
# happen independently of each other.
#
# The size of the ascii art is stored and the ascii is printed first.
# Once the ascii is printed, the cursor is located right below the art
# (See marker $[1]).
#
# Using the stored ascii size, the cursor is then moved to marker $[2].
# This is simply a cursor up escape sequence using the "height" of the
# ascii art.
#
# 'log()' then moves the cursor to the right the "width" of the ascii art
# with an additional amount of padding to add a gap between the art and
# the information (See marker $[3]).
#
# When 'log()' has executed, the cursor is then located at marker $[4].
# When 'log()' is run a second time, the next line of information is
# printed, moving the cursor to marker $[5].
#
# Markers $[4] and $[5] repeat all the way down through the ascii art
# until there is no more information left to print.
#
# Every time 'log()' is called the script keeps track of how many lines
# were printed. When printing is complete the cursor is then manually
# placed below the information and the art according to the "heights"
# of both.
#
# The math is simple: move cursor down $((ascii_height - info_height)).
# If the aim is to move the cursor from marker $[5] to marker $[6],
# plus the ascii height is 8 while the info height is 2 it'd be a move
# of 6 lines downwards.
#
# However, if the information printed is "taller" (takes up more lines)
# than the ascii art, the cursor isn't moved at all!
#
# Once the cursor is at marker $[6], the script exits. This is the gist
# of how this "dynamic" printing and layout works.
#
# This method allows ascii art to be stored without markers for info
# and it allows for easy swapping of info order and amount.
#
# $[2] ___ $[3] goldie@KISS
# $[4](.· | $[5] os KISS Linux
# (<> |
# / __ \
# ( / \ /|
# _/\ __)/_)
# \/-____\/
# $[1]
#
# $[6] /home/goldie $
# End here if no data was found. # End here if no data was found.
[ "$2" ] || return [ "$2" ] || return
@ -22,10 +75,27 @@ log() {
info=$* info=$*
} }
# Construct the information string. # Move the cursor to the right, the width of the ascii art with an
out="[3${PF_COL1-4};1m${name}" # additional gap for text spacing.
out="$out${PF_SEP}[$((info_length-${#name}))C" printf '[%sC' "${ascii_width--1}"
out="$out[3${PF_COL2-7}m$info"
# Print the info name and color the text.
printf '[3%s;1m%s' "${PF_COL1-4}" "$name"
# Print the info name and info data separator.
printf %s "$PF_SEP"
# Move the cursor backward the length of the *current* info name and
# then move it forwards the length of the *longest* info name. This
# aligns each info data line.
printf '[%sD[%sC' "${#name}" "${PF_ALIGN-$info_length}"
# Print the info data, color it and strip all leading whitespace
# from the string.
printf '[3%sm%s\n' "${PF_COL2-7}" "$info"
# Keep track of the number of times 'log()' has been run.
info_height=$((${info_height:-0} + 1))
} }
get_title() { get_title() {
@ -42,7 +112,7 @@ get_title() {
# shellcheck disable=SC2039 # shellcheck disable=SC2039
hostname=${HOSTNAME:-${hostname:-$(hostname)}} hostname=${HOSTNAME:-${hostname:-$(hostname)}}
log "[3${PF_COL3:-1}m${user}${c7}@[3${PF_COL3:-1}m${hostname}" " " log "[3${PF_COL3:-1}m${user}${c7}@[3${PF_COL3:-1}m${hostname}" " " >&6
} }
get_os() { get_os() {
@ -53,7 +123,7 @@ get_os() {
# On first run, this function displays _nothing_, only on the second # On first run, this function displays _nothing_, only on the second
# invocation is 'log()' called. # invocation is 'log()' called.
[ "$distro" ] && { [ "$distro" ] && {
log os "$distro" log os "$distro" >&6
return return
} }
@ -216,18 +286,13 @@ get_kernel() {
case $os in case $os in
# Don't print kernel output on some systems as the # Don't print kernel output on some systems as the
# OS name includes it. # OS name includes it.
*BSD*|Haiku|Minix) ;; *BSD*|Haiku|Minix)
return
IRIX)
kernel=$(uname -vR)
kernel=${kernel#* }
;; ;;
esac
*) # '$kernel' is the cached output of 'uname -r'.
# '$kernel' is the cached output of 'uname -r'. log kernel "$kernel" >&6
log kernel "$kernel"
;;
esac
} }
get_host() { get_host() {
@ -296,7 +361,7 @@ get_host() {
done done
# '$arch' is the cached output from 'uname -m'. # '$arch' is the cached output from 'uname -m'.
log host "${host:-$arch}" log host "${host:-$arch}" >&6
} }
get_uptime() { get_uptime() {
@ -370,7 +435,7 @@ get_uptime() {
[ "$h" = 0 ] || uptime="${uptime}${h}h " [ "$h" = 0 ] || uptime="${uptime}${h}h "
[ "$m" = 0 ] || uptime="${uptime}${m}m " [ "$m" = 0 ] || uptime="${uptime}${m}m "
log uptime "${uptime:-0m}" log uptime "${uptime:-0m}" >&6
} }
get_pkgs() { get_pkgs() {
@ -482,7 +547,7 @@ get_pkgs() {
IRIX) packages=$((packages - 3)) ;; IRIX) packages=$((packages - 3)) ;;
esac esac
[ "$packages" -gt 1 ] && log pkgs "$packages" [ "$packages" -gt 1 ] && log pkgs "$packages" >&6
} }
get_memory() { get_memory() {
@ -685,7 +750,7 @@ get_memory() {
;; ;;
esac esac
log memory "${mem_used:-?}M / ${mem_full:-?}M" log memory "${mem_used:-?}M / ${mem_full:-?}M" >&6
} }
get_wm() { get_wm() {
@ -814,7 +879,7 @@ get_wm() {
;; ;;
esac esac
log wm "$wm" log wm "$wm" >&6
} }
@ -825,18 +890,18 @@ get_de() {
# #
# Display the value of '$XDG_CURRENT_DESKTOP', if it's empty, # Display the value of '$XDG_CURRENT_DESKTOP', if it's empty,
# display the value of '$DESKTOP_SESSION'. # display the value of '$DESKTOP_SESSION'.
log de "${XDG_CURRENT_DESKTOP:-$DESKTOP_SESSION}" log de "${XDG_CURRENT_DESKTOP:-$DESKTOP_SESSION}" >&6
} }
get_shell() { get_shell() {
# Display the basename of the '$SHELL' environment variable. # Display the basename of the '$SHELL' environment variable.
log shell "${SHELL##*/}" log shell "${SHELL##*/}" >&6
} }
get_editor() { get_editor() {
# Display the value of '$VISUAL', if it's empty, display the # Display the value of '$VISUAL', if it's empty, display the
# value of '$EDITOR'. # value of '$EDITOR'.
log editor "${VISUAL:-$EDITOR}" log editor "${VISUAL:-$EDITOR}" >&6
} }
get_palette() { get_palette() {
@ -851,7 +916,7 @@ get_palette() {
# Print the palette with a new-line before and afterwards. # Print the palette with a new-line before and afterwards.
printf '\n' >&6 printf '\n' >&6
log "$palette log "$palette
" " " " " " >&6
} }
get_ascii() { get_ascii() {
@ -1363,6 +1428,8 @@ get_ascii() {
# information. The 'sed' is used to strip 'm' color codes from # information. The 'sed' is used to strip 'm' color codes from
# the ascii art so they don't affect the width variable. # the ascii art so they don't affect the width variable.
while read -r line; do while read -r line; do
ascii_height=$((${ascii_height:-0} + 1))
# This was a ternary operation but they aren't supported in # This was a ternary operation but they aren't supported in
# Minix's shell. # Minix's shell.
[ "${#line}" -gt "${ascii_width:-0}" ] && [ "${#line}" -gt "${ascii_width:-0}" ] &&
@ -1378,6 +1445,13 @@ get_ascii() {
# Add a gap between the ascii art and the information. # Add a gap between the ascii art and the information.
ascii_width=$((ascii_width + 4)) ascii_width=$((ascii_width + 4))
# Print the ascii art and position the cursor back where we
# started prior to printing it.
# '[1m': Print the ascii in bold.
# '[m': Clear bold.
# '[%sA': Move the cursor up '$ascii_height' amount of lines.
printf '%s[%sA' "$ascii" "$ascii_height" >&6
} }
main() { main() {
@ -1444,10 +1518,6 @@ main() {
set -f set -f
set +f ${PF_INFO-ascii title os host kernel uptime pkgs memory} set +f ${PF_INFO-ascii title os host kernel uptime pkgs memory}
# Grab the ascii art if the user has enabled it. This simply
# populates the '$ascii' variable.
case $@ in *ascii*) get_ascii; shift; esac
# Iterate over the info functions to determine the lengths of the # Iterate over the info functions to determine the lengths of the
# "info names" for output alignment. The option names and subtitles # "info names" for output alignment. The option names and subtitles
# match 1:1 so this is thankfully simple. # match 1:1 so this is thankfully simple.
@ -1463,22 +1533,26 @@ main() {
# Add an additional space of length to act as a gap. # Add an additional space of length to act as a gap.
info_length=$((info_length + 1)) info_length=$((info_length + 1))
while IFS= read -r line || [ "$1" ]; do # Iterate over the above list and run any existing "get_" functions.
# Iterate over the info skipping any lines for info; do "get_$info"; done
# which are blank.
for info; do
"get_$1"
shift "$(($# ? 1 : 0))"
[ "$out" ] && break
done
printf '\033[%sC%s\033[m\r\033[1m%s\033[m\n' \
"$ascii_width" "$out" "$line" >&6
out=
done <<-EOF
$ascii
EOF
} }
# Position the cursor below both the ascii art and information lines
# according to the height of both. If the information exceeds the ascii
# art in height, don't touch the cursor (0/unset), else move it down
# N lines.
#
# This was a ternary operation but they aren't supported in Minix's shell.
[ "${info_height:-0}" -lt "${ascii_height:-0}" ] &&
cursor_pos=$((ascii_height - info_height))
# Print '$cursor_pos' amount of newlines to correctly position the
# cursor. This used to be a 'printf $(seq X X)' however 'seq' is only
# typically available (by default) on GNU based systems!
while [ "${i:=0}" -le "${cursor_pos:-0}" ]; do
printf '\n'
i=$((i + 1))
done >&6
} }
main "$@" main "$@"