From b11e28914139fb585c0a4de4ddb6a55f19a5493d Mon Sep 17 00:00:00 2001 From: Isaac Wolkerstorfer Date: Thu, 13 Sep 2012 14:02:09 +0200 Subject: [PATCH 1/4] Add agnoster.zsh-theme --- themes/agnoster.zsh-theme | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 themes/agnoster.zsh-theme diff --git a/themes/agnoster.zsh-theme b/themes/agnoster.zsh-theme new file mode 100644 index 000000000..15848979e --- /dev/null +++ b/themes/agnoster.zsh-theme @@ -0,0 +1,28 @@ +# Theme by [agnoster](https://github.com/agnoster) +# See https://gist.github.com/3712874 + +ZSH_THEME_GIT_PROMPT_DIRTY='±' + +function _git_prompt_info() { + ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git show-ref --head -s --abbrev |head -n1 2> /dev/null)" + echo "${ref/refs\/heads\//⭠ }$(parse_git_dirty)" +} + +function _git_info() { + if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then + local BG_COLOR=green + if [[ -n $(parse_git_dirty) ]]; then + BG_COLOR=yellow + fi + echo "%{%K{$BG_COLOR}%}⮀%{%F{black}%} $(_git_prompt_info) %{%F{$BG_COLOR}%K{blue}%}⮀" + else + echo "%{%K{blue}%}⮀" + fi +} + +PROMPT_HOST='%{%b%F{gray}%K{black}%} %(?.%{%F{green}%}✔.%{%F{red}%}✘)%{%F{gray}%} %m %{%F{black}%}' +PROMPT_DIR='%{%F{white}%} %1~ ' +PROMPT_SU='%(!.%{%k%F{blue}%K{black}%}⮀%{%F{yellow}%} ⚡ %{%k%F{black}%}.%{%k%F{blue}%})⮀%{%f%k%b%}' + +PROMPT='%{%f%b%k%} +$PROMPT_HOST$(_git_info)$PROMPT_DIR$PROMPT_SU ' From bf4157514f8880ac04ba328f7f1b74c34f1b39d7 Mon Sep 17 00:00:00 2001 From: Isaac Wolkerstorfer Date: Mon, 17 Sep 2012 11:44:25 -0500 Subject: [PATCH 2/4] Refactored theme --- themes/agnoster.zsh-theme | 96 +++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 19 deletions(-) diff --git a/themes/agnoster.zsh-theme b/themes/agnoster.zsh-theme index 15848979e..9cfcff3d0 100644 --- a/themes/agnoster.zsh-theme +++ b/themes/agnoster.zsh-theme @@ -1,28 +1,86 @@ -# Theme by [agnoster](https://github.com/agnoster) -# See https://gist.github.com/3712874 +### Segment drawing +# A few utility functions to make it easy and re-usable to draw segmented prompts -ZSH_THEME_GIT_PROMPT_DIRTY='±' - -function _git_prompt_info() { - ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git show-ref --head -s --abbrev |head -n1 2> /dev/null)" - echo "${ref/refs\/heads\//⭠ }$(parse_git_dirty)" +CURRENT_BG='' +SEGMENT_SEPARATOR='⮀' +function segment_start() { + local bg=$1 + local fg=$2 + if [[ -n $CURRENT_BG && $bg != $CURRENT_BG ]]; then + echo -n " %{%K{$bg}%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR" + else + echo -n "%{%K{$bg}%}" + fi + [[ -n $fg ]] && fg="%F{$fg}" || fg="%f" + echo -n "%{$fg%} " + CURRENT_BG=$bg } -function _git_info() { - if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then - local BG_COLOR=green - if [[ -n $(parse_git_dirty) ]]; then - BG_COLOR=yellow - fi - echo "%{%K{$BG_COLOR}%}⮀%{%F{black}%} $(_git_prompt_info) %{%F{$BG_COLOR}%K{blue}%}⮀" +function segment_stop() { + if [[ -n $CURRENT_BG ]]; then + echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR" else - echo "%{%K{blue}%}⮀" + echo -n "%{%k%}" + fi + echo -n "%{%f%}" + CURRENT_BG='' +} + +### Prompt components +# Each component will draw itself, and hide itself if no information needs to be shown + +function prompt_context() { + local user=`whoami` + + if [[ ("$user" != "$DEFAULT_USER") || (-n "$SSH_CLIENT") ]]; then + segment_start black + #echo -n "%{%F{yellow}%}$user%{%F{gray}%}@%{%F{green}%}%m%{%f%}" + echo -n "%(!.%{%F{yellow}%}.)$user@%m" fi } -PROMPT_HOST='%{%b%F{gray}%K{black}%} %(?.%{%F{green}%}✔.%{%F{red}%}✘)%{%F{gray}%} %m %{%F{black}%}' -PROMPT_DIR='%{%F{white}%} %1~ ' -PROMPT_SU='%(!.%{%k%F{blue}%K{black}%}⮀%{%F{yellow}%} ⚡ %{%k%F{black}%}.%{%k%F{blue}%})⮀%{%f%k%b%}' +function prompt_git() { + if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then + ZSH_THEME_GIT_PROMPT_DIRTY='±' + local dirty=$(parse_git_dirty) + local ref + ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git show-ref --head -s --abbrev |head -n1 2> /dev/null)" + if [[ -n $dirty ]]; then + segment_start yellow black + else + segment_start green black + fi + echo -n "${ref/refs\/heads\//⭠ }$dirty" + fi +} + +function prompt_dir() { + segment_start blue white + echo -n '%~' +} + +function prompt_status() { + local symbols + symbols=() + [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘" + [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡" + jobs=$(jobs -l | wc -l) + [[ $jobs -gt 0 ]] && symbols+="%{%F{cyan}%}⚙" + if [[ -n "$symbols" ]]; then + segment_start black white + echo -n "${symbols}" + fi +} + +## Main prompt +function build_prompt() { + RETVAL=$? + prompt_status + prompt_context + prompt_dir + prompt_git + segment_stop +} PROMPT='%{%f%b%k%} -$PROMPT_HOST$(_git_info)$PROMPT_DIR$PROMPT_SU ' +$(build_prompt) ' From 6e85ff5b44e81f5b9d743b6ba1df7ed7b62dc2df Mon Sep 17 00:00:00 2001 From: Isaac Wolkerstorfer Date: Tue, 25 Sep 2012 18:35:08 -0500 Subject: [PATCH 3/4] Updated documentation for agnoster theme --- themes/agnoster.zsh-theme | 99 +++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 35 deletions(-) diff --git a/themes/agnoster.zsh-theme b/themes/agnoster.zsh-theme index 9cfcff3d0..71bc25fc2 100644 --- a/themes/agnoster.zsh-theme +++ b/themes/agnoster.zsh-theme @@ -1,22 +1,51 @@ +# vim:ft=zsh ts=2 sw=2 sts=2 +# +# agnoster's Theme +# A Powerline-inspired theme for ZSH +# +# # README +# +# In order for this theme to render correctly, you will need a +# [Powerline-patched font](https://gist.github.com/1595572). +# +# In addition, I recommend the +# [Solarized theme](https://github.com/altercation/solarized/) and, if you're +# using it on Mac OS X, [iTerm 2](http://www.iterm2.com/) over Terminal.app - +# it has significantly better color fidelity. +# +# # Goals +# +# The aim of this theme is to only show you *relevant* information. Like most +# prompts, it will only show git information when in a git working directory. +# However, it goes a step further: everything from the current user and +# hostname to whether the last call exited with an error to whether background +# jobs are running in this shell will all be displayed automatically when +# appropriate. + ### Segment drawing # A few utility functions to make it easy and re-usable to draw segmented prompts -CURRENT_BG='' +CURRENT_BG='NONE' SEGMENT_SEPARATOR='⮀' -function segment_start() { - local bg=$1 - local fg=$2 - if [[ -n $CURRENT_BG && $bg != $CURRENT_BG ]]; then - echo -n " %{%K{$bg}%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR" + +# 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 -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} " else - echo -n "%{%K{$bg}%}" + echo -n "%{$bg%}%{$fg%} " fi - [[ -n $fg ]] && fg="%F{$fg}" || fg="%f" - echo -n "%{$fg%} " - CURRENT_BG=$bg + CURRENT_BG=$1 + [[ -n $3 ]] && echo -n $3 } -function segment_stop() { +# End the prompt, closing any open segments +prompt_end() { if [[ -n $CURRENT_BG ]]; then echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR" else @@ -29,58 +58,58 @@ function segment_stop() { ### Prompt components # Each component will draw itself, and hide itself if no information needs to be shown -function prompt_context() { +# Context: user@hostname (who am I and where am I) +prompt_context() { local user=`whoami` - if [[ ("$user" != "$DEFAULT_USER") || (-n "$SSH_CLIENT") ]]; then - segment_start black - #echo -n "%{%F{yellow}%}$user%{%F{gray}%}@%{%F{green}%}%m%{%f%}" - echo -n "%(!.%{%F{yellow}%}.)$user@%m" + if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then + prompt_segment black default "%(!.%{%F{yellow}%}.)$user@%m" fi } -function prompt_git() { +# Git: branch/detached head, dirty status +prompt_git() { + local ref dirty if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then ZSH_THEME_GIT_PROMPT_DIRTY='±' - local dirty=$(parse_git_dirty) - local ref + dirty=$(parse_git_dirty) ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git show-ref --head -s --abbrev |head -n1 2> /dev/null)" if [[ -n $dirty ]]; then - segment_start yellow black + prompt_segment yellow black else - segment_start green black + prompt_segment green black fi echo -n "${ref/refs\/heads\//⭠ }$dirty" fi } -function prompt_dir() { - segment_start blue white - echo -n '%~' +# Dir: current working directory +prompt_dir() { + prompt_segment blue black '%~' } -function prompt_status() { +# Status: +# - was there an error +# - am I root +# - are there background jobs? +prompt_status() { local symbols symbols=() [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘" [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡" - jobs=$(jobs -l | wc -l) - [[ $jobs -gt 0 ]] && symbols+="%{%F{cyan}%}⚙" - if [[ -n "$symbols" ]]; then - segment_start black white - echo -n "${symbols}" - fi + [[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙" + + [[ -n "$symbols" ]] && prompt_segment black default "$symbols" } ## Main prompt -function build_prompt() { +build_prompt() { RETVAL=$? prompt_status prompt_context prompt_dir prompt_git - segment_stop + prompt_end } -PROMPT='%{%f%b%k%} -$(build_prompt) ' +PROMPT='%{%f%b%k%}$(build_prompt) ' From b207792f30f523e2fc82a70e75893bf1124bf7a8 Mon Sep 17 00:00:00 2001 From: Isaac Wolkerstorfer Date: Tue, 25 Sep 2012 19:06:13 -0500 Subject: [PATCH 4/4] Add link to gist --- themes/agnoster.zsh-theme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/themes/agnoster.zsh-theme b/themes/agnoster.zsh-theme index 71bc25fc2..c3107c06c 100644 --- a/themes/agnoster.zsh-theme +++ b/themes/agnoster.zsh-theme @@ -1,6 +1,6 @@ # vim:ft=zsh ts=2 sw=2 sts=2 # -# agnoster's Theme +# agnoster's Theme - https://gist.github.com/3712874 # A Powerline-inspired theme for ZSH # # # README