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

177
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,106 +2972,114 @@ 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
"auto") ascii_distro="$(trim "$distro")" ;;
esac
# Get colors and bold # Distro detection
bold getdistro
colors case "${ascii_distro:-auto}" in
"auto") ascii_distro="$(trim "$distro")" ;;
esac
# If the script exits for any reason, unhide the cursor. # Get colors and bold
trap 'printf "\033[?25h"' EXIT bold
colors
# Clear the scren # Clear the scren
clear clear
# Hide the cursor # Hide the cursor
printf "\033[?25l" printf "\033[?25l"
if [ "$image" != "off" ]; then # Images {{{
# If iterm2 is detected use iterm2 backend.
if [ -n "$ITERM_PROFILE" ]; then
image_backend="iterm2"
elif [ "$(tycat 2>/dev/null)" ]; then # Get the image src
image_backend="tycat" if [ "$image" != "off" ]; then
# Set the image backend
if [ -n "$ITERM_PROFILE" ]; then
image_backend="iterm2"
else elif [ "$(tycat 2>/dev/null)" ]; then
image_backend="w3m" image_backend="tycat"
else
image_backend="w3m"
fi
# Find w3mimgdisplay
[ "$image_backend" == "w3m" ] && \
[ "$image" != "ascii" ] && \
getw3m_img_path
# Get the image
getimage
fi fi
# Find w3mimgdisplay # Display the image if enabled
[ "$image_backend" == "w3m" ] && \ if [ "$image" != "off" ] && [ "$image" != "ascii" ]; then
[ "$image" != "ascii" ] && \ case "$image_backend" in
getw3m_img_path "w3m")
printf "%b%s\n" "0;1;$xoffset;$yoffset;$width;$height;;;;;$img\n4;\n3;" |\
$w3m_img_path 2>/dev/null || padding="\033[0C"
;;
# Get the image "iterm2")
getimage printf "%b%s\a\n" "\033]1337;File=width=${width}px;height=${height}px;inline=1:$(base64 < "$img")"
fi ;;
# Display the image if enabled "tycat")
if [ "$image" != "off" ] && [ "$image" != "ascii" ]; then tycat "$img"
case "$image_backend" in ;;
"w3m") esac
printf "%b%s\n" "0;1;$xoffset;$yoffset;$width;$height;;;;;$img\n4;\n3;" |\ fi
$w3m_img_path 2>/dev/null || padding="\033[0C"
;;
"iterm2") # }}}
printf "%b%s\a\n" "\033]1337;File=width=${width}px;height=${height}px;inline=1:$(base64 < "$img")"
;;
"tycat") # Disable line wrap
tycat "$img" printf "\033[?7l"
;;
esac
fi
# Disable line wrap # Move cursor to the top
printf "\033[?7l" [ "$image" != "off" ] && printf "\033[0H"
# Move cursor to the top # Print the info
[ "$image" != "off" ] && printf "\033[0H" printinfo
# Print the info # Dynamic prompt location {{{
printinfo
# Dynamic prompt location {{{ # Get cursor position
info_height="$(stty -echo; IFS=';' read -rdR -t 1 -d c -p $'\033[6n\033[c' ROW COL; printf "%s" "${ROW#*[}"; stty echo)"
# Get cursor position # Calculate image height in terminal cells.
info_height="$(stty -echo; IFS=';' read -rdR -t 1 -d c -p $'\033[6n\033[c' ROW COL; printf "%s" "${ROW#*[}"; stty echo)" # The '+ 3' adds a gap between the prompt and the content.
[ "$image" != "ascii" ] && [ "$image" != "off" ] && \
lines="$((${height:-1} / ${font_height:-1} + 3))"
# Calculate image height in terminal cells. # If the info is higher than the ascii/image place the prompt
# The '+ 3' adds a gap between the prompt and the content. # based on the info height instead of the ascii/image height.
[ "$image" != "ascii" ] && [ "$image" != "off" ] && \ [ "${lines:-0}" -lt "${info_height:-0}" ] && lines="$info_height"
lines="$((${height:-1} / ${font_height:-1} + 3))"
# If the info is higher than the ascii/image place the prompt # Set the prompt location
# based on the info height instead of the ascii/image height. [ "$image" != "off" ] && printf "%b%s" "\033[${lines:-0}H"
[ "${lines:-0}" -lt "${info_height:-0}" ] && lines="$info_height"
# Set the prompt location # }}}
[ "$image" != "off" ] && printf "%b%s" "\033[${lines:-0}H"
# Re-enable line wrap
# }}} printf "%b%s" "\033[?7h"
# Re-enable line wrap # If enabled take a screenshot
printf "%b%s" "\033[?7h" [ "$scrot" == "on" ] && takescrot
# If enabled take a screenshot # Show error messages
if [ "$scrot" == "on" ]; then [ "$verbose" == "on" ] && printf "%s" "$err"
takescrot
fi # Reset exit status of the tests above.
printf "%s"
# Show error messages }
if [ "$verbose" == "on" ]; then
printf "%s" "$err" main
fi
# }}} # }}}