Move the commands near the bottom of the script to a function called main

This commit is contained in:
Dylan Araps 2016-08-12 20:36:48 +10:00
parent 2511511174
commit 7a774dd48d
1 changed files with 92 additions and 85 deletions

125
neofetch
View File

@ -2532,14 +2532,11 @@ getdefaultconfig () {
fi fi
} }
# Source config file
getdefaultconfig 2>/dev/null
# }}} # }}}
# Source Config {{{ # Source Config {{{
getconfig () { getuserconfig () {
# Check $config_file # Check $config_file
if [ -f "$config_file" ]; then if [ -f "$config_file" ]; then
source "$config_file" source "$config_file"
@ -2579,7 +2576,9 @@ case "$@" in
config="off" config="off"
;; ;;
esac esac
[ "${config:-on}" == "on" ] && getconfig 2>/dev/null
getdefaultconfig 2>/dev/null
[ "${config:-on}" == "on" ] && getuserconfig 2>/dev/null
# }}} # }}}
@ -2925,7 +2924,7 @@ while [ "$1" ]; do
--config) --config)
case "$2" in case "$2" in
"none" | "off") config="off" ;; "none" | "off") config="off" ;;
*) config_file="$2"; config="on"; getconfig 2>/dev/null ;; *) config_file="$2"; config="on"; getuserconfig 2>/dev/null ;;
esac esac
;; ;;
--test) --test)
@ -2973,30 +2972,34 @@ done
# Call Functions and Finish Up {{{ # Call Functions and Finish Up {{{
# Restore cursor and clear screen on ctrl+c main () {
trap 'printf "\033[?25h"; clear; exit' 2 # Restore cursor and clear screen on ctrl+c
trap 'printf "\033[?25h"; clear; exit' 2
# Distro detection # If the script exits for any reason, unhide the cursor.
getdistro trap 'printf "\033[?25h"' EXIT
case "${ascii_distro:-auto}" in
# Distro detection
getdistro
case "${ascii_distro:-auto}" in
"auto") ascii_distro="$(trim "$distro")" ;; "auto") ascii_distro="$(trim "$distro")" ;;
esac esac
# Get colors and bold # Get colors and bold
bold bold
colors colors
# If the script exits for any reason, unhide the cursor. # Clear the scren
trap 'printf "\033[?25h"' EXIT clear
# Clear the scren # Hide the cursor
clear printf "\033[?25l"
# Hide the cursor # Images {{{
printf "\033[?25l"
if [ "$image" != "off" ]; then # Get the image src
# If iterm2 is detected use iterm2 backend. if [ "$image" != "off" ]; then
# Set the image backend
if [ -n "$ITERM_PROFILE" ]; then if [ -n "$ITERM_PROFILE" ]; then
image_backend="iterm2" image_backend="iterm2"
@ -3014,10 +3017,10 @@ if [ "$image" != "off" ]; then
# Get the image # Get the image
getimage getimage
fi fi
# Display the image if enabled # Display the image if enabled
if [ "$image" != "off" ] && [ "$image" != "ascii" ]; then if [ "$image" != "off" ] && [ "$image" != "ascii" ]; then
case "$image_backend" in case "$image_backend" in
"w3m") "w3m")
printf "%b%s\n" "0;1;$xoffset;$yoffset;$width;$height;;;;;$img\n4;\n3;" |\ printf "%b%s\n" "0;1;$xoffset;$yoffset;$width;$height;;;;;$img\n4;\n3;" |\
@ -3032,47 +3035,51 @@ if [ "$image" != "off" ] && [ "$image" != "ascii" ]; then
tycat "$img" tycat "$img"
;; ;;
esac esac
fi fi
# Disable line wrap # }}}
printf "\033[?7l"
# Move cursor to the top # Disable line wrap
[ "$image" != "off" ] && printf "\033[0H" printf "\033[?7l"
# Print the info # Move cursor to the top
printinfo [ "$image" != "off" ] && printf "\033[0H"
# Dynamic prompt location {{{ # Print the info
printinfo
# Get cursor position # Dynamic prompt location {{{
info_height="$(stty -echo; IFS=';' read -rdR -t 1 -d c -p $'\033[6n\033[c' ROW COL; printf "%s" "${ROW#*[}"; stty echo)"
# Calculate image height in terminal cells. # Get cursor position
# The '+ 3' adds a gap between the prompt and the content. info_height="$(stty -echo; IFS=';' read -rdR -t 1 -d c -p $'\033[6n\033[c' ROW COL; printf "%s" "${ROW#*[}"; stty echo)"
[ "$image" != "ascii" ] && [ "$image" != "off" ] && \
# Calculate image height in terminal cells.
# The '+ 3' adds a gap between the prompt and the content.
[ "$image" != "ascii" ] && [ "$image" != "off" ] && \
lines="$((${height:-1} / ${font_height:-1} + 3))" lines="$((${height:-1} / ${font_height:-1} + 3))"
# If the info is higher than the ascii/image place the prompt # If the info is higher than the ascii/image place the prompt
# based on the info height instead of the ascii/image height. # based on the info height instead of the ascii/image height.
[ "${lines:-0}" -lt "${info_height:-0}" ] && lines="$info_height" [ "${lines:-0}" -lt "${info_height:-0}" ] && lines="$info_height"
# Set the prompt location # Set the prompt location
[ "$image" != "off" ] && printf "%b%s" "\033[${lines:-0}H" [ "$image" != "off" ] && printf "%b%s" "\033[${lines:-0}H"
# }}} # }}}
# Re-enable line wrap # Re-enable line wrap
printf "%b%s" "\033[?7h" printf "%b%s" "\033[?7h"
# If enabled take a screenshot # If enabled take a screenshot
if [ "$scrot" == "on" ]; then [ "$scrot" == "on" ] && takescrot
takescrot
fi # Show error messages
[ "$verbose" == "on" ] && printf "%s" "$err"
# Show error messages
if [ "$verbose" == "on" ]; then # Reset exit status of the tests above.
printf "%s" "$err" printf "%s"
fi }
main
# }}} # }}}