Skip to content

Instantly share code, notes, and snippets.

@agnoster
Last active September 25, 2024 09:27
Show Gist options
  • Save agnoster/3712874 to your computer and use it in GitHub Desktop.
Save agnoster/3712874 to your computer and use it in GitHub Desktop.
My ZSH Theme

agnoster.zsh-theme

A ZSH theme optimized for people who use:

  • Solarized
  • Git
  • Unicode-compatible fonts and terminals (I use iTerm2 + Menlo)

For Mac users, I highly recommend iTerm 2 + Solarized Dark

Compatibility

NOTE: In all likelihood, you will need to install a Powerline-patched font for this theme to render correctly.

To test if your terminal and font support it, check that all the necessary characters are supported by copying the following command to your terminal: echo "\ue0b0 \u00b1 \ue0a0 \u27a6 \u2718 \u26a1 \u2699". The result should look like this:

Character Example

What does it show?

  • If the previous command failed (✘)
  • User @ Hostname (if user is not DEFAULT_USER, which can then be set in your profile)
  • Git status
    • Branch () or detached head (➦)
    • Current branch / SHA1 in detached head state
    • Dirty working directory (±, color change)
  • Working directory
  • Elevated (root) privileges (⚡)

Screenshot

Future Work

I don't want to clutter it up too much, but I am toying with the idea of adding RVM (ruby version) and n (node.js version) display.

It's currently hideously slow, especially inside a git repo. I guess it's not overly so for comparable themes, but it bugs me, and I'd love to hear ideas about how to improve the performance.

Would be nice for the code to be a bit more sane and re-usable. Something to easily append a section with a given FG/BG, and add the correct opening and closing.

Also the dependency on a powerline-patched font is regrettable, but there's really no way to get that effect without it. Ideally there would be a way to check for compatibility, or maybe even fall back to one of the similar unicode glyphs.

# vim:ft=zsh ts=2 sw=2 sts=2
#
# agnoster's Theme - https://gist.github.com/3712874
# 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='NONE'
PRIMARY_FG=black
# Characters
SEGMENT_SEPARATOR="\ue0b0"
PLUSMINUS="\u00b1"
BRANCH="\ue0a0"
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
print -n "%{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%}"
else
print -n "%{$bg%}%{$fg%}"
fi
CURRENT_BG=$1
[[ -n $3 ]] && print -n $3
}
# End the prompt, closing any open segments
prompt_end() {
if [[ -n $CURRENT_BG ]]; then
print -n "%{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
else
print -n "%{%k%}"
fi
print -n "%{%f%}"
CURRENT_BG=''
}
### Prompt components
# Each component will draw itself, and hide itself if no information needs to be shown
# Context: user@hostname (who am I and where am I)
prompt_context() {
local user=`whoami`
if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CONNECTION" ]]; then
prompt_segment $PRIMARY_FG default " %(!.%{%F{yellow}%}.)$user@%m "
fi
}
# Git: branch/detached head, dirty status
prompt_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 $ref"
else
ref="$DETACHED ${ref/.../}"
fi
prompt_segment $color $PRIMARY_FG
print -Pn " $ref"
fi
}
# Dir: current working directory
prompt_dir() {
prompt_segment blue $PRIMARY_FG ' %~ '
}
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
local symbols
symbols=()
[[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}$CROSS"
[[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}$LIGHTNING"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}$GEAR"
[[ -n "$symbols" ]] && prompt_segment $PRIMARY_FG default " $symbols "
}
## Main prompt
prompt_agnoster_main() {
RETVAL=$?
CURRENT_BG='NONE'
prompt_status
prompt_context
prompt_dir
prompt_git
prompt_end
}
prompt_agnoster_precmd() {
vcs_info
PROMPT='%{%f%b%k%}$(prompt_agnoster_main) '
}
prompt_agnoster_setup() {
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
prompt_opts=(cr subst percent)
add-zsh-hook precmd prompt_agnoster_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_agnoster_setup "$@"
@hypefi
Copy link

hypefi commented Jan 5, 2021

quick fix that worked for me :
cd ~/.oh-my-zsh/themes
vi agnoster.zsh-theme
replace (\ue0b0) with ▓ (\u2593) and  (\ue0a0) with ⎇ (\u2387)

@davidhcefx
Copy link

davidhcefx commented Feb 27, 2022

@agnoster I like your theme. But I noticed that in line 118, you wrote:

[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}$GEAR"

I would however suggest changing it to %1(j.%{%F{cyan}%}$GEAR.) for better performance.

Proof-of-concept

$ GEAR="\u2699"
$ f1() { local s; [[ $(jobs -l | wc -l) -gt 0 ]] && s+="%{%F{cyan}%}$GEAR"; print -n "$s" }
$ f2() { local s; s+="%1(j.%{%F{cyan}%}$GEAR.)"; print -n "$s" }
$ timer() { date +%S.%N >> /tmp/timer.log }
$ timer_newline() { echo "" >> /tmp/timer.log }
$ precmd_functions=(timer)

# test original version (w/ and w/o background jobs)
$ PROMPT='$(f1) > '
$ <long press Enter>
$ timer_newline; cat &
$ <long press Enter>
$ timer_newline; kill %%

# test modified version (w/ and w/o background jobs)
$ PROMPT='$(f2) > '
$ <long press Enter>
$ timer_newline; cat &
$ <long press Enter>
$ timer_newline; kill %%

After some processing we got:

for test in open('/tmp/timer.log').read().strip().split('\n\n'):
    lines = test.split('\n')
    assert len(lines) >= 10, "Must have at least 10 logs. Try again and press Enter longer."
    lines = list(map(float, lines[-10:]))
    print(sum(lines[i + 1] - lines[i] for i in range(9)) / 10)

"""
0.34356011
0.3448782799999998
0.1385942700000001
0.13762055999999986
"""

Therefore it's an increase in performance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment