1
0

feat: migrate from Oh-My-Zsh to ZInit

This commit is contained in:
2024-08-14 13:49:10 +02:00
parent 4a8dcfba3d
commit adc26f9860
7 changed files with 212 additions and 68 deletions

48
.zshrc
View File

@@ -2,39 +2,47 @@
# export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH # export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH
# Path to your Oh My Zsh installation. # Path to your Oh My Zsh installation.
export ZSH="$HOME/.oh-my-zsh" # export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="robbyrussell" # ZSH_THEME="robbyrussell"
zstyle ':omz:update' mode reminder # just remind me to update when it's time # zstyle ':omz:update' mode reminder # just remind me to update when it's time
plugins=( # plugins=(
git # git
zsh-syntax-highlighting # zsh-syntax-highlighting
# zsh-autosuggestions # # zsh-autosuggestions
zsh-autocomplete # zsh-autocomplete
colored-man-pages # colored-man-pages
sudo # sudo
) # )
zstyle ':autocomplete:*' delay 0.1 # seconds (float)
source $ZSH/oh-my-zsh.sh # source $ZSH/oh-my-zsh.sh
# ZInit
ZINIT_HOME="${XDG_DATA_HOME:-${HOME}/.local/share}/zinit/zinit.git"
[ ! -d $ZINIT_HOME ] && mkdir -p "$(dirname $ZINIT_HOME)"
[ ! -d $ZINIT_HOME/.git ] && git clone https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME"
source "${ZINIT_HOME}/zinit.zsh"
# fzf # fzf
source <(fzf --zsh) source <(fzf --zsh)
zinit snippet OMZ::lib/git.zsh
[ -f ~/zsh/aliases.sh ] && source ~/zsh/aliases.sh [ -f ~/zsh/aliases.sh ] && source ~/zsh/aliases.sh
[ -f ~/zsh/functions.sh ] && source ~/zsh/functions.sh [ -f ~/zsh/functions.sh ] && source ~/zsh/functions.sh
[ -f ~/zsh/prompt.sh ] && source ~/zsh/prompt.sh
[ -f ~/zsh/completion.sh ] && source ~/zsh/completion.sh
# Custom theme
PROMPT=""
# Path # Path
PROMPT+="%{$fg[cyan]%}%c%{$reset_color%} " PATH+=":/home/anton/Rider/bin"
# Arrow zinit ice wait'!0'
PROMPT+="%(?:%{$fg_bold[green]%}%1{➜%} :%{$fg[red]%}%? %{$fg_bold[red]%}%1{➜%} )"
# Git [ -f ~/zsh/plugins.sh ] && source ~/zsh/plugins.sh
PROMPT+='$(git_prompt_info)'
# source ${ZINIT_HOME}/../plugins/marlonrichert---zsh-autocomplete/zsh-autocomplete.plugin.zsh
# zstyle ':autocomplete:*' delay 0.1 # seconds (float)

View File

@@ -9,9 +9,9 @@ alias apps='cd ~/.local/share/applications/'
alias louis='ssh anton@192.168.178.48' alias louis='ssh anton@192.168.178.48'
# LS # LS
alias ls='lsd' alias ls='ls --color=always -F'
alias la='lsd -a' alias la='ls -a'
alias ll='lsd -l' alias ll='ls -l'
# WG # WG
alias wgon='wg-quick up ~/Downloads/wg0.conf' alias wgon='wg-quick up ~/Downloads/wg0.conf'

6
zsh/completion.sh Normal file
View File

@@ -0,0 +1,6 @@
setopt interactivecomments # VERY IMPORTANT LINE! Completions will be very bad without it
zinit ice as"completion"
zinit light zsh-users/zsh-completions
zinit light marlonrichert/zsh-autocomplete

View File

@@ -31,48 +31,3 @@ split() {
(kitty . &) (kitty . &)
} }
# === FUNCTIONS FOR COMMAND TIMING ===
# Function to capture the start time of the command
preexec() {
timer_start=$EPOCHSECONDS
}
# Function to capture the end time of the command and calculate the elapsed time
precmd() {
local timer_end=$EPOCHSECONDS
local elapsed=$((timer_end - timer_start))
# Reset RPROMPT
RPROMPT=""
if (( elapsed > 36000 )); then
return
fi
if (( elapsed > 0 )); then
local color time_str
if (( elapsed >= 60 )); then
local minutes=$((elapsed / 60))
local seconds=$((elapsed % 60))
time_str="${minutes}m ${seconds}s"
color="%{$fg_bold[red]%}"
elif (( elapsed >= 10 )); then
time_str="${elapsed}s"
color="%{$fg_bold[blue]%}"
elif (( elapsed >= 5 )); then
time_str="${elapsed}s"
color="%{$fg_bold[yellow]%}"
elif (( elapsed >= 1 )); then
time_str="${elapsed}s"
color="%{$fg_bold[green]%}"
fi
# Set RPROMPT if elapsed time is greater than or equal to 1 second
if [[ -n $time_str ]]; then
RPROMPT="${color}${time_str}%{$reset_color%}"
fi
fi
timer_start=0
}

27
zsh/git.sh Normal file
View File

@@ -0,0 +1,27 @@
function git_prompt_info() {
# If we are on a folder not tracked by git, get out.
# Otherwise, check for hide-info at global and local repository level
if ! __git_prompt_git rev-parse --git-dir &> /dev/null \
|| [[ "$(__git_prompt_git config --get oh-my-zsh.hide-info 2>/dev/null)" == 1 ]]; then
return 0
fi
# Get either:
# - the current branch name
# - the tag name if we are on a tag
# - the short SHA of the current commit
local ref
ref=$(__git_prompt_git symbolic-ref --short HEAD 2> /dev/null) \
|| ref=$(__git_prompt_git describe --tags --exact-match HEAD 2> /dev/null) \
|| ref=$(__git_prompt_git rev-parse --short HEAD 2> /dev/null) \
|| return 0
# Use global ZSH_THEME_GIT_SHOW_UPSTREAM=1 for including upstream remote info
local upstream
if (( ${+ZSH_THEME_GIT_SHOW_UPSTREAM} )); then
upstream=$(__git_prompt_git rev-parse --abbrev-ref --symbolic-full-name "@{upstream}" 2>/dev/null) \
&& upstream=" -> ${upstream}"
fi
echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${ref:gs/%/%%}${upstream:gs/%/%%}$(parse_git_dirty)${ZSH_THEME_GIT_PROMPT_SUFFIX}"
}

17
zsh/plugins.sh Normal file
View File

@@ -0,0 +1,17 @@
# Plugins
zinit ice blockf
zinit light zsh-users/zsh-syntax-highlighting
# zinit light zdharma-continuum/fast-syntax-highlighting
# completion
# [ -f ~/zsh/completion.sh ] && source ~/zsh/completion.sh
# oh-my-zsh plugins
# zinit snippet OMZ::/plugins/colored-man-pages/colored-man-pages.plugin.zsh
# zinit snippet OMZ::/plugins/sudo/sudo.plugin.zsh
zinit snippet OMZP::colored-man-pages
zinit snippet OMZP::sudo
zinit snippet OMZP::git
zinit snippet OMZ::lib/git.zsh

131
zsh/prompt.sh Normal file
View File

@@ -0,0 +1,131 @@
# Custom prompt
update_prompt() {
ret_code=$?
PROMPT=""
# Git
if [[ $(git_repo_name) ]]; then
# PROMPT+="%{$fg[yellow]%}$(git remote show | head -n 1)"
# PROMPT+="%{$fg_bold[magenta] $(git_repo_name)"
# PROMPT+="%{$fg_bold[blue]($(git_current_branch))%{$fg[white]%}:"
PROMPT+="%{$fg[yellow]%}$(git remote show | head -n 1) %{$fg_bold[magenta]%}$(git_repo_name)%{$fg_bold[blue]%}($(git_current_branch))%{$fg[white]%}:"
fi
# Path
PROMPT+="%{$fg[cyan]%}%c%{$reset_color%} "
# Arrow
if [[ started -eq 0 ]] || [[ $ret_code -eq 0 ]]; then
PROMPT+="%{$fg_bold[green]%}%1{➜%} "
else
PROMPT+="%{$fg[red]%}%? %{$fg_bold[red]%}%1{➜%} "
fi
# PROMPT+="%(?:%{$fg_bold[green]%}%1{➜%} :%{$fg[red]%}(%?%) %{$fg_bold[red]%}%1{➜%} )"
# PROMPT+="%(?:%{${started:+$fg_bold[green]}${started:=$fg[green]}%}%1{➜%} :%{${started:+$fg_bold[green]%)"
return $?
}
update_prompt
# Define a map of error codes to messages
declare -A ERROR_MESSAGES=(
[64]="USAGE"
[65]="FORMAT"
[68]="HOSTNAME"
[69]="UNAVAIL"
[70]="SOFTWARE"
[71]="OSERR"
[72]="OSFILE"
[73]="CREATEFAIL"
[74]="IOERR"
[75]="TEMP"
[76]="PROTOCOL"
[77]="DENIED"
[78]="CONFERR" # Configuration error
[126]="EXECFAIL"
[127]="COMMAND" # Command not found
[130]="SIGINT"
[137]="SIGKILL"
# Add more error codes and messages as needed
)
# === FUNCTIONS FOR COMMAND TIMING ===
# Function to capture the start time of the command in milliseconds
preexec() {
started=1
timer_start=$(( $(date +%s%3N) )) # Captures current time in milliseconds
}
# Function to capture the end time of the command and calculate the elapsed time
precmd() {
update_prompt
if [[ started -eq 0 ]]; then
RPROMPT=""
return
fi
started=0
local timer_end=$(( $(date +%s%3N) )) # Captures current time in milliseconds
local elapsed=$((timer_end - timer_start))
# Reset RPROMPT
RPROMPT=""
# Handle error codes: Prepend the error message if the command failed
if [[ ret_code -ne 0 ]]; then
local error_msg=${ERROR_MESSAGES[$ret_code]}
if [[ -n $error_msg ]]; then
RPROMPT="%{$fg_bold[red]%}(${error_msg})%{$reset_color%}$RPROMPT"
fi
fi
# Do not display the time if it is above 100 hours (360000000 ms)
if (( elapsed > 360000000 )); then
return
fi
if (( elapsed > 0 )); then
local color time_str
local seconds=$((elapsed / 1000))
local milliseconds=$((elapsed % 1000))
if (( seconds >= 60 )); then
local minutes=$((seconds / 60))
seconds=$((seconds % 60))
if (( minutes >= 60 )); then
local hours=$((minutes / 60))
minutes=$((minutes % 60))
time_str="${hours}h ${minutes}m"
if (( seconds > 0 )); then
time_str="${time_str} ${seconds}s"
fi
else
time_str="${minutes}m"
if (( seconds > 0 )); then
time_str="${time_str} ${seconds}s"
fi
fi
color="%{$fg[red]%}"
elif (( seconds >= 10 )); then
time_str="${seconds}s"
color="%{$fg[orange]%}"
elif (( seconds >= 5 )); then
time_str="${seconds}s"
color="%{$fg[yellow]%}"
elif (( seconds >= 1 )); then
time_str="${seconds}s ${milliseconds}ms"
color="%{$fg_bold[green]%}"
else
time_str="${milliseconds}ms"
color="%{$fg_bold[green]%}"
fi
# Append the time string to RPROMPT
if [[ -n $time_str ]]; then
RPROMPT="${RPROMPT} ${color}${time_str}%{$reset_color%}"
fi
fi
}