##########################################################################
#
#               ███████╗███████╗██╗  ██╗██████╗  ██████╗
#               ╚══███╔╝██╔════╝██║  ██║██╔══██╗██╔════╝
#                 ███╔╝ ███████╗███████║██████╔╝██║     
#                ███╔╝  ╚════██║██╔══██║██╔══██╗██║     
#               ███████╗███████║██║  ██║██║  ██║╚██████╗
#               ╚══════╝╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝
#
##########################################################################                                        

# Zsh and Zinit configuration ---------------------------------------------------------------- {{{

# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:/usr/local/bin:$HOME/.local/bin/:$HOME/.cargo/bin/:$PATH

# Path to your Zinit installation.
export ZSH="${XDG_DATE_HOME:-${HOME}/.local/share}/zinit/zinit.git"

# Download and install Zinit if it's not already installed
if [ ! -d $ZSH ]; then
    git clone https://github.com/zdharma-continuum/zinit.git $ZSH
fi

source $ZSH/zinit.zsh

# Theme ---------------------------------------------------------------- {{{ 

# Set the theme to use with Oh My Posh
eval "$(oh-my-posh init zsh --config $HOME/.config/ohmyposh/prompt.json)"

# }}}

# General options ---------------------------------------------------------------- {{{ 

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
HIST_STAMPS="dd.mm.yyyy"

# }}}

# Plugins and snippets ---------------------------------------------------------------- {{{ 

zinit light zsh-users/zsh-syntax-highlighting
zinit light zsh-users/zsh-autosuggestions
zinit light zsh-users/zsh-completions
zinit ice from'gh-r' as'program'
zinit light sei40kr/fast-alias-tips-bin
zinit light sei40kr/zsh-fast-alias-tips

zinit snippet OMZP::git
zinit snippet OMZP::zoxide

# }}}

# History ---------------------------------------------------------------- {{{ 

HISTSIZE=5000               # The number of commands to remember in the command history
HISTFILE=~/.zsh_history     # The file to save the command history to
SAVEHIST=$HISTSIZE          # The number of commands to save in the history file
HISTDUP=erase               # Erase duplicates in the history file
setopt appendhistory        # Append history to the history file 
setopt sharehistory         # Share history between all sessions
setopt hist_ignore_space    # Ignore commands that start with a space
setopt hist_ignore_dups     # Ignore duplicate commands
setopt hist_ignore_all_dups # Ignore all duplicate commands
setopt hist_save_no_dups    # Do not save duplicate commands
setopt hist_find_no_dups    # Do not display duplicate commands

# Key bindings
bindkey "^p" history-search-backward    # Search backward in history
bindkey "^n" history-search-forward     # Search forward in history

# }}}

# Completion ---------------------------------------------------------------- {{{ 

# Zsh allows command completion.
# The completion is very modifiable, here are some options
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
zstyle ':completion:*:descriptions' format '%U%B%d%b%u'
zstyle ':completion:*:warnings' format '%BSorry, no results'
zstyle ':completion:*' menu select=2
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' list-colors '${(s.:.)LS_COLORS}'

# Load bash completion functions
autoload -Uz +X compinit && compinit
autoload -Uz +X bashcompinit && bashcompinit

 # -q is for quiet; actually run all the `compdef's saved before `compinit` call
# (`compinit' declares the `compdef' function, so it cannot be used until
# `compinit' is ran; Zinit solves this via intercepting the `compdef'-calls and
# storing them for later use with `zinit cdreplay')
zinit cdreplay -q

# }}}

# }}}

# User configuration ---------------------------------------------------------------- {{{

# Preferred editor for local and remote sessions
if [[ -n $SSH_CONNECTION ]]; then
   export EDITOR='nvim'
else
   export EDITOR='vim'
fi

# Set the ssh-agent if it is not already running
if ! pgrep -u "$USER" ssh-agent > /dev/null; then
    eval "$(ssh-agent -s)" > /dev/null
fi
export SSH_AUTH_SOCK=$(ssh-agent -s | grep -oP '(?<=SSH_AUTH_SOCK=)[^;]+')
# Add the keys to the agent
if [ -f ~/.ssh/id_ed25519 ] && ! ssh-add -l | grep -q id_ed25519; then
    ssh-add ~/.ssh/id_ed25519 > /dev/null 2>&1
fi
if [ -f ~/.ssh/hexasec ] && ! ssh-add -l | grep -q hexasec; then
    ssh-add ~/.ssh/hexasec > /dev/null 2>&1
fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Aliases ---------------------------------------------------------------- {{{

# Set personal aliases 
# For a full list of active aliases, run `alias`.
alias ls='lsd'
alias ll='ls -alF'
alias rm='rip'
alias cat='bat'
alias grep='rg'
alias find='fd -H'
alias sed='sd'
alias c='clear'
alias prolog='~/.config/scryer-prolog/target/release/scryer-prolog'     # À remplacer par prolog='scryer-prolog'
alias csc='sudo cytech-site-change'
alias quit='exit'
alias compresse='echo "tar -cv [nomDuFichieràCompresser] -f [nomArchive].tar"'
alias maj='~/./miseajour.sh'
alias vi='\vim'
alias vim='nvim'

# }}}

# Allow zoxide to work and to replace cd
eval "$(zoxide init --cmd cd zsh)"

# }}}
