Skip to content

Commit

Permalink
configure colors with zstyle
Browse files Browse the repository at this point in the history
Available colors:
- branch
- branch:cached
- exec_time
- git_arrow
- host
- path
- prompt:error
- prompt:success
- user
- user:root, default color changed from `white` to `default`
- virtualenv
  • Loading branch information
bricewge committed Jun 12, 2019
1 parent e7036c4 commit 6570bea
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
52 changes: 42 additions & 10 deletions pure.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,30 @@ prompt_pure_preexec() {
export VIRTUAL_ENV_DISABLE_PROMPT=${VIRTUAL_ENV_DISABLE_PROMPT:-12}
}

# Change the colors if their value are different from the current ones
prompt_pure_set_colors() {
local color_temp
for key val in ${(kv)prompt_pure_colors}; do
if ! zstyle -T ":prompt:pure:$key" color "$val"; then
zstyle -s ":prompt:pure:$key" color color_temp
test -n "$color_temp" && prompt_pure_colors[$key]=$color_temp
fi
done
}

prompt_pure_preprompt_render() {
setopt localoptions noshwordsplit

# Set color for git branch/dirty status, change color if dirty checking has
# been delayed.
local git_color=242
[[ -n ${prompt_pure_git_last_dirty_check_timestamp+x} ]] && git_color=red
local git_color=$prompt_pure_colors[branch]
[[ -n ${prompt_pure_git_last_dirty_check_timestamp+x} ]] && git_color=$prompt_pure_colors[branch:cached]

# Initialize the preprompt array.
local -a preprompt_parts

# Set the path.
preprompt_parts+=('%F{blue}%~%f')
preprompt_parts+=('%F{${prompt_pure_colors[path]}}%~%f')

# Add git branch and dirty status info.
typeset -gA prompt_pure_vcs_info
Expand All @@ -124,13 +135,13 @@ prompt_pure_preprompt_render() {
fi
# Git pull/push arrows.
if [[ -n $prompt_pure_git_arrows ]]; then
preprompt_parts+=('%F{cyan}${prompt_pure_git_arrows}%f')
preprompt_parts+=('%F{$prompt_pure_colors[git_arrow]}${prompt_pure_git_arrows}%f')
fi

# Username and machine, if applicable.
[[ -n $prompt_pure_state[username] ]] && preprompt_parts+=('${prompt_pure_state[username]}')
# Execution time.
[[ -n $prompt_pure_cmd_exec_time ]] && preprompt_parts+=('%F{yellow}${prompt_pure_cmd_exec_time}%f')
[[ -n $prompt_pure_cmd_exec_time ]] && preprompt_parts+=('%F{$prompt_pure_colors[exec_time]}${prompt_pure_cmd_exec_time}%f')

local cleaned_ps1=$PROMPT
local -H MATCH MBEGIN MEND
Expand Down Expand Up @@ -174,6 +185,9 @@ prompt_pure_precmd() {
# shows the full path in the title
prompt_pure_set_title 'expand-prompt' '%~'

# Modify the colors if some have changed
prompt_pure_set_colors

# preform async git dirty check and fetch
prompt_pure_async_tasks

Expand Down Expand Up @@ -509,7 +523,7 @@ prompt_pure_state_setup() {

# Check SSH_CONNECTION and the current state.
local ssh_connection=${SSH_CONNECTION:-$PROMPT_PURE_SSH_CONNECTION}
local username
local username hostname
if [[ -z $ssh_connection ]] && (( $+commands[who] )); then
# When changing user on a remote system, the $SSH_CONNECTION
# environment variable can be lost, attempt detection via who.
Expand Down Expand Up @@ -542,11 +556,12 @@ prompt_pure_state_setup() {
unset MATCH MBEGIN MEND
fi

hostname='%F{$prompt_pure_colors[host]}@%m%f'
# show username@host if logged in through SSH
[[ -n $ssh_connection ]] && username='%F{242}%n@%m%f'
[[ -n $ssh_connection ]] && username='%F{$prompt_pure_colors[user]}%n%f'"$hostname"

# show username@host if root, with username in white
[[ $UID -eq 0 ]] && username='%F{white}%n%f%F{242}@%m%f'
[[ $UID -eq 0 ]] && username='%F{$prompt_pure_colors[user:root]}%n%f'"$hostname"

typeset -gA prompt_pure_state
prompt_pure_state=(
Expand All @@ -573,6 +588,7 @@ prompt_pure_setup() {
zmodload zsh/datetime
zmodload zsh/zle
zmodload zsh/parameter
zmodload zsh/zutil

autoload -Uz add-zsh-hook
autoload -Uz vcs_info
Expand All @@ -582,6 +598,22 @@ prompt_pure_setup() {
# to be available, it was added in Zsh 5.3.
autoload -Uz +X add-zle-hook-widget 2>/dev/null

# Set the default colors
typeset -gA prompt_pure_colors
prompt_pure_colors=(
branch 242
branch:cached red
exec_time yellow
git_arrow cyan
host 242
path blue
prompt:error red
prompt:success magenta
user 242
user:root default
virtualenv 242
)

add-zsh-hook precmd prompt_pure_precmd
add-zsh-hook preexec prompt_pure_preexec

Expand All @@ -595,10 +627,10 @@ prompt_pure_setup() {
fi

# if a virtualenv is activated, display it in grey
PROMPT='%(12V.%F{242}%12v%f .)'
PROMPT='%(12V.%F{$prompt_pure_colors[virtualenv]}%12v%f .)'

# prompt turns red if the previous command didn't exit with 0
PROMPT+='%(?.%F{magenta}.%F{red})${prompt_pure_state[prompt]}%f '
PROMPT+='%(?.%F{$prompt_pure_colors[prompt:success]}.%F{$prompt_pure_colors[prompt:error]})${prompt_pure_state[prompt]}%f '

# Store prompt expansion symbols for in-place expansion via (%). For
# some reason it does not work without storing them in a variable first.
Expand Down
34 changes: 34 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ prompt pure
| **`PURE_GIT_DOWN_ARROW`** | Defines the git down arrow symbol. | `` |
| **`PURE_GIT_UP_ARROW`** | Defines the git up arrow symbol. | `` |

## Colors

Colors can be changed by using `[zstyle](http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fzutil-Module)` with a pattern of the form `:prompt:pure:$color_name` and style `color`. The color names, their default and what part they affect are:
- `branch` (242), the name of the current branch when in a git repository
- `branch:cached` (red), the name of the current branch when the data isn't fresh
- `exec_time` (yellow), the execution time of the last command when exceeding `PURE_CMD_MAX_EXEC_TIME`
- `git_arrow` (cyan), for `PURE_GIT_UP_ARROW` and `PURE_GIT_DOWN_ARROW`
- `host` (242), the hostname when on a remote machine
- `path` (blue), the current path ie. `PWD`
- `pompt:error` (red), the `PURE_PROMPT_SYMBOL` when the previous command has *failed*
- `prompt:success` (magenta), the `PURE_PROMPT_SYMBOL` when the previous command has *succeded*
- `user` (242), the username when on remote machine
- `user:root` (default), the username when the user is root
- `virtualenv` (242), the name of the python virtualenv when in use

The following diagram shows where each colors are applied on the prompt.
``` text
path
| branch
| | git_arrow
| | | host
| | | |
~/dev/pure master* ⇡ zaphod@heartofgold 42s
venv ❯ | |
| | | exec_time
| | user
| prompt
virtualenv
```

## Example

```sh
Expand All @@ -102,6 +132,10 @@ autoload -U promptinit; promptinit

# optionally define some options
PURE_CMD_MAX_EXEC_TIME=10
# change path color
zstyle :prompt:pure:path color white
# prompt color whatever the result of the last command
zstyle ':prompt:pure:prompt:*' green

prompt pure
```
Expand Down

0 comments on commit 6570bea

Please sign in to comment.