## Set values
# Hide welcome message
set fish_greeting
set VIRTUAL_ENV_DISABLE_PROMPT "1"
set -x MANROFFOPT "-c"
set -x MANPAGER "sh -c 'col -bx | bat -l man -p'"

## Environment setup
# Env vars
export EDITOR=nvim
export VISUAL=nvim

if test -f ~/.fish_profile
  source ~/.fish_profile
end

# Add ~/.local/bin to PATH
if test -d ~/.local/bin
    if not contains -- ~/.local/bin $PATH
        set -p PATH ~/.local/bin
    end
end

# Add ~/go/bin to PATH
if test -d ~/go/bin
    if not contains -- ~/go/bin $PATH
        set -p PATH ~/go/bin
    end
end

## Plugin configuration
# Set settings for https://github.com/franciscolourenco/done
set -U __done_min_cmd_duration 10000
set -U __done_notification_urgency_level low

# Starship prompt
if type -q starship && status --is-interactive
    starship init fish | source
end

# Zoxide
if type -q zoxide && status --is-interactive
    zoxide init fish | source
end

## Functions
# Fish command history
function history
    builtin history --reverse --show-time='%F %T '
end

function ba --argument filename
    cp $filename $filename.bak
end

function cp_bar
    cp $argv[1] $argv[2] &
    set cpid $last_pid

    progress -mp $cpid
    kill $cpid &> /dev/null
end

function unzipc -a input_file
    if test -z "$input_file"
        echo "Usage: unzip <filename.zip>"
        return 1
    end

    if test ! -f $input_file
        echo "Error: File not found - $input_file"
        return 1
    end

    set folder_name (basename -s .zip $input_file)

    command unzip "$input_file" -d "$folder_name" && echo "File '$input_file' successfully extracted to '$folder_name/'" 
end

function untarc -a input_file
    if test -z "$input_file"
        echo "Usage: untar <filename.zip>"
        return 1
    end

    if test ! -f $input_file
        echo "Error: File not found - $input_file"
        return 1
    end

    set folder_name (string split -m 1 "." $input_file)[1]
    mkdir $folder_name

    command tar -xvf "$input_file" -C "$folder_name" && echo "File '$input_file' successfully extracted to '$folder_name/'"
end

# Maven
function mvn-init -a groupId artifactId -d "Create a simple maven project"
    if test -z "$groupId" -o -z "$artifactId"
        echo -e "Usage: mvn-init <groupId> <artifactId>\ngroupId or artifactId missing!"
    else
        mvn archetype:generate -DgroupId=$groupId -DartifactId=$artifactId -DinteractiveMode=false
    end
end

function mvn-run -a mainClass -d "Run your current maven project"
    if test -z "$mainClass"
        echo -e "Usgae: mvn-run <mainClass> [arguments]\nmainClass missing!"
    else
        set args ""
        for arg in $argv[2..-1]
            set args "$args $arg"
        end

        if test -z $args
            mvn clean compile exec:java -Dexec.mainClass="$mainClass"
        else
            mvn clean compile exec:java -Dexec.mainClass="$mainClass" -Dexec.args="$args"
        end
    end
end

# Yazi wrapper
function y
	set tmp (mktemp -t "yazi-cwd.XXXXXX")
	yazi $argv --cwd-file="$tmp"
	if read -z cwd < "$tmp"; and [ -n "$cwd" ]; and [ "$cwd" != "$PWD" ]
		builtin cd -- "$cwd"
	end
	rm -f -- "$tmp"
end

# Convert webm to opus
function w2o
    for f in *.webm
        ffmpeg -i "$f" -map 0:a:0 -c:a copy (path change-extension opus "$f")
        and rm "$f"
    end
end

## Aliases
# Replace ls with eza
if type -q eza
    alias ls='eza -al --color=always --group-directories-first --icons' # preferred listing
    alias la='eza -a --color=always --group-directories-first --icons'  # all files and dirs
    alias ll='eza -l --color=always --group-directories-first --icons'  # long format
    alias lt='eza -aT --color=always --group-directories-first --icons' # tree listing
    alias l.='eza -ald --color=always --group-directories-first --icons .*' # show only dotfiles
end

# Replace cat with bat
if type -q bat
    alias cat='bat --style header --style snip --style changes --style header'
end

# Replace yay with paru
if type -q paru
    alias yay='paru'
end

# Replace cd with zoxide
if type -q zoxide
    alias cd='z'
end
 
# Common use
alias :q=exit
alias ipn='ip'
alias nano=nvim
alias cls='clear'
alias please='sudo'
alias ip='ip -color'
alias wip='curl https://ipinfo.io/ip'
alias mkdirs='mkdir --parents'
alias grubup="sudo update-grub"
alias fixpacman="sudo rm /var/lib/pacman/db.lck"
alias tarnow='tar -acf '
alias tarlist='tar -tvf '
alias wget='wget -c '
alias rmpkg='sudo pacman -Rcns '
alias purgepkg="sudo pacman -Rdd "
alias psmem='ps auxf | sort -nr -k 4'
alias psmem10='ps auxf | sort -nr -k 4 | head -10'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias grep='grep --color=auto'
alias grepf='grep -F --color=auto'
alias grepe='grep -E --color=auto'
alias grepi='grep -i --color=auto'
alias hw='hwinfo --short'                           # Hardware Info
alias bigpkg="expac -H M '%m\t%n' | sort -h | nl"   # Sort installed packages according to size in MB
alias gitpkg='pacman -Q | grep -i "\-git" | wc -l'  # List amount of -git packages
alias cleanup='sudo pacman -Rns (pacman -Qtdq)'     # Cleanup orphaned packages
alias jctl="journalctl -p 3 -xb"                    # Get the error messages from journalctl
alias ripp="expac --timefmt='%Y-%m-%d %T' '%l\t%n %v' | sort | tail -200 | nl" # Recent installed packages
alias ch="cliphist list | fzf | cliphist decode | wl-copy"
alias dirsize="du -h --max-depth=1"


# Get fastest mirrors
alias mirror="sudo reflector -f 30 -l 30 --number 10 --verbose --save /etc/pacman.d/mirrorlist"
alias mirrord="sudo reflector --latest 50 --number 20 --sort delay --save /etc/pacman.d/mirrorlist"
alias mirrors="sudo reflector --latest 50 --number 20 --sort score --save /etc/pacman.d/mirrorlist"
alias mirrora="sudo reflector --latest 50 --number 20 --sort age --save /etc/pacman.d/mirrorlist"

## Run fastfetch
if status --is-interactive 
    if type -q fastfetch
        fastfetch -l arch
    end
end
