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 Git stash support #462

Merged
merged 8 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 32 additions & 10 deletions pure.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
# \e[2K => clear everything on the current line


+vi-git-stash() {
local -a stashes
stashes=$(git stash list 2>/dev/null | wc -l)
if [[ $stashes -gt 0 ]]; then
hook_com[misc]="stash=${stashes}"
fi
}

# Turns seconds into human readable time.
# 165392 => 1d 21h 56m 32s
# https://github.com/sindresorhus/pretty-time-zsh
Expand Down Expand Up @@ -145,6 +153,10 @@ prompt_pure_preprompt_render() {
if [[ -n $prompt_pure_git_arrows ]]; then
preprompt_parts+=('%F{$prompt_pure_colors[git:arrow]}${prompt_pure_git_arrows}%f')
fi
# Git stash symbol (if opted in).
if [[ -n $prompt_pure_vcs_info[stash] ]]; then
preprompt_parts+=('%F{$prompt_pure_colors[git:stash]}${PURE_GIT_STASH_SYMBOL:-≡}%f')
fi

# Username and machine, if applicable.
[[ -n $prompt_pure_state[username] ]] && preprompt_parts+=($prompt_pure_state[username])
Expand Down Expand Up @@ -253,19 +265,24 @@ prompt_pure_async_vcs_info() {
# to be used or configured as the user pleases.
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:*' use-simple true
# Only export three message variables from `vcs_info`.
zstyle ':vcs_info:*' max-exports 3
# Export branch (%b), Git toplevel (%R), and action (rebase/cherry-pick) (%a).
zstyle ':vcs_info:git*' formats '%b' '%R'
zstyle ':vcs_info:git*' actionformats '%b' '%R' '%a'
# Only export four message variables from `vcs_info`.
zstyle ':vcs_info:*' max-exports 4
# Export branch (%b), Git toplevel (%R), action (rebase/cherry-pick) (%a),
# and stash information via misc (%m).
zstyle ':vcs_info:git*' formats '%b' '%R' '%a' '%m'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m guessing there are no repercussions from including %a here?

Copy link
Contributor Author

@carhartl carhartl Dec 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason it was producing an error when formats didn't have the same amount as actionformats. I really don't understand why these two seem to interact, maybe I did something wrong elsewhere, still new to zsh internals.

EDIT: not an error, but something that looked like an error, but was just not the expected output.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, it’s been a while since I worked with vcs_info as well so just being cautious 😄. It’s probably fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a second look... %m was only required in formats only, but not in actionformats.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. How about when a git rebase is active? That’s when action formats kick in AFAICR.

Copy link
Contributor Author

@carhartl carhartl Dec 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the actionformats are for when git is performing a merge or rebase. If I understand all this correctly, if we were omitting %m from actionformats, info[stash]=$vcs_info_msg_3_ would remain empty (?) and the stash symbol would never be shown during such an action. Which is what we want?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need to ensure that $vcs_info_msg_3_ would always point to the expected output...

zstyle ':vcs_info:git*' actionformats '%b' '%R' '%a' '%m'
if [[ $1 == 0 ]]; then
zstyle ':vcs_info:git*+set-message:*' hooks git-stash
fi

vcs_info

local -A info
info[pwd]=$PWD
info[top]=$vcs_info_msg_1_
info[branch]=$vcs_info_msg_0_
info[top]=$vcs_info_msg_1_
info[action]=$vcs_info_msg_2_
info[stash]=$vcs_info_msg_3_

print -r - ${(@kvq)info}
}
Expand Down Expand Up @@ -355,13 +372,16 @@ prompt_pure_async_tasks() {
unset prompt_pure_git_dirty
unset prompt_pure_git_last_dirty_check_timestamp
unset prompt_pure_git_arrows
unset prompt_pure_git_stash
unset prompt_pure_git_fetch_pattern
prompt_pure_vcs_info[branch]=
prompt_pure_vcs_info[top]=
prompt_pure_vcs_info[stash]=
fi
unset MATCH MBEGIN MEND

async_job "prompt_pure" prompt_pure_async_vcs_info
zstyle -t ":prompt:pure:git:stash" show
async_job "prompt_pure" prompt_pure_async_vcs_info $?

# Only perform tasks inside a Git working tree.
[[ -n $prompt_pure_vcs_info[top] ]] || return
Expand All @@ -374,14 +394,14 @@ prompt_pure_async_refresh() {

if [[ -z $prompt_pure_git_fetch_pattern ]]; then
# We set the pattern here to avoid redoing the pattern check until the
# working three has changed. Pull and fetch are always valid patterns.
# working tree has changed. Pull and fetch are always valid patterns.
typeset -g prompt_pure_git_fetch_pattern="pull|fetch"
async_job "prompt_pure" prompt_pure_async_git_aliases
fi

async_job "prompt_pure" prompt_pure_async_git_arrows

# Do not preform `git fetch` if it is disabled or in home folder.
# Do not perform `git fetch` if it is disabled or in home folder.
if (( ${PURE_GIT_PULL:-1} )) && [[ $prompt_pure_vcs_info[top] != $HOME ]]; then
# Tell the async worker to do a `git fetch`.
async_job "prompt_pure" prompt_pure_async_git_fetch
Expand Down Expand Up @@ -449,10 +469,11 @@ prompt_pure_async_callback() {
# Git directory. Run the async refresh tasks.
[[ -n $info[top] ]] && [[ -z $prompt_pure_vcs_info[top] ]] && prompt_pure_async_refresh

# Always update branch and top-level.
# Always update branch, top-level and stash.
prompt_pure_vcs_info[branch]=$info[branch]
prompt_pure_vcs_info[top]=$info[top]
prompt_pure_vcs_info[action]=$info[action]
prompt_pure_vcs_info[stash]=$info[stash]

do_render=1
;;
Expand Down Expand Up @@ -681,6 +702,7 @@ prompt_pure_setup() {
prompt_pure_colors_default=(
execution_time yellow
git:arrow cyan
git:stash cyan
git:branch 242
git:branch:cached red
git:action 242
Expand Down
17 changes: 13 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ prompt pure
| **`PURE_PROMPT_VICMD_SYMBOL`** | Defines the prompt symbol used when the `vicmd` keymap is active (VI-mode). | `❮` |
| **`PURE_GIT_DOWN_ARROW`** | Defines the git down arrow symbol. | `⇣` |
| **`PURE_GIT_UP_ARROW`** | Defines the git up arrow symbol. | `⇡` |
| **`PURE_GIT_STASH_SYMBOL`** | Defines the git stash symbol. | `≡` |
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in the comments, this should be zstyle ':prompt:pure:git:stash' icon S.


Showing git stash status as part of the prompt is not activated by default. To activate this you'll need to opt in via `zstyle`:

`zstyle :prompt:pure:git:stash show yes`

## Colors

Expand All @@ -82,6 +86,7 @@ As explained in ZSH's [manual](http://zsh.sourceforge.net/Doc/Release/Zsh-Line-E
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:
- `execution_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`.
- `git:stash` (cyan) - For `PURE_GIT_STASH_SYMBOL`.
- `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.
- `git:action` (242) - The current action in progress (cherry-pick, rebase, etc.) when in a Git repository.
Expand All @@ -100,11 +105,12 @@ The following diagram shows where each color is applied on the prompt:
┌───────────────────────────────────────────── path
│ ┌────────────────────────────────── git:branch
│ │ ┌─────────────────────────── git:action
| | | ┌─────────────────── git:dirty
┌─────────────────── git:dirty
│ │ │ │ ┌───────────────── git:arrow
│ │ │ │ │ ┌──────── host
│ │ │ │ │ │
~/dev/pure master|rebase-i* ⇡ zaphod@heartofgold 42s
│ │ │ │ │ ┌─────────────── git:stash
│ │ │ │ │ │ ┌────── host
│ │ │ │ │ │ │
~/dev/pure master|rebase-i* ⇡ ≡ zaphod@heartofgold 42s
venv ❯ │ │
│ │ │ └───── execution_time
│ │ └──────────────────────── user
Expand Down Expand Up @@ -141,6 +147,9 @@ zstyle :prompt:pure:path color white
# change the color for both `prompt:success` and `prompt:error`
zstyle ':prompt:pure:prompt:*' color cyan

# turn on git stash status
zstyle :prompt:pure:git:stash show yes

prompt pure
```

Expand Down