## Functions

# Show a directory listing when using 'cd'
function cd() {
    new_directory="$*";
    if [ $# -eq 0 ]; then
        new_directory=${HOME};
    fi;
    builtin cd "${new_directory}" && /bin/ls -lhF --time-style=long-iso --color=auto --ignore=lost+found
}

# Make a directory, then go there
function md() {
    test -n "$1" || return
    mkdir -p "$1" && cd "$1"
}

# "path" shows current path, one element per line.
# If an argument is supplied, grep for it.
function path() {
    test -n "$1" && {
        echo "$PATH" | perl -p -e "s/:/\n/g;" | grep -i "$1"
    } || {
        echo "$PATH" | perl -p -e "s/:/\n/g;"
    }
}

# Show file listing when using 'popd'
function popd() {
    builtin popd "$@" && /bin/ls -lhF --time-style=long-iso --color=auto --ignore=lost+found
}


# Show file listing when using 'pushd'
function pushd() {
    builtin pushd "$@" && /bin/ls -lhF --time-style=long-iso --color=auto --ignore=lost+found
}

function extract() {
  if [ -f "$1" ] ; then
    case "$1" in
      *.tar.bz2)   tar xjvf "$1"    ;;
      *.tar.gz)    tar xzvf "$1"    ;;
      *.tar.xz)    tar xvf "$1"     ;;
      *.bz2)       bzip2 -d "$1"    ;;
      *.rar)       unrar2dir "$1"   ;;
      *.gz)        gunzip "$1"      ;;
      *.tar)       tar xf "$1"      ;;
      *.tbz2)      tar xjf "$1"     ;;
      *.tgz)       tar xzf "$1"     ;;
      *.zip)       unzip2dir "$1"   ;;
      *.Z)         uncompress "$1"  ;;
      *.7z)        7z x "$1"        ;;
      *.ace)       unace x "$1"     ;;
      *)           echo "'$1' cannot be extracted via extract()"   ;;
    esac
  else
    echo "'$1' is not a valid file"
  fi
}

function tree() {
    if command -v eza > /dev/null 2>&1; then
        eza -lhF --group --tree --time-style=long-iso --color=auto --ignore-glob='lost+found'
    else
        command tree
    fi
}
