diff --git a/.zshrc b/.zshrc index 8e6c1cb..0be974c 100644 --- a/.zshrc +++ b/.zshrc @@ -2,39 +2,47 @@ # export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH # 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=( - git - zsh-syntax-highlighting - # zsh-autosuggestions - zsh-autocomplete - colored-man-pages - sudo -) +# plugins=( +# git +# zsh-syntax-highlighting +# # zsh-autosuggestions +# zsh-autocomplete +# colored-man-pages +# 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 source <(fzf --zsh) +zinit snippet OMZ::lib/git.zsh + [ -f ~/zsh/aliases.sh ] && source ~/zsh/aliases.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 -PROMPT+="%{$fg[cyan]%}%c%{$reset_color%} " +PATH+=":/home/anton/Rider/bin" -# Arrow -PROMPT+="%(?:%{$fg_bold[green]%}%1{➜%} :%{$fg[red]%}%? %{$fg_bold[red]%}%1{➜%} )" +zinit ice wait'!0' -# Git -PROMPT+='$(git_prompt_info)' +[ -f ~/zsh/plugins.sh ] && source ~/zsh/plugins.sh + +# source ${ZINIT_HOME}/../plugins/marlonrichert---zsh-autocomplete/zsh-autocomplete.plugin.zsh +# zstyle ':autocomplete:*' delay 0.1 # seconds (float) diff --git a/zsh/aliases.sh b/zsh/aliases.sh index b3ffa96..6528b5e 100644 --- a/zsh/aliases.sh +++ b/zsh/aliases.sh @@ -9,9 +9,9 @@ alias apps='cd ~/.local/share/applications/' alias louis='ssh anton@192.168.178.48' # LS -alias ls='lsd' -alias la='lsd -a' -alias ll='lsd -l' +alias ls='ls --color=always -F' +alias la='ls -a' +alias ll='ls -l' # WG alias wgon='wg-quick up ~/Downloads/wg0.conf' diff --git a/zsh/completion.sh b/zsh/completion.sh new file mode 100644 index 0000000..571bfde --- /dev/null +++ b/zsh/completion.sh @@ -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 diff --git a/zsh/functions.sh b/zsh/functions.sh index d1470c4..4c81df7 100644 --- a/zsh/functions.sh +++ b/zsh/functions.sh @@ -31,48 +31,3 @@ split() { (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 -} diff --git a/zsh/git.sh b/zsh/git.sh new file mode 100644 index 0000000..d73a554 --- /dev/null +++ b/zsh/git.sh @@ -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}" +} diff --git a/zsh/plugins.sh b/zsh/plugins.sh new file mode 100644 index 0000000..8d8639b --- /dev/null +++ b/zsh/plugins.sh @@ -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 + diff --git a/zsh/prompt.sh b/zsh/prompt.sh new file mode 100644 index 0000000..efa3d24 --- /dev/null +++ b/zsh/prompt.sh @@ -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 +}