Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for configuring colors with zstyle #472

Merged
merged 9 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions pure.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,34 @@ 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
bricewge marked this conversation as resolved.
Show resolved Hide resolved
zstyle -t ":prompt:pure:$key" color "$val"
case $? in
1) # current style is diffrent than the one from zstyle
zstyle -s ":prompt:pure:$key" color color_temp
prompt_pure_colors[$key]=$color_temp ;;
2) # no style is defined
prompt_pure_colors[$key]=$prompt_pure_colors_default[$key] ;;
esac
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[git:branch]
[[ -n ${prompt_pure_git_last_dirty_check_timestamp+x} ]] && git_color=$prompt_pure_colors[git: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 +139,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 +189,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 +527,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 +560,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 default color
[[ $UID -eq 0 ]] && username='%f%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[version]="1.9.0"
Expand Down Expand Up @@ -618,6 +637,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 @@ -627,6 +647,23 @@ 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 colors
typeset -gA prompt_pure_colors_default prompt_pure_colors
prompt_pure_colors_default=(
exec_time yellow
git:arrow cyan
git:branch 242
git:branch:cached red
host 242
path blue
prompt:error red
prompt:success magenta
user 242
user:root default
virtualenv 242
)
prompt_pure_colors=("${(@fkv)prompt_pure_colors_default}")

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

Expand All @@ -640,10 +677,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
39 changes: 39 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,39 @@ 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:
bricewge marked this conversation as resolved.
Show resolved Hide resolved
- `exec_time` (yellow) - The execution time of the last command when exceeding `PURE_CMD_MAX_EXEC_TIME`
bricewge marked this conversation as resolved.
Show resolved Hide resolved
- `git:arrow` (cyan) - For `PURE_GIT_UP_ARROW` and `PURE_GIT_DOWN_ARROW`
- `git:branch` (242) - The name of the current branch when in a Git repository
- `git:branch:cached` (red) - The name of the current branch when the data isn't fresh
- `host` (242) - The hostname when on a remote machine
- `path` (blue) - The current path, for example, `PWD`
- `prompt: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
bricewge marked this conversation as resolved.
Show resolved Hide resolved
- `virtualenv` (242) - The name of the Python `virtualenv` when in use

The following diagram shows where each color is applied on the prompt:

```
path
| git:branch
| | git:arrow
| | | host
| | | |
~/dev/pure master* ⇡ zaphod@heartofgold 42s
venv ❯ | |
| | | exec_time
| | user
| prompt
virtualenv
bricewge marked this conversation as resolved.
Show resolved Hide resolved
```


## Example

```sh
Expand All @@ -103,6 +136,12 @@ autoload -U promptinit; promptinit
# optionally define some options
PURE_CMD_MAX_EXEC_TIME=10

# change the path color
zstyle :prompt:pure:path color white

# change the color for both `prompt:success` and `prompt:error`
zstyle ':prompt:pure:prompt:*' color cyan

prompt pure
```

Expand Down