# ~/.zshrc : servyass (aussi utilisé par root : /root/.zshrc le charge, d'où les chemins absolus)
# Style du portfolio : fond noir, accent lime #d3fd50, encre #f5f5f0, gris #8a8a85, ambre #ffcf4a, rouge #ff5f5f.
# Ancienne config : ~/backups/zsh-20261008/

export PATH="$HOME/.local/bin:$PATH"
export EDITOR=${EDITOR:-nano}
export LANG=${LANG:-fr_FR.UTF-8}

# ── Historique ────────────────────────────────────────────────────────
HISTFILE=~/.zsh_history
HISTSIZE=20000
SAVEHIST=20000
setopt histignorealldups histignorespace sharehistory extendedhistory
setopt autocd interactivecomments nobeep
bindkey -e

# ── Complétion : menu navigable, couleurs du thème ────────────────────
autoload -Uz compinit && compinit -C
zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|=*' 'l:|=* r:|=*'
zstyle ':completion:*' group-name ''
zstyle ':completion:*:descriptions' format $'%F{#d3fd50}── %d%f'
zstyle ':completion:*:warnings' format $'%F{#ff5f5f}── aucun résultat%f'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"

# ── Couleurs de ls, man/less ──────────────────────────────────────────
# sg/su/ow/tw/st : sans fond coloré (beaucoup de fichiers de ~ sont setgid à cause de FileBrowser)
export LS_COLORS='su=38;2;255;95;95:sg=38;2;245;245;240:ow=1;38;2;211;253;80:tw=1;38;2;211;253;80:st=1;38;2;211;253;80:ca=0:di=1;38;2;211;253;80:ln=38;2;124;200;255:ex=38;2;255;207;74:or=38;2;255;95;95:mi=38;2;255;95;95:*.tar=38;2;190;120;255:*.gz=38;2;190;120;255:*.zip=38;2;190;120;255:*.md=38;2;245;245;240:*.yml=38;2;160;160;155:*.json=38;2;160;160;155'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
export LESS='-R'
export LESS_TERMCAP_md=$'\e[1;38;2;211;253;80m' LESS_TERMCAP_me=$'\e[0m'
export LESS_TERMCAP_us=$'\e[4;38;2;245;245;240m' LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_so=$'\e[38;2;10;10;10;48;2;211;253;80m' LESS_TERMCAP_se=$'\e[0m'
export MANPAGER='less -R'

# ── Alias ─────────────────────────────────────────────────────────────
alias ls='ls --color=auto --group-directories-first'
alias ll='ls -lah --color=auto --group-directories-first'
alias grep='grep --color=auto'
alias ..='cd ..' ...='cd ../..'
alias ec="$EDITOR /home/yass/.zshrc"     # éditer ce fichier (même pour root)
alias sc="source $HOME/.zshrc"           # recharger
alias dps='docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'
alias motd='servyass_banner'             # réafficher l'accueil

# ── Prompt ────────────────────────────────────────────────────────────
#  SERVYASS  ~/chemin sur main●  · 12s
#  ❯                                          ✕ 1   14:02:05
autoload -Uz vcs_info add-zsh-hook
setopt promptsubst
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:git:*' check-for-changes true
zstyle ':vcs_info:git:*' unstagedstr '%F{#ffcf4a}●%f'
zstyle ':vcs_info:git:*' stagedstr '%F{#d3fd50}✚%f'
zstyle ':vcs_info:git:*' formats ' %F{#8a8a85}sur%f %F{#d3fd50}%b%f%u%c'
zstyle ':vcs_info:git:*' actionformats ' %F{#8a8a85}sur%f %F{#d3fd50}%b%f %F{#ff5f5f}(%a)%f%u%c'

typeset -g _yb_t0 _yb_took _yb_first=1
_yb_preexec() { _yb_t0=$EPOCHREALTIME; print -Pn "\e]0;servyass · %~ · ${1[1,40]}\a"; }
_yb_precmd() {
  vcs_info
  _yb_took=''
  if [[ -n $_yb_t0 ]]; then
    local s=$(( ${EPOCHREALTIME%.*} - ${_yb_t0%.*} ))
    if (( s >= 60 )); then _yb_took=" %F{#8a8a85}·%f %F{#ffcf4a}$(( s / 60 )) min $(( s % 60 )) s%f"
    elif (( s >= 3 )); then _yb_took=" %F{#8a8a85}·%f %F{#ffcf4a}${s} s%f"; fi
    _yb_t0=''
  fi
  (( _yb_first )) && _yb_first=0 || print   # une ligne d'air entre deux commandes
  print -Pn "\e]0;servyass · %~\a"
}
zmodload zsh/datetime
add-zsh-hook preexec _yb_preexec
add-zsh-hook precmd _yb_precmd

# pastille d'hôte : lime pour yass, rouge en root
_yb_host='%(!.%K{#ff5f5f}%F{#0a0a0a}%B ROOT · SERVYASS %b%f%k.%K{#d3fd50}%F{#0a0a0a}%B SERVYASS %b%f%k)'
PROMPT='${_yb_host} %B%F{#f5f5f0}%(5~|…/%3~|%~)%f%b${vcs_info_msg_0_}${_yb_took}
%(?.%F{#d3fd50}.%F{#ff5f5f})❯%f '
RPROMPT='%(?..%F{#ff5f5f}✕ %?%f  )%F{#5d5d58}%*%f'
PROMPT2='%F{#5d5d58}·%f '

# ── Plugins (paquets Debian + oh-my-zsh déjà présents) ─────────────────
_omz=/home/yass/.zplug/repos/ohmyzsh/ohmyzsh/plugins
[[ -r $_omz/sudo/sudo.plugin.zsh ]] && source $_omz/sudo/sudo.plugin.zsh   # Échap Échap = ajoute sudo
[[ -r $_omz/git/git.plugin.zsh ]] && source $_omz/git/git.plugin.zsh       # alias git (gst, gco, gp...)
unset _omz

if [[ -r /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh ]]; then
  source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
  ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=#5d5d58'
  ZSH_AUTOSUGGEST_STRATEGY=(history completion)
fi

# fzf : Ctrl+R historique, Ctrl+T fichiers, Alt+C dossiers
[[ -r /usr/share/doc/fzf/examples/key-bindings.zsh ]] && source /usr/share/doc/fzf/examples/key-bindings.zsh
[[ -r /usr/share/doc/fzf/examples/completion.zsh ]] && source /usr/share/doc/fzf/examples/completion.zsh
export FZF_DEFAULT_OPTS="--height=45% --layout=reverse --border=rounded --prompt='❯ ' --pointer='▌' --marker='✓' \
  --color=bg+:#141414,fg:#c8c8c0,fg+:#f5f5f0,hl:#d3fd50,hl+:#d3fd50,pointer:#d3fd50,prompt:#d3fd50,marker:#d3fd50,info:#5d5d58,border:#2a2a28,spinner:#d3fd50,header:#8a8a85"

# coloration syntaxique (toujours en dernier)
if [[ -r /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]]; then
  source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  typeset -A ZSH_HIGHLIGHT_STYLES
  ZSH_HIGHLIGHT_STYLES[command]='fg=#d3fd50'
  ZSH_HIGHLIGHT_STYLES[alias]='fg=#d3fd50'
  ZSH_HIGHLIGHT_STYLES[builtin]='fg=#d3fd50'
  ZSH_HIGHLIGHT_STYLES[function]='fg=#d3fd50'
  ZSH_HIGHLIGHT_STYLES[precommand]='fg=#d3fd50,underline'
  ZSH_HIGHLIGHT_STYLES[unknown-token]='fg=#ff5f5f'
  ZSH_HIGHLIGHT_STYLES[reserved-word]='fg=#be78ff'
  ZSH_HIGHLIGHT_STYLES[path]='fg=#f5f5f0,underline'
  ZSH_HIGHLIGHT_STYLES[single-quoted-argument]='fg=#ffcf4a'
  ZSH_HIGHLIGHT_STYLES[double-quoted-argument]='fg=#ffcf4a'
  ZSH_HIGHLIGHT_STYLES[dollar-quoted-argument]='fg=#ffcf4a'
  ZSH_HIGHLIGHT_STYLES[single-hyphen-option]='fg=#8a8a85'
  ZSH_HIGHLIGHT_STYLES[double-hyphen-option]='fg=#8a8a85'
  ZSH_HIGHLIGHT_STYLES[commandseparator]='fg=#be78ff'
  ZSH_HIGHLIGHT_STYLES[redirection]='fg=#be78ff'
  ZSH_HIGHLIGHT_STYLES[comment]='fg=#5d5d58'
  ZSH_HIGHLIGHT_STYLES[globbing]='fg=#7cc8ff'
fi

# historique filtré par ce qui est déjà tapé : flèches haut / bas
if [[ -r /home/yass/.zplug/repos/zsh-users/zsh-history-substring-search/zsh-history-substring-search.zsh ]]; then
  source /home/yass/.zplug/repos/zsh-users/zsh-history-substring-search/zsh-history-substring-search.zsh
  HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='fg=#0a0a0a,bg=#d3fd50'
  HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='fg=#0a0a0a,bg=#ff5f5f'
  bindkey '^[[A' history-substring-search-up;   bindkey '^[OA' history-substring-search-up
  bindkey '^[[B' history-substring-search-down; bindkey '^[OB' history-substring-search-down
fi

# ── Accueil : à l'ouverture d'une session (SSH ou premier shell), pas dans les sous-shells ──
source /home/yass/.config/servyass/banner.zsh
if [[ -o interactive && $SHLVL -le 1 && -z $VSCODE_INJECTION && -z $TMUX ]]; then
  servyass_banner
fi
