From d5eab2ec71d08ade9565f148e65631e542b4d079 Mon Sep 17 00:00:00 2001 From: Ciacho Date: Mon, 7 Mar 2016 16:15:56 +0100 Subject: [PATCH] Update theme ciacho --- themes/ciacho.zsh-theme | 321 +++++++++++++++++++++++++--------------- 1 file changed, 205 insertions(+), 116 deletions(-) diff --git a/themes/ciacho.zsh-theme b/themes/ciacho.zsh-theme index c361fb097..464dc275e 100644 --- a/themes/ciacho.zsh-theme +++ b/themes/ciacho.zsh-theme @@ -1,112 +1,66 @@ -CIACHO_VERSION="0.1d" -function my_git_prompt() { - tester=$(git rev-parse --git-dir 2> /dev/null) || return - - INDEX=$(git status --porcelain 2> /dev/null) - STATUS="" +# vim:ft=zsh ts=2 sw=2 sts=2 +# +# Ciacho Theme +# Based on agnoster theme +# In order for this theme to render correctly, you will need a +# [Powerline-patched font](https://github.com/Lokaltog/powerline-fonts). +# Make sure you have a recent version: the code points that Powerline +# uses changed in 2012, and older versions will display incorrectly, +# in confusing ways. - # is branch ahead? - if $(echo "$(git log origin/$(current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then - STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_AHEAD" + +CIACHO_VERSION="0.2" + + +### Segment drawing +# A few utility functions to make it easy and re-usable to draw segmented prompts + +CURRENT_BG='NONE' +PRIMARY_FG=black + +#SEGMENT_SEPARATOR=" " +SEGMENT_SEPARATOR="\ue0b0" + +PLUSMINUS="\u00b1" +BRANCH="\ue0a0" +BRANCH_BEGIN="‹ \ue0a0" +BRANCH_END="›" +DETACHED="\u27a6" +CROSS="\u2718" +LIGHTNING="\u26a1" +GEAR="\u2699" + + +# Begin a segment +# Takes two arguments, background and foreground. Both can be omitted, +# rendering default background/foreground. +prompt_segment() { + local bg fg + [[ -n $1 ]] && bg="%K{$1}" || bg="%k" + [[ -n $2 ]] && fg="%F{$2}" || fg="%f" + if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then + echo -ne "%{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%}" + else + echo -ne "%{$bg%}%{$fg%}" fi - - # is anything staged? - if $(echo "$INDEX" | grep -E -e '^(D[ M]|[MARC][ MD]) ' &> /dev/null); then - STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_STAGED" - fi - - # is anything unstaged? - if $(echo "$INDEX" | grep -E -e '^[ MARC][MD] ' &> /dev/null); then - STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_UNSTAGED" - fi - - # is anything untracked? - if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then - STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_UNTRACKED" - fi - - # is anything unmerged? - if $(echo "$INDEX" | grep -E -e '^(A[AU]|D[DU]|U[ADU]) ' &> /dev/null); then - STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_UNMERGED" - fi - - if [[ -n $STATUS ]]; then - STATUS=" $STATUS" - fi - - echo "$ZSH_THEME_GIT_PROMPT_PREFIX$(my_current_branch)$STATUS$ZSH_THEME_GIT_PROMPT_SUFFIX" + CURRENT_BG=$1 + [[ -n $3 ]] && echo -n $3 } -function my_current_branch() { - echo $(current_branch || echo "(no branch)") -} - -function ssh_connection() { - if [[ -n $SSH_CONNECTION ]]; then - echo "%{$fg_bold[green]%}(ssh)%{$reset_color%} " - elif [[ -n $SUDO_USER ]]; then - echo "%{$fg_bold[red]%}(sudo)%{$reset_color%} " - fi -} - -function vps_check() { - if [[ -f /etc/vps ]]; then - echo "%{$fg_bold[red]%}(VPS)%{$reset_color%} " +# End the prompt, closing any open segments +prompt_end() { + if [[ -n $CURRENT_BG ]]; then + echo -ne "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR" + else + echo -ne "%{%k%}" fi + echo -ne "%{%f%}" + CURRENT_BG='' } -function ciacho_version() { - if [[ -n $CIACHO_VERSION ]]; then - echo " %{$fg_bold[blue]%}($CIACHO_VERSION)%{$reset_color%} " - fi -} - -function locale_check() { - if [[ $LANG == "pl_PL.UTF-8" ]]; then - L_CODETITLE="%{$fg_bold[green]%}(UTF)%{$reset_color%} " - elif [[ $LANG == "pl_PL" ]]; then - L_CODETITLE="%{$fg_bold[white]%}(ISO)%{$reset_color%} " -# else -# L_CODETITLE="%{$fg_bold[green]%}(ISO)%{$reset_color%} " - fi - echo $L_CODETITLE -} - - if [[ $( whoami ) == root && -f /etc/vps ]]; then - ## czerwony @ bialy - l_user="%{$fg_bold[red]%}%n%{$reset_color%}" - l_host="%{$fg_bold[white]%}%m%{$reset_color%}" - l_pwd="%{$fg_bold[red]%}%#%{$reset_color%}" - elif [[ $( whoami ) == root ]]; then - ## czerwony @ cyan - l_user="%{$fg_bold[red]%}%n%{$reset_color%}" - l_host="%{$fg_bold[cyan]%}%m%{$reset_color%}" - l_pwd="%{$fg_bold[red]%}%#%{$reset_color%}" - elif [[ $( hostname ) == jail || -f /etc/vps ]]; then - ## bialy @ czerwony - l_user="%{$fg_bold[white]%}%n%{$reset_color%}" - l_host="%{$fg_bold[red]%}%m%{$reset_color%}" - l_pwd="%{$fg_bold[white]%}%#%{$reset_color%}" - elif [[ $( hostname ) == "Varacolaci.local" ]]; then - ## bialy @ czerwony - l_user="%{$fg_bold[blue]%}%n%{$reset_color%}" - l_host="%{$fg_bold[red]%}%m%{$reset_color%}" - l_pwd="%{$fg_bold[white]%}%#%{$reset_color%}" -# elif [[ $( hostname ) == "Varacolaci.local" ]]; then -# l_user=$PR_BLUE"%n"$PR_RESET" "$PR_BOLD_RED"%m"$PR_RESET -# elif [[ $( uname ) == Darwin ]]; then -# l_user=$PR_BLUE"%n"$PR_RESET" "$PR_BLUE"%m"$PR_RESET - else - l_user="%{$fg_bold[green]%}%n%{$reset_color%}" - l_host="%{$fg_bold[green]%}%m%{$reset_color%}" - l_pwd="%{$fg_bold[green]%}%#%{$reset_color%}" - fi -function user_check() { - echo "$l_user@$l_host" -} - - -function ciacho_battery() { +function prompt_ciacho_battery() { + local symbols +symbols=() if [[ $(uname) == "Darwin" ]] ; then if [[ $(ioreg -rc AppleSmartBattery | grep -c '^.*"ExternalConnected"\ =\ No') -eq 1 ]] ; then b=$(battery_pct) @@ -117,23 +71,158 @@ function ciacho_battery() { else color='red' fi - echo "%{$fg[$color]%}[$(battery_pct_remaining)%%]%{$reset_color%} :" - else - echo "%{$fg[yellow]%}[∞]%{$reset_color%} :" + symbols+="$LIGHTNING %{%F{$color}%}[$(battery_pct_remaining)%%]%}" + prompt_segment red default "$symbols" fi - fi } -#PROMPT=$'\n$(ssh_connection)$(vps_check)$(locale_check)$(user_check)%{$reset_color%}$(ciacho_version)$(my_git_prompt) : %~\n%# ' -PROMPT=$'\n$(ssh_connection)$(vps_check)$(locale_check)$(user_check)%{$reset_color%}$(ciacho_version)$(my_git_prompt) : $(ciacho_battery) %~\n$l_pwd ' -ZSH_THEME_PROMPT_RETURNCODE_PREFIX="%{$fg_bold[red]%}" -ZSH_THEME_GIT_PROMPT_PREFIX=" $fg[white]‹ %{$fg_bold[yellow]%}" -ZSH_THEME_GIT_PROMPT_AHEAD="%{$fg_bold[magenta]%}↑" -ZSH_THEME_GIT_PROMPT_STAGED="%{$fg_bold[green]%}●" -ZSH_THEME_GIT_PROMPT_UNSTAGED="%{$fg_bold[red]%}●" -ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg_bold[white]%}●" -ZSH_THEME_GIT_PROMPT_UNMERGED="%{$fg_bold[red]%}✕" -ZSH_THEME_GIT_PROMPT_SUFFIX=" $fg_bold[white]›%{$reset_color%}" +function prompt_ciacho_status() { + local symbols + symbols=() + [[ -n $SSH_CLIENT ]] && symbols+="%{%F{cyan}[SSH]" + [[ -f /etc/vps ]] && symbols+="%{%F{yellow}[VPS]" + [[ $UID -eq 0 ]] && symbols+="%{%F{red}%}[ROOT]" + [[ $LANG == "pl_PL.UTF-8" ]] && symbols+="%{%F{green}%}[UTF]" + [[ $LANG == "pl_PL" ]] && symbols+="%{%F{green}%}[ISO]" + + [[ -n "$symbols" ]] && prompt_segment black default "$symbols " +} + +prompt_ciacho_git() { + local color ref + is_dirty() { + test -n "$(git status --porcelain --ignore-submodules)" + } + ref="$vcs_info_msg_0_" + if [[ -n "$ref" ]]; then + if is_dirty; then + color=yellow + ref="${ref} $PLUSMINUS" + else + color=green + ref="${ref} " + fi + if [[ "${ref/.../}" == "$ref" ]]; then + ref="$BRANCH_BEGIN $ref $BRANCH_END" + else + ref="$DETACHED ${ref/.../}" + fi + prompt_segment $color $PRIMARY_FG + print -Pn " $ref" + fi +} + +prompt_ciacho_hg() { + local rev status + if $(hg id >/dev/null 2>&1); then + if $(hg prompt >/dev/null 2>&1); then + if [[ $(hg prompt "{status|unknown}") = "?" ]]; then + # if files are not added + prompt_segment red white + st='±' + elif [[ -n $(hg prompt "{status|modified}") ]]; then + # if any modification + prompt_segment yellow black + st='±' + else + # if working copy is clean + prompt_segment green black + fi + echo -n $(hg prompt "☿ {rev}@{branch}") $st + else + st="" + rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g') + branch=$(hg id -b 2>/dev/null) + if `hg st | grep -q "^\?"`; then + prompt_segment red black + st='±' + elif `hg st | grep -q "^[MA]"`; then + prompt_segment yellow black + st='±' + else + prompt_segment green black + fi + echo -n "☿ $rev@$branch" $st + fi + fi +} + +# Dir: current working directory +prompt_ciacho_dir() { + prompt_segment blue $PRIMARY_FG ' %~' +} + + +# Context: user@hostname (who am I and where am I) +prompt_ciacho_context() { + local uid_color + local host_color + + if [[ $(whoami) == root ]]; then + # red @ cyan + uid_color=red + host_color=white + elif [[ $(uname) == Darwin ]]; then + uid_color=cyan + host_color=cyan + elif [[ $(uname) == Darwin && $(whoami) == root ]]; then + uid_color=red + host_color=cyan + else + uid_color=white + host_color=white + fi + prompt_segment black default "%(!.%{%F{$uid_color}%}.)$USER%(!.%{%F{white}%}.)@%(!.%{%F{$host_color}%}.)%m" + +} + +prompt_ciacho_hash() { + if [[ $(whoami) == root ]]; then + prompt_segment red $PRIMARY_FG "\n %#" + else + prompt_segment blue $PRIMARY_FG "\n %#" + fi +} + +prompt_ciacho_main() { + RETVAL=$? + prompt_ciacho_status + prompt_ciacho_battery + prompt_ciacho_context + prompt_ciacho_git + prompt_ciacho_hg + prompt_ciacho_dir + prompt_end + prompt_ciacho_hash + prompt_end + +} + + +prompt_ciacho_precmd() { + vcs_info + PROMPT='%{%f%b%k%}$(prompt_ciacho_main) ' +} + + +prompt_ciacho_setup() { + autoload -Uz add-zsh-hook + autoload -Uz vcs_info + + prompt_opts=(cr subst percent) + + add-zsh-hook precmd prompt_ciacho_precmd + + zstyle ':vcs_info:*' enable git + zstyle ':vcs_info:*' check-for-changes false + zstyle ':vcs_info:git*' formats '%b' + zstyle ':vcs_info:git*' actionformats '%b (%a)' +} + +prompt_ciacho_setup "$@" + + +PROMPT='%{%f%b%k%}$(build_prompt) '