#!/bin/bash e_err() { echo -e "\e[1;31m$1\e[0m" } e_info() { echo -e "\e[1;34m$1\e[0m" } e_succ() { echo -e "\e[1;32m$1\e[0m" } e_warn() { echo -e "\e[1;38;5;208m$1\e[0m" } function fake_alias() { : } function next_line() { : } # FILL LINE FUNCTIONS fill_line_with_string() { local string="$1" local length="$2" local fill="${3:- }" if [[ ${#fill} -eq 1 ]]; then beg_char=$fill end_char=$fill fi if [[ ${#fill} -eq 2 ]]; then beg_char="${fill:0:1}" end_char="${fill:1:1}" fi if [[ ${#fill} -gt 2 ]]; then beg_char="${fill:0:1}" end_char="${fill:0:1}" fi local padding=$(((length - ${#string} - 2) / 2)) local beg beg=$(printf "%${padding}s" | tr ' ' "$beg_char") local end end=$(printf "%${padding}s" | tr ' ' "$end_char") printf "%s%s%s" "$beg" " $string " "$end" echo "" } fill_line() { local length="$1" local fill="$2" printf "%*s\n" "$length" "" | tr ' ' "$fill" } # ALIAS DESCRIPTIONS line_length=59 get_alias_descriptions() { local file_path="$1" if [ -f "$file_path" ]; then echo "" echo "@ $file_path" local description="" while IFS= read -r line; do if [[ "$line" =~ ^### ]]; then fill_line_with_string "${line#'### '}" "$line_length" "-" elif [[ "$line" =~ ^# ]]; then description="${line#"# "}" elif [[ "$line" =~ ^(alias|fake_alias) ]]; then alias_name=$(printf "%10s" "$(echo "$line" | cut -d'=' -f1 | awk '{print $2}')") echo "$alias_name :: $description" elif [[ $line == "next_line" ]]; then printf "%*s%s\n" "14" "" "$description" else description="" fi done <"$file_path" fi } display_alias_descriptions() { echo "" fill_line_with_string "ALIASES DESCRIPTIONS" "$line_length" " " get_alias_descriptions "$ALIASES"/.aliases get_alias_descriptions "$ALIASES"/.git_aliases get_alias_descriptions "$ALIASES"/.docker_aliases get_alias_descriptions "$ALIASES"/.local_aliases get_alias_descriptions "$ALIASES"/.custom_aliases fill_line "$line_length" "-" } ea_help() { e_info "Usage: ea [c|l|h]" echo "Edit ~/.config/aliases/.aliases file when used without arguments" echo "Args: c :: edit ~/.config/aliases/.custom_aliases" echo " l :: edit ~/.config/aliases/.local_aliases" echo " f edit ~/.config/aliases/.functions.sh" echo " h :: print help" } edit_aliases() { if [ $# -eq 0 ]; then ${EDITOR:-nano} "$ALIASES"/.aliases elif [ $# -gt 1 ]; then e_err "Too many arguments!" ea_help else case "$1" in l) ${EDITOR:-nano} "$ALIASES"/.local_aliases ;; c) ${EDITOR:-nano} "$ALIASES"/.custom_aliases ;; f) ${EDITOR:-nano} "$ALIASES"/.functions.sh ;; h) ea_help ;; *) e_err "Invalid option!"; ea_help ;; esac fi } cd_to_file() { cwd=$(pwd) cd "${1:-.}" || return file=$(fzf --preview 'bat --color=always {}') if [ -z "$file" ]; then cd "$cwd" || return else cd "$(dirname "$file")" || return fi } mkdir_and_cd() { mkdir -p "$1" || e_err "Failed to create directory: $1" cd "$1" || e_err "Failed to navigate to directory: $1" }