# Only run in interactive sessions
if status is-interactive
    # Prompt: Starship
    if type -q starship
        starship init fish | source
    else
        echo "Starship is not installed. Install it from https://starship.rs/"
    end

    # Flashing cursor
    printf '\e[5 q'

    # Bind Ctrl+U to clear the line backward
    bind \cU backward-kill-line
end

# PATH setup
set -gx PYENV_ROOT "$HOME/.pyenv"
set -gx PATH $HOME/bin $HOME/.local/bin /usr/local/bin $HOME/go/bin $HOME/.cargo/bin $PYENV_ROOT/bin $HOME/.local/share/gem/ruby/3.4.0/bin $PATH

# Pyenv init
if type -q pyenv
    pyenv init - | source
end

# Enable auto-correction
set -g fish_autocorrect true

# Aliases
alias nl 'nl -ba'
alias cat bat
alias bat 'bat --theme=gruvbox-dark'
alias find fd
alias fd 'fd -H'
#alias grep rg
alias pdf evince
alias ls 'eza -lah --grid -s modified --smart-group --group-directories-first'
alias l 'eza -ah --grid -s modified --smart-group --group-directories-first'
alias cp 'cp -riv'
alias mv 'mv -iv'
alias mkdir 'mkdir -pv'
alias less 'less -FSRXc'
# CAUSES PROBLEMS WITH git.fish autocomplection      alias path 'echo $PATH | tr ":" "\n"'
alias myip 'curl ipinfo.io'
alias moan_myip='sh -c '\''X=$(echo "aHR0cHM6Ly93d3cubW9hbm15aXAuY29tLw==" | base64 -d); curl -s "$X" | grep -o "output/[0-9.]*.mp3" | sed "s|^|$X|" | xargs -n1 -I{} sh -c "wget -qO- \"{}\" | (mpg123 - || ffplay -nodisp -autoexit -)"'\''' # sh -c 'X=$(echo "aHR0cHM6Ly93d3cubW9hbm15aXAuY29tLw==" | base64 -d); curl -s "$X" | grep -o "output/[0-9.]*.mp3" | sed "s|^|$X|" | xargs -n1 -I{} sh -c "wget -qO- \"{}\" | (mpg123 - || ffplay -nodisp -autoexit -)"'
alias ss 'ss -tulpn'
alias pscpu 'ps -eo pid,ppid,cmd,%mem,%cpu,etime --sort=-%cpu | head -n 21'
alias psmem 'ps -eo pid,ppid,cmd,%mem,%cpu,etime --sort=-%mem | head -n 21'
alias c clear
alias weer weather
alias v nvim
alias rm 'rm -r'
alias httpx httpx-toolkit
alias copy wl-copy
alias android_files aft-mtp-cli
alias open_raid_drives 'sudo cryptsetup open /dev/md0 md0_crypt; and sudo mount /dev/mapper/md0_crypt /mnt/raid_storage'

# Weather function
function weather
    if test "$argv[1]" = week
        wthrr -f w $argv[2]
    else if test -z "$argv[1]"
        wthrr -f d
        wthrr -f t
    else
        wthrr -f d $argv[1]
        wthrr -f t $argv[1]
    end
end

function extract
    if test -f "$argv[1]"
        set file "$argv[1]"
        set filename (basename "$file")
        set dirname (string replace -r '\.tar\.bz2$|\.tar\.gz$|\.tbz2$|\.tgz$|\.tar$|\.bz2$|\.gz$|\.zip$|\.Z$|\.7z$|\.xz$|\.rar$' '' "$filename")

        mkdir -p "$dirname"

        switch $file
            case "*.tar.bz2"
                tar xjf "$file" -C "$dirname"
            case "*.tar.gz"
                tar xzf "$file" -C "$dirname"
            case "*.tbz2"
                tar xjf "$file" -C "$dirname"
            case "*.tgz"
                tar xzf "$file" -C "$dirname"
            case "*.tar"
                tar xf "$file" -C "$dirname"
            case "*.bz2"
                bunzip2 -c "$file" >"$dirname/$dirname"
            case "*.gz"
                gunzip -c "$file" >"$dirname/$dirname"
            case "*.zip"
                unzip "$file" -d "$dirname"
            case "*.Z"
                uncompress -c "$file" >"$dirname/$dirname"
            case "*.7z"
                7z x "$file" -o"$dirname"
            case "*.xz"
                xz -dc "$file" >"$dirname/$dirname"
            case "*.rar"
                unrar x "$file" "$dirname"
            case "*"
                echo "'$file' cannot be extracted via extract()"
        end
    else
        echo "'$argv[1]' is not a valid file"
    end
end

# Arch Linux mirror ranking function
function mirrors
    curl -s "https://archlinux.org/mirrorlist/?country=BE&country=DK&country=DE&country=IS&country=NL&protocol=https&ip_version=4&ip_version=6" |
        sed -e 's/^#Server/Server/' -e '/^#/d' |
        rankmirrors -n 10 - | sudo tee /etc/pacman.d/mirrorlist >/dev/null
end

#function !!
#    eval (history --max=1)
#end

function __expand_bang_bang --on-event fish_preexec
    # Get the current command line
    set cmd (commandline)

    # Get the last command from history
    set last (history | head -n1)

    # Replace !! with the last command
    set new_cmd (string replace -r '!!' $last -- $cmd)

    # Apply expansion if it changed
    if test "$cmd" != "$new_cmd"
        commandline -r $new_cmd
    end
end
