# Created by newuser for 5.9
# 启动时间统计
zmodload zsh/datetime
start_time=$EPOCHREALTIME

# 启用 Powerlevel10k 即时提示
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# 基础路径配置
export PATH="$HOME/.local/bin:$PATH"

### Added by Zinit's installer
if [[ ! -f $HOME/.local/share/zinit/zinit.git/zinit.zsh ]]; then
    print -P "%F{33} %F{220}Installing %F{33}ZDHARMA-CONTINUUM%F{220} Initiative Plugin Manager (%F{33}zdharma-continuum/zinit%F{220})…%f"
    command mkdir -p "$HOME/.local/share/zinit" && command chmod g-rwX "$HOME/.local/share/zinit"
    command git clone <https://github.com/zdharma-continuum/zinit> "$HOME/.local/share/zinit/zinit.git" && \\
        print -P "%F{33} %F{34}Installation successful.%f%b" || \\
        print -P "%F{160} The clone has failed.%f%b"
fi

source "$HOME/.local/share/zinit/zinit.git/zinit.zsh"
autoload -Uz _zinit
(( ${+_comps} )) && _comps[zinit]=_zinit

# 加载主题
zinit ice depth=1; zinit light romkatv/powerlevel10k

# 加载补全
autoload -Uz compinit
compinit

# 加载插件
zinit light zdharma-continuum/fast-syntax-highlighting
zinit light zsh-users/zsh-autosuggestions
zinit light zsh-users/zsh-completions
zinit light zsh-users/zsh-history-substring-search
zinit light hlissner/zsh-autopair
zinit light MichaelAquilina/zsh-you-should-use
zinit light agkozak/zsh-z

# 绑定历史搜索键(在加载插件后绑定)
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down

# fzf 配置
[[ -f /usr/share/fzf/key-bindings.zsh ]] && source /usr/share/fzf/key-bindings.zsh
[[ -f /usr/share/fzf/completion.zsh ]] && source /usr/share/fzf/completion.zsh

# fzf 配置
export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border'

# 如果安装了 bat,添加预览功能
if command -v bat >/dev/null; then
    export FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS --preview 'bat --style=numbers --color=always --line-range :500 {}'"
fi

# 基本选项设置
setopt AUTO_CD
setopt EXTENDED_HISTORY
setopt SHARE_HISTORY
setopt HIST_IGNORE_DUPS
setopt HIST_FIND_NO_DUPS
setopt HIST_REDUCE_BLANKS

# 历史记录配置
HISTFILE=~/.config/zsh/cache/.zsh_history
HISTSIZE=1000000
SAVEHIST=1000000

# 按键绑定
bindkey '^R' fzf-history-widget
bindkey '^T' fzf-file-widget
bindkey '^[[1;5C' forward-word
bindkey '^[[1;5D' backward-word
bindkey '^A' beginning-of-line
bindkey '^E' end-of-line
bindkey '^K' kill-line
bindkey '^U' kill-whole-line
bindkey '^W' backward-kill-word
bindkey '^H' backward-delete-char
bindkey '^[[3~' delete-char
bindkey '^[[H' beginning-of-line
bindkey '^[[F' end-of-line
bindkey '^P' up-line-or-history
bindkey '^N' down-line-or-history

# eza 别名设置
if command -v eza >/dev/null; then
    alias ls='eza --icons --group-directories-first'
    alias ll='eza --icons --group-directories-first -l'
    alias la='eza --icons --group-directories-first -la'
    alias lt='eza --icons --group-directories-first -T'
    alias lg='eza --icons --group-directories-first -l --git'
else
    alias ls='ls --color=auto'
    alias ll='ls -lah'
    alias la='ls -A'
    alias l='ls -CF'
fi

# 别名设置
alias ..='cd ..'
alias ...='cd ../..'
alias c='clear'
alias h='history'
alias update='sudo pacman -Syu'
alias yay-update='yay -Syu'
alias ports='ss -tulanp'
alias grep='grep --color=auto'
alias df='df -h'
alias du='du -h'
alias free='free -h'
alias top='htop'
alias vim='nvim'
alias dirsize='du -sh'
alias ds='du -sh * | sort -h'

# 目录大小分析函数
function dsize() {
    if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        echo "用法: dsize [目录路径]"
        echo "显示指定目录或当前目录的详细大小信息"
        echo "选项:"
        echo "  -h, --help    显示此帮助信息"
        return
    fi

    local target_dir="${1:-.}"
    echo "正在分析目录: $target_dir"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "总大小:"
    du -sh "$target_dir"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "详细大小(前10个最大的文件/目录):"
    du -sh "$target_dir"/* 2>/dev/null | sort -rh | head -n 10
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
}

# 补全系统设置
zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'

# 系统信息显示函数
function show_system_info() {
    if (( ${+start_time} )); then
        end_time=$EPOCHREALTIME
        load_time=$(( (end_time - start_time) * 1000 ))
        print -P "%F{yellow}ZSH 加载时间: ${load_time}ms%f"
    fi

    print -P "%F{blue}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━%f"

    if (( $+commands[neofetch] )); then
        neofetch
    fi

    print -P "%F{blue}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━%f"

    print -P "%F{green}快捷键提示:%f %F{yellow}Ctrl+R%f 历史 %F{yellow}Ctrl+T%f 文件 %F{yellow}Alt+C%f 目录"
    print -P "使用 %F{yellow}z 关键词%f 可以快速跳转到常用目录"

    print -P "%F{blue}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━%f"
}

show_system_info

# 加载 powerlevel10k 配置
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

typeset -g POWERLEVEL9K_INSTANT_PROMPT=quiet

怎么使用?

  1. 安装基础工具

    sudo pacman -S zsh git curl wget neofetch fzf fd bat htop neovim
    
  2. 安装官方字体,并在 Konsole 设置里应用 MesloLGS

    yay -S ttf-meslo-nerd-font-powerlevel10k
    
  3. 创建配置目录,和历史命令文件

    mkdir -p ~/.config/zsh/cache
    touch ~/.config/zsh/cache/.zsh_history
    chmod 600 ~/.config/zsh/cache/.zsh_history
    
  4. 安装zinit

    bash -c "$(curl --fail --show-error --silent --location <https://raw.githubusercontent.com/zdharma-continuum/zinit/HEAD/scripts/install.sh>)"
    
  5. 将上方的配置复制到 .zshrc

    vim ~/.zshrc