From ac8b7beb9b94520fa363b5196726ae479edb68fb Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Mon, 17 Apr 2023 23:16:33 +0300 Subject: [PATCH] scripts: x-hr & x-welxome-banner --- local/bin/x-hr | 53 ++++++++++++++++++++++ local/bin/x-welcome-banner | 93 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100755 local/bin/x-hr create mode 100755 local/bin/x-welcome-banner diff --git a/local/bin/x-hr b/local/bin/x-hr new file mode 100755 index 0000000..aee609e --- /dev/null +++ b/local/bin/x-hr @@ -0,0 +1,53 @@ +#!/usr/bin/env bash +# Simple script to output a solid line in the terminal +# Useful for marking the end of a task in your bash log +# Inspired by @LuRsT's script of the same name +# Can be called directly, or source'd in *rc file +# +# Licensed under MIT, (C) Alicia Sykes 2022 +# See: https://github.com/Lissy93/dotfiles +# +# Modified by Ismo Vuorinen 2023 + +# Determine width of terminal +hr_col_count="$(tput cols)" +if [ -z "${hr_col_count+set}" ] || [ "$hr_col_count" -lt 1 ]; then + hr_col_count="${COLUMNS:-80}" +fi + +# Colors +CLR_RED="\033[1;31m" +hr_color="${hr_color:=$CLR_RED}" +hr_reset="\033[0m" + +# Prints the HR line +hr_draw_char() { + local CHAR="$1" + local LINE='' + LINE=$(printf "%*s" "$((hr_col_count - 2))") + LINE="${LINE// /${CHAR}}" + echo -e "◀${hr_color}${LINE:0:${hr_col_count}}${hr_reset}▶" +} + +# Passes param and calls hr() +hr() { + for WORD in "${@:--}"; do + hr_draw_char "$WORD" + done +} + +# Determine if file is being run directly or sourced +( + [[ -n $ZSH_EVAL_CONTEXT && $ZSH_EVAL_CONTEXT =~ :file$ ]] \ + || [[ -n $KSH_VERSION && $(cd "$(dirname -- "$0")" \ + && printf '%s' "${PWD%/}/")$(basename -- "$0") != "${.sh.file}" ]] \ + || [[ -n $BASH_VERSION ]] && (return 0 2>/dev/null) +) && sourced=1 || sourced=0 + +# Either instantiate immediately, or set alias for later +if [ "$sourced" -eq 0 ]; then + [ "$0" == "${BASH_SOURCE[0]}" ] && hr "$@" +else + export alias hr='hr' +fi + diff --git a/local/bin/x-welcome-banner b/local/bin/x-welcome-banner new file mode 100755 index 0000000..0e1e420 --- /dev/null +++ b/local/bin/x-welcome-banner @@ -0,0 +1,93 @@ +#!/usr/bin/env bash + +###################################################################### +# 🌞 Welcome Banner # +###################################################################### +# Prints personal greeting, system info and data about today # +# Intended for use as a MOTD, for when using multiple systems # +# For docs and more info, see: https://github.com/lissy93/dotfiles # +# # +# Licensed under MIT (C) Alicia Sykes 2022 # +###################################################################### + +# Formatting variables +COLOR_P='\033[1;36m' +COLOR_S='\033[0;36m' +RESET='\033[0m' + +# Print time-based personalized message, using figlet & lolcat if availible +function welcome_greeting () { + h=$(date +%H) + if [ "$h" -lt 04 ] || [[ $h -gt 22 ]]; + then greeting="Good Night" + elif [ "$h" -lt 12 ]; + then greeting="Good morning" + elif [ "$h" -lt 18 ]; + then greeting="Good afternoon" + elif [ "$h" -lt 22 ]; + then greeting="Good evening" + else + greeting="Hello" + fi + WELCOME_MSG="$greeting $USER!" + if hash lolcat 2>/dev/null && hash figlet 2>/dev/null; then + echo "${WELCOME_MSG}" | figlet | lolcat + else + echo -e "$COLOR_P${WELCOME_MSG}${RESET}\n" + fi +} + +# Print system information with neofetch, if it's installed +function welcome_sysinfo () { + if hash neofetch 2>/dev/null; then + neofetch --shell_version off \ + --disable kernel distro shell resolution de wm wm_theme theme icons terminal \ + --backend off \ + --colors 4 8 4 4 8 6 \ + --color_blocks off \ + --memory_display info + fi +} + +# Print todays info: Date, IP, weather, etc +function welcome_today () { + timeout=1 + echo -e "\033[1;34mToday\n------" + + # Print date time + echo -e "$COLOR_S$(date '+🗓️ Date: %A, %B %d, %Y at %H:%M')" + + # Print local weather + curl -s -m $timeout "https://wttr.in?format=%cWeather:+%C+%t,+%p+%w" + echo -e "${RESET}" + + # Print IP address + if hash ip 2>/dev/null; then + ip_address=$(ip route get 8.8.8.8 | awk -F"src " 'NR==1{split($2,a," ");print a[1]}') + ip_interface=$(ip route get 8.8.8.8 | awk -F"dev " 'NR==1{split($2,a," ");print a[1]}') + echo -e "${COLOR_S}🌐 IP: $(curl -s -m $timeout 'https://ipinfo.io/ip') (${ip_address} on ${ip_interface})" + echo -e "${RESET}\n" + fi +} + +# Putting it all together +function welcome() { + welcome_greeting + welcome_sysinfo + welcome_today +} + +# Determine if file is being run directly or sourced +([[ -n $ZSH_EVAL_CONTEXT && $ZSH_EVAL_CONTEXT =~ :file$ ]] \ + || [[ -n $KSH_VERSION && $(cd "$(dirname -- "$0")" \ + && printf '%s' "${PWD%/}/")$(basename -- "$0") != "${.sh.file}" ]] \ + || [[ -n $BASH_VERSION ]] && (return 0 2>/dev/null) +) && sourced=1 || sourced=0 + +# If script being called directly run immediately +if [ "$sourced" -eq 0 ]; then + welcome "$@" +fi + +# EOF +