snippets/functions.sh

39 lines
1.3 KiB
Bash
Raw Normal View History

2023-12-21 11:33:34 +00:00
bold=$(tput bold)
reset=$(tput sgr0)
purple=$(tput setaf 171)
green=$(tput setaf 76)
tan=$(tput setaf 3)
header() {
printf "\n${bold}${purple}========== %s ==========${reset}\n" "$@"
}
arrow() {
printf " ➜ %s\n" "$@"
}
success() {
printf "${green} ✔ %s${reset}\n" "$@"
}
warning() {
printf "${tan} ➜ %s${reset}\n" "$@"
}
function top_commands_full() {
local filter="$1"
local max_results=${2:-'50'}
history | \cat | awk '{$1=$1};1' | sed 's/^[0-9\* TAB]*//g' | awk '{CMD[$0]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "%\t" a; }' | grep "$filter" | sort -nr | nl | head "-n$max_results"
}
function suggest_aliases() {
local search_input_size=${1:-'50'}
header "alias recommendations"
while read -r line; do
local matching_aliases=$(alias | grep -i "$line")
if [ ! -z "$matching_aliases" ]; then
success "there is an alias for $line :"
while read -r alias_line; do
arrow "$alias_line"
done < <(echo "$matching_aliases")
echo
else
warning "no alias for $line"
fi
done < <(top_commands_full "" "$search_input_size" | awk '{ $1=""; $2=""; $3=""; print}' | awk 'NF' | awk '{$1=$1};1' | awk -v COUNT=1 'NF>COUNT' | head "-$search_input_size")
}