From 89598d72d5b16166ecae3c3dbf0ac1aa9002ea05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20P=C5=82azie=C5=84ski?= Date: Sun, 8 Sep 2024 18:06:25 +0200 Subject: [PATCH] Move to spaceship prompt --- zsh.d/50_prompt.zsh | 162 ----------------------------------------- zsh.d/50_spaceship.zsh | 36 +++++++++ zsh.d/50_vcs.zsh | 56 -------------- zshrc | 48 ++---------- 4 files changed, 41 insertions(+), 261 deletions(-) delete mode 100644 zsh.d/50_prompt.zsh create mode 100644 zsh.d/50_spaceship.zsh delete mode 100644 zsh.d/50_vcs.zsh diff --git a/zsh.d/50_prompt.zsh b/zsh.d/50_prompt.zsh deleted file mode 100644 index b48afc7..0000000 --- a/zsh.d/50_prompt.zsh +++ /dev/null @@ -1,162 +0,0 @@ -# vim: ft=zsh - -# Based on agnoster's Theme - https://gist.github.com/3712874 - -CURRENT_BG='NONE' - -if [[ -n $MULTIBYTE_SUPPORTED ]]; then - FAILED_CHARACTER="\ue125" - SUCCESS_CHARACTER="\ue124" - SUPERUSER_CHARACTER="\ue22b" - JOBS_CHARACTER="\ue12a" - NO_JOBS_CHARACTER="\u2022" - SEGMENT_SEPARATOR_FORWARD="\ue0b0" - SEGMENT_SEPARATOR_BACKWARD="\ue0b2" -else - FAILED_CHARACTER="X" - SUCCESS_CHARACTER="V" - SUPERUSER_CHARACTER="#" - JOBS_CHARACTER="O" - NO_JOBS_CHARACTER="." - SEGMENT_SEPARATOR_FORWARD="" - SEGMENT_SEPARATOR_BACKWARD="" -fi - -prompt_segment() { - local direction newbg newfg text - direction="$1" - newbg="$2" - newfg="$3" - text="$4" - if [[ -z $text ]]; then return; fi - if [[ $newbg != $CURRENT_BG ]]; then - if [[ "$direction" == 'forward' ]]; then - if [[ $CURRENT_BG != 'NONE' ]]; then - print -n "%{%K{$newbg}%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR_FORWARD%{%F{$newfg}%}" - else - print -n "%{%K{$newbg}%F{$newfg}%}" - fi - else - print -n "%{%F{$newbg}%}$SEGMENT_SEPARATOR_BACKWARD%{%F{$newfg}%K{$newbg}%}" - fi - fi - print -n " $text " - CURRENT_BG=$newbg -} - -# End the prompt, closing any open segments -prompt_end() { - if [[ -n $CURRENT_BG ]]; then - print -n "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR_FORWARD" - fi - CURRENT_BG='' -} - -prompt_clear() { - print -n "%{%k%f%}" - CURRENT_BG='' -} - -### Prompt components -# Each component will draw itself, and hide itself if no information needs to be shown - -# Root privileges -prompt_root() { - if [[ $UID -eq 0 ]]; then - print -n $SUPERUSER_CHARACTER - fi -} - -# Different username -prompt_user() { - local user=$USER - if command-exists whoami && [[ -z $user ]]; then - user=$(whoami) - fi - if [[ "$user" != "$DEFAULT_USER" && $UID -ne 0 ]]; then - print -n $user - fi -} - -# Different host -prompt_host() { - if [[ -n "$SSH_CONNECTION" ]]; then - print -n "%m" - fi -} - -# Makefile exists -prompt_makefile() { - if [[ -f Makefile ]]; then - print -n "make" - fi -} - -# Status: -# - was there an error -# - are there background jobs? -prompt_status() { - local symbols - symbols=() - if [[ $LAST_RETURN_VALUE -ne 0 ]]; then - symbols+="%{%F{red}%}$FAILED_CHARACTER%{%f%}" - else - symbols+="%{%F{green}%}$SUCCESS_CHARACTER%{%f%}" - fi - if [[ $(jobs -l) ]]; then - symbols+="%{%F{cyan}%}$JOBS_CHARACTER%{%f%}" - else - symbols+="%{%F{white}%}$NO_JOBS_CHARACTER%{%f%}" - fi - echo "$symbols" -} - -## Main prompt -prompt_forward() { - CURRENT_BG='NONE' - prompt_segment forward black default "$(prompt_status)" - prompt_segment forward red yellow "$(prompt_root)" - prompt_segment forward magenta black "$(prompt_user)" - prompt_segment forward cyan black "$(prompt_host)" - prompt_segment forward blue black '%~' # prompt directory - prompt_end - prompt_clear -} - -## Reverse prompt -prompt_backward() { - CURRENT_BG='NONE' - prompt_segment backward magenta black "$MAVEN_PROJECT" # prompt maven project - prompt_segment backward cyan black "$PACKAGE_JSON_PROJECT" # prompt package.json project - prompt_segment backward cyan black "$(prompt_makefile)" - prompt_segment backward yellow black "$vcs_info_msg_0_" # prompt vcs - prompt_segment backward green black "%T" # prompt time - prompt_clear -} - -prompt2_forward() { - CURRENT_BG='NONE' - prompt_segment forward black default "$(prompt_status)" - prompt_segment forward red yellow "$(prompt_root)" - prompt_segment forward magenta black "$(prompt_user)" - prompt_segment forward cyan black "$(prompt_host)" - prompt_segment forward blue black '%~' # prompt directory - prompt_segment forward red black '%_' # unmatched quote - prompt_end - prompt_clear -} - -prompt_precmd() { - vcs_info - PROMPT="%{%f%b%k%}$(prompt_forward) " - PS="$PROMPT" - PS2="%{%f%b%k%}$(prompt2_forward) " - RPROMPT="%{%f%b%k%}$(prompt_backward)" - PRS="$RPROMPT" - SPROMPT="Correct %{%F{red}%}%R%{%f%} to %{%F{green}%}%r%f? [%Uy%ues|%Un%uo|%Ua%ubort|%Ue%udit] " -} - -ZLE_RPROMPT_INDENT=1 -prompt_opts=(cr subst percent) - -add-zsh-hook precmd prompt_precmd diff --git a/zsh.d/50_spaceship.zsh b/zsh.d/50_spaceship.zsh new file mode 100644 index 0000000..77f3f8f --- /dev/null +++ b/zsh.d/50_spaceship.zsh @@ -0,0 +1,36 @@ +# vim: ft=zsh + +SPACESHIP_ASYNC_SHOW_COUNT=true + +SPACESHIP_GIT_BRANCH_PREFIX="" + +SPACESHIP_GIT_STATUS_SHOW=true + +SPACESHIP_GIT_STATUS_PREFIX="" +SPACESHIP_GIT_STATUS_SUFFIX="" + +SPACESHIP_MAVEN_ARTIFACT_SUFFIX=" " +SPACESHIP_MAVEN_VERSION_SUFFIX=" " + +SPACESHIP_USER_SHOW=needed + +SPACESHIP_PROMPT_ORDER=( + time + user + host + dir + git + package + maven_artifact + maven_version + exec_time + line_sep + battery + jobs + char +) + +# maven excluded +SPACESHIP_PACKAGE_ORDER=(npm lerna cargo composer julia gradle python dart) + + diff --git a/zsh.d/50_vcs.zsh b/zsh.d/50_vcs.zsh deleted file mode 100644 index 830f790..0000000 --- a/zsh.d/50_vcs.zsh +++ /dev/null @@ -1,56 +0,0 @@ -# vim: ft=zsh - -if [[ -n $MULTIBYTE_SUPPORTED ]]; then - UNSTAGED_CHARACTER="\ue168" - CHANGES_CHARACTER="\ue16b" - UNTRACKED_CHARACTER="\ue16c" - BRANCH_CHARACTER="\ue822" - REVISION_CHARACTER="\ue821" - AHEAD_CHARACTER="\ue174 " - BEHIND_CHARACTER="\ue175 " - ACTIONS_CHARACTER="\ue831" -else - UNSTAGED_CHARACTER="!" - CHANGES_CHARACTER="*" - UNTRACKED_CHARACTER="?" - BRANCH_CHARACTER="~" - REVISION_CHARACTER="r" - AHEAD_CHARACTER="+" - BEHIND_CHARACTER="-" - ACTIONS_CHARACTER="!" -fi - -autoload -Uz vcs_info - -zstyle ':vcs_info:*' enable git hg svn -zstyle ':vcs_info:*' check-for-changes true -zstyle ':vcs_info:*' get-revision true - -# these formats are set for PROMPT -zstyle ':vcs_info:*' formats "%s $BRANCH_CHARACTER%b $REVISION_CHARACTER%i%u" -zstyle ':vcs_info:*' actionformats "%s $BRANCH_CHARACTER%b $REVISION_CHARACTER%i%u [%a]" -zstyle ':vcs_info:*' branchformat '%b' - -zstyle ':vcs_info:hg*' unstagedstr "$CHANGES_CHARACTER" -zstyle ':vcs_info:hg*' hgrevformat "%r" # default "%r:%h" - -zstyle ':vcs_info:git*' formats "$BRANCH_CHARACTER%b%u%c%m" # git is standard -zstyle ':vcs_info:git*' actionformats "$BRANCH_CHARACTER%b%u%c%m $ACTIONS_CHARACTER%a" -zstyle ':vcs_info:git*' unstagedstr " $UNSTAGED_CHARACTER" -zstyle ':vcs_info:git*' stagedstr " $CHANGES_CHARACTER" - -zstyle ':vcs_info:git*+set-message:*' hooks git-additional - -function +vi-git-additional() { - local ahead behind - local -a branchstatus - ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l) - (($ahead)) && branchstatus+=(" $AHEAD_CHARACTER${ahead}") - behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l) - (($behind)) && branchstatus+=(" $BEHIND_CHARACTER${behind}") - hook_com[misc]="${(j::)branchstatus}" - if [[ $(git rev-parse --is-inside-work-tree 2>/dev/null) == 'true' && - -n $(git status --porcelain | grep -E '^\?\?' 2>/dev/null | tail -n1) ]]; then - hook_com[unstaged]+=" $UNTRACKED_CHARACTER" - fi -} diff --git a/zshrc b/zshrc index ab69402..b745653 100644 --- a/zshrc +++ b/zshrc @@ -164,47 +164,6 @@ vim_pager() { } alias vless='vim_pager' -#---------------------------------- Maven ------------------------------------ -# Read project information from current directory - needed for prompt - -maven_read_project() { - local location parts - location=$(find_up pom.xml) - if [[ ! -r "$location" || -z $commands[xmllint] ]]; then - MAVEN_PROJECT="" - return 1 - fi - # executing xmllint takes some time, so this does it in single execution - parts=($(echo "cat /*[local-name()='project']/*[local-name()='artifactId']/text()\n" \ - "cat /*[local-name()='project']/*[local-name()='version']/text()\n" \ - "cat /*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']/text()\n" | - xmllint --shell $location 2>/dev/null | - sed '/^\/ >/d' | - sed '/^ -------/d')) - if [[ "${#parts}" > 1 ]]; then - MAVEN_PROJECT="${parts[1]}@${parts[2]}" - else - MAVEN_PROJECT="pom!" - fi -} - -add-zsh-hook chpwd maven_read_project - -#---------------------------------- package.json ------------------------------------ -# Read project information from current directory - needed for prompt - -package_json_read_project() { - local location parts - location=$(find_up package.json) - if [[ ! -r "$location" || -z $commands[jq] ]]; then - PACKAGE_JSON_PROJECT="" - return 1 - fi - PACKAGE_JSON_PROJECT=$(jq -r '.name + "@" + .version' $location) -} - -add-zsh-hook chpwd package_json_read_project - #---------------------------------- Miscellaneous ---------------------------- setopt extended_glob @@ -294,13 +253,16 @@ source ${ZPLUG_ROOT}/init.zsh zplug "plugins/git", from:oh-my-zsh zplug "plugins/sudo", from:oh-my-zsh zplug "plugins/command-not-found", from:oh-my-zsh -zplug "zsh-users/zsh-syntax-highlighting" +zplug "zsh-users/zsh-syntax-highlighting", defer:2 zplug "zsh-users/zsh-autosuggestions" zplug "zsh-users/zsh-history-substring-search" zplug "zsh-users/zsh-completions" zplug "junegunn/fzf", use:"shell/*.zsh" -zplug "$ZDOTDIR/zsh.d", from:local +zplug "spaceship-prompt/spaceship-prompt", use:spaceship.zsh, from:github, as:theme +zplug "nivertius/spaceship-maven-project", use:spaceship-maven-project.plugin.zsh, from:github, as:theme + +zplug "$ZDOTDIR/zsh.d", from:local, defer:1 if ! zplug check --verbose; then printf "Install? [y/N]: "