Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DietPi-Banner | Word wrap for small terminals #5820

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from
120 changes: 83 additions & 37 deletions dietpi/func/dietpi-banner
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@
'Large hostname'
'Print credits'
'Let'\''s Encrypt cert status'
'Word wrap for small terminals?'
)

# Set defaults: Disable CPU temp by default in VMs
if (( $G_HW_MODEL == 20 ))
then
aENABLED=(1 0 0 0 0 1 0 1 0 0 0 1 1 0 0 1 0)
aENABLED=(1 0 0 0 0 1 0 1 0 0 0 1 1 0 0 1 0 0)
else
aENABLED=(1 0 1 0 0 1 0 0 0 0 0 1 1 0 0 1 0)
aENABLED=(1 0 1 0 0 1 0 0 0 0 0 1 1 0 0 1 0 0)
fi

COLOUR_RESET='\e[0m'
Expand All @@ -80,10 +81,53 @@
# shellcheck disable=SC1090
[[ -f $FP_SAVEFILE ]] && . "$FP_SAVEFILE"

GREEN_LINE=" ${aCOLOUR[0]}─────────────────────────────────────────────────────$COLOUR_RESET"
TERM_WIDTH=$(tput cols)
printf -v GREEN_LINE "%$(( $TERM_WIDTH - 2 ))s" # Separator line with 1 space left and right respectively
GREEN_LINE=" ${aCOLOUR[0]}${GREEN_LINE//?/─}$COLOUR_RESET"
GREEN_BULLET=" ${aCOLOUR[0]}-$COLOUR_RESET"
GREEN_SEPARATOR="${aCOLOUR[0]}:$COLOUR_RESET"

# Print a banner line split into title and description with word-wrapping
shopt -s extglob # globally extended globbing for "*(pattern)" syntax
Print_Line()
{
local title=$1 # Bold part before including ":"
local descp=$2 # Normal part after ":"

if (( ${aENABLED[16]} == 1 ))
then
# Strip control codes to accurately measure visual length
local stripped_title=${title//\\e[[0-9]*([;0-9])m}
local stripped_descp=${descp//\\e[[0-9]*([;0-9])m}
local title_len=${#stripped_title}
local descp_len=${#stripped_descp}

# Wrap title if required
if (( $title_len >= $TERM_WIDTH ))
then
title=$(echo "$stripped_title" | fold -s -w "$TERM_WIDTH")
title=${title//$'\n'/$'\n' } # Add 3 spaces padding for hyphen
title="${aCOLOUR[1]}$title$COLOUR_RESET"
# Recalculate the title length based on the last line with padding
title_len=$(( ($title_len + 3) % $TERM_WIDTH + 3 ))
fi

# Obtain remaining length for description starting 1 space after title
local avail_len=$(( $TERM_WIDTH - $title_len - 1))

# Wrap description if required
if (( $descp_len >= $avail_len ))
then
descp=$(echo "$stripped_descp" | fold -s -w "$avail_len")
local left_pad
printf -v left_pad "%$(( $title_len + 1 ))s"
descp=${descp//$'\n'/$'\n'$left_pad}
fi
fi

echo -e "$title $descp"
}

DIETPI_VERSION="$G_DIETPI_VERSION_CORE.$G_DIETPI_VERSION_SUB.$G_DIETPI_VERSION_RC"
[[ $G_GITBRANCH == 'master' ]] || DIETPI_VERSION+=" ($G_GITBRANCH)"

Expand Down Expand Up @@ -165,62 +209,65 @@
local text_update_available_date=$(LC_ALL=${locale:-C.UTF-8} date '+%R - %a %x')
fi

echo -e "$GREEN_LINE
${aCOLOUR[1]}DietPi v$DIETPI_VERSION$COLOUR_RESET $GREEN_SEPARATOR $text_update_available_date$COLOUR_RESET
$GREEN_LINE"
echo -e "$GREEN_LINE"
Print_Line " ${aCOLOUR[1]}DietPi v$DIETPI_VERSION$COLOUR_RESET $GREEN_SEPARATOR" "$text_update_available_date$COLOUR_RESET"
echo -e "$GREEN_LINE"
}

Print_Local_Ip()
{
[[ ${aENABLED[5]} == 1 ]] || return 0
local iface=$(G_GET_NET -q iface)
local ip=$(G_GET_NET -q ip)
echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[5]} $GREEN_SEPARATOR ${ip:-Use dietpi-config to setup a connection} (${iface:-NONE})"
Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[5]} $GREEN_SEPARATOR" "${ip:-Use dietpi-config to setup a connection} (${iface:-NONE})"
}

Print_Credits()
{
echo -e " ${aCOLOUR[2]}DietPi Team : https://github.com/MichaIng/DietPi#the-dietpi-project-team"
Print_Line " ${aCOLOUR[2]}DietPi Team :" 'https://github.com/MichaIng/DietPi#the-dietpi-project-team'

[[ -f '/boot/dietpi/.prep_info' ]] && mawk 'NR==1 {sub(/^0$/,"DietPi Core Team");a=$0} NR==2 {print " Image by : "a" (pre-image: "$0")"}' /boot/dietpi/.prep_info
if [[ -f '/boot/dietpi/.prep_info' ]]; then
local info=$(mawk 'NR==1 {sub(/^0$/,"DietPi Core Team");a=$0} NR==2 {print ""a" (pre-image: "$0")"}' /boot/dietpi/.prep_info)
Print_Line ' Image by :' "$info"
fi

echo -e " Patreon Legends : Camry2731, Chris Gelatt
Website : https://dietpi.com/ | https://twitter.com/DietPi_
Contribute : https://dietpi.com/contribute.html
Web Hosting by : https://myvirtualserver.com$COLOUR_RESET\n"
Print_Line ' Patreon Legends :' 'Camry2731, Chris Gelatt'
Print_Line ' Website :' 'https://dietpi.com/ | https://twitter.com/DietPi_'
Print_Line ' Contribute :' 'https://dietpi.com/contribute.html'
Print_Line ' Web Hosting by :' "https://myvirtualserver.com$COLOUR_RESET\n"
}

Print_Updates()
{
# DietPi update available?
if [[ $AVAILABLE_UPDATE ]]
then
echo -e " ${aCOLOUR[1]}dietpi-update$COLOUR_RESET $GREEN_SEPARATOR ${aCOLOUR[3]}Run now to update DietPi from v$DIETPI_VERSION to v$AVAILABLE_UPDATE$COLOUR_RESET\n"
Print_Line " ${aCOLOUR[1]}dietpi-update$COLOUR_RESET $GREEN_SEPARATOR" "${aCOLOUR[3]}Run now to update DietPi from v$DIETPI_VERSION to v$AVAILABLE_UPDATE$COLOUR_RESET\n"

# New DietPi live patches available?
elif (( $LIVE_PATCHES ))
then
echo -e " ${aCOLOUR[1]}dietpi-update$COLOUR_RESET $GREEN_SEPARATOR ${aCOLOUR[3]}Run now to check out new available DietPi live patches$COLOUR_RESET\n"
Print_Line " ${aCOLOUR[1]}dietpi-update$COLOUR_RESET $GREEN_SEPARATOR" "${aCOLOUR[3]}Run now to check out new available DietPi live patches$COLOUR_RESET\n"

# APT updates available?
elif (( $PACKAGE_COUNT ))
then
echo -e " ${aCOLOUR[1]}apt upgrade$COLOUR_RESET $GREEN_SEPARATOR ${aCOLOUR[3]}Run now to apply $PACKAGE_COUNT available APT package upgrades$COLOUR_RESET\n"
Print_Line " ${aCOLOUR[1]}apt upgrade$COLOUR_RESET $GREEN_SEPARATOR" "${aCOLOUR[3]}Run now to apply $PACKAGE_COUNT available APT package upgrades$COLOUR_RESET\n"

# Reboot required to finalise kernel upgrade?
elif (( $REBOOT_REQUIRED ))
then
echo -e " ${aCOLOUR[1]}reboot$COLOUR_RESET $GREEN_SEPARATOR ${aCOLOUR[3]}A reboot is required to finalise a recent kernel upgrade$COLOUR_RESET\n"
Print_Line " ${aCOLOUR[1]}reboot$COLOUR_RESET $GREEN_SEPARATOR" "${aCOLOUR[3]}A reboot is required to finalise a recent kernel upgrade$COLOUR_RESET\n"
fi
}

Print_Useful_Commands()
{
echo -e " ${aCOLOUR[1]}dietpi-launcher$COLOUR_RESET $GREEN_SEPARATOR All the DietPi programs in one place
${aCOLOUR[1]}dietpi-config$COLOUR_RESET $GREEN_SEPARATOR Feature rich configuration tool for your device
${aCOLOUR[1]}dietpi-software$COLOUR_RESET $GREEN_SEPARATOR Select optimised software for installation
${aCOLOUR[1]}htop$COLOUR_RESET $GREEN_SEPARATOR Resource monitor
${aCOLOUR[1]}cpu$COLOUR_RESET $GREEN_SEPARATOR Shows CPU information and stats\n"
Print_Line " ${aCOLOUR[1]}dietpi-launcher$COLOUR_RESET $GREEN_SEPARATOR" 'All the DietPi programs in one place'
Print_Line " ${aCOLOUR[1]}dietpi-config$COLOUR_RESET $GREEN_SEPARATOR" 'Feature rich configuration tool for your device'
Print_Line " ${aCOLOUR[1]}dietpi-software$COLOUR_RESET $GREEN_SEPARATOR" 'Select optimised software for installation'
Print_Line " ${aCOLOUR[1]}htop$COLOUR_RESET $GREEN_SEPARATOR" 'Resource monitor'
Print_Line " ${aCOLOUR[1]}cpu$COLOUR_RESET $GREEN_SEPARATOR" 'Shows CPU information and stats\n'
}

Print_Banner()
Expand All @@ -232,27 +279,25 @@ $GREEN_LINE"
# shellcheck disable=SC1091
(( ${aENABLED[14]} == 1 )) && . /boot/dietpi/func/dietpi-print_large "$(</etc/hostname)" && echo
# Device model
(( ${aENABLED[0]} == 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[0]} $GREEN_SEPARATOR $G_HW_MODEL_NAME"
(( ${aENABLED[0]} == 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[0]} $GREEN_SEPARATOR" "$G_HW_MODEL_NAME"
# Uptime
(( ${aENABLED[1]} == 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[1]} $GREEN_SEPARATOR $(uptime -p 2>&1)"
(( ${aENABLED[1]} == 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[1]} $GREEN_SEPARATOR" "$(uptime -p 2>&1)"
# CPU temp
(( ${aENABLED[2]} == 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[2]} $GREEN_SEPARATOR $(print_full_info=1 G_OBTAIN_CPU_TEMP 2>&1)"
(( ${aENABLED[2]} == 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[2]} $GREEN_SEPARATOR" "$(print_full_info=1 G_OBTAIN_CPU_TEMP 2>&1)"
# Hostname
(( ${aENABLED[3]} == 1 && ${aENABLED[14]} != 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[3]} $GREEN_SEPARATOR $(</etc/hostname)"
(( ${aENABLED[3]} == 1 && ${aENABLED[14]} != 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[3]} $GREEN_SEPARATOR" "$(</etc/hostname)"
# NIS/YP domainname
(( ${aENABLED[4]} == 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[4]} $GREEN_SEPARATOR $(hostname -y 2>&1)"
(( ${aENABLED[4]} == 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[4]} $GREEN_SEPARATOR" "$(hostname -y 2>&1)"
# LAN IP
Print_Local_Ip
# WAN IP + location info
(( ${aENABLED[6]} == 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[6]} $GREEN_SEPARATOR $(G_GET_WAN_IP 2>&1)"
(( ${aENABLED[6]} == 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[6]} $GREEN_SEPARATOR" "$(G_GET_WAN_IP 2>&1)"
# DietPi-VPN connection status
(( ${aENABLED[13]} == 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[13]} $GREEN_SEPARATOR $(/boot/dietpi/dietpi-vpn status 2>&1)"
(( ${aENABLED[13]} == 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[13]} $GREEN_SEPARATOR" "$(/boot/dietpi/dietpi-vpn status 2>&1)"
# Freespace (RootFS)
(( ${aENABLED[7]} == 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[7]} $GREEN_SEPARATOR $(df -h --output=avail / | mawk 'NR==2 {print $1}' 2>&1)"
(( ${aENABLED[7]} == 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[7]} $GREEN_SEPARATOR" "$(df -h --output=avail / | mawk 'NR==2 {print $1}' 2>&1)"
# Freespace (DietPi userdata)
(( ${aENABLED[8]} == 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[8]} $GREEN_SEPARATOR $(df -h --output=avail /mnt/dietpi_userdata | mawk 'NR==2 {print $1}' 2>&1)"
# Weather
(( ${aENABLED[9]} == 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[9]} $GREEN_SEPARATOR $(curl -sSfLm 3 'https://wttr.in/?format=4' 2>&1)"
(( ${aENABLED[8]} == 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[8]} $GREEN_SEPARATOR" "$(df -h --output=avail /mnt/dietpi_userdata | mawk 'NR==2 {print $1}' 2>&1)"
# Let's Encrypt cert status
if (( ${aENABLED[16]} == 1 ))
then
Expand All @@ -263,17 +308,19 @@ $GREEN_LINE"
certinfo=$(openssl x509 -enddate -noout -in "$i" | mawk '/notAfter=/{print "Valid until "$4"-"substr($1,10)"-"$2" "$3}' 2>&1)
break
done
echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[16]} $GREEN_SEPARATOR $certinfo"
Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[16]} $GREEN_SEPARATOR" "$certinfo"
fi
# Weather
(( ${aENABLED[9]} == 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[9]} $GREEN_SEPARATOR" "$(curl -sSfLm 3 'https://wttr.in/?format=4' 2>&1)"
# Custom
(( ${aENABLED[10]} == 1 )) && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[10]} $GREEN_SEPARATOR $(bash "$FP_CUSTOM" 2>&1)"
(( ${aENABLED[10]} == 1 )) && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[10]} $GREEN_SEPARATOR" "$(bash "$FP_CUSTOM" 2>&1)"
# MOTD
if (( ${aENABLED[12]} == 1 ))
then
local motd fp_motd='/run/dietpi/.dietpi_motd'
[[ -f $fp_motd ]] || curl -sSfLm 3 'https://dietpi.com/motd' -o "$fp_motd"
# shellcheck disable=SC1090
[[ -f $fp_motd ]] && . "$fp_motd" &> /dev/null && [[ $motd ]] && echo -e "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[12]} $GREEN_SEPARATOR $motd"
[[ -f $fp_motd ]] && . "$fp_motd" &> /dev/null && [[ $motd ]] && Print_Line "$GREEN_BULLET ${aCOLOUR[1]}${aDESCRIPTION[12]} $GREEN_SEPARATOR" "$motd"
fi
echo -e "$GREEN_LINE\n"

Expand Down Expand Up @@ -340,7 +387,6 @@ NB: It is executed as bash script, so it needs to be in bash compatible syntax.
Menu_Main
Print_Banner
fi

#-----------------------------------------------------------------------------------
exit 0
#-----------------------------------------------------------------------------------
Expand Down