Skip to content

Commit

Permalink
Merge branch 'michaeldfallen-cbandera_stash_indicator' into feature/s…
Browse files Browse the repository at this point in the history
…tash_indicator
  • Loading branch information
Claudio Bandera committed Oct 16, 2015
2 parents 740cb5b + 4cd0f26 commit 127e3e5
Show file tree
Hide file tree
Showing 10 changed files with 634 additions and 65 deletions.
70 changes: 58 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ last few years. Maybe it can help you too.
- [Files status](#files-status)
- [Local commits status](#local-commits-status)
- [Remote commits status](#remote-commits-status)
- [Stash status](#stash-status)
- [(Optional) Auto-fetch repos](#optional-auto-fetch-repos)
- [Customise your prompt](#customise-your-prompt)
- [Support](#support)
- [Ensuring prompt execution](#ensuring-prompt-execution)
- [Configuring colours](#configuring-colours)
Expand Down Expand Up @@ -120,6 +122,9 @@ Red | Unstaged, you'll need to `git add` them before you can commit
Grey | Untracked, these are new files git is unaware of
Yellow | Conflicted, these need resolved before they can be committed

The use of feature is controlled by the `GIT_RADAR_FORMAT` environment variable.
See [Customise your prompt](#customise-your-prompt) for how to personalise this.

### Local commits status

The prompt will show you the difference in commits between your branch and the
Expand All @@ -132,6 +137,9 @@ Prompt | Meaning
![git:(master 3↓)] | We have 3 commits to pull down
![git:(master 3⇵5)] | Our version and origins version of `master` have diverged

The use of feature is controlled by the `GIT_RADAR_FORMAT` environment variable.
See [Customise your prompt](#customise-your-prompt) for how to personalise this.

### Remote commits status

The prompt will also show the difference between your branch on origin and what
Expand All @@ -146,7 +154,8 @@ Prompt | Meaning
![git:(m 4 → my-branch)] | There are 4 commits on `origin/master` that aren't on `origin/my-branch`
![git:(m 1 ⇄ 2 my-branch)] | `origin/master` and `origin/my-branch` have diverged, we'll need to rebase or merge

If you don't rely on this status, you can always hide this part of the prompt by calling git-radar with `--no-remote-status`.
The use of feature is controlled by the `GIT_RADAR_FORMAT` environment variable.
See [Customise your prompt](#customise-your-prompt) for how to personalise this.

### Stash status
The prompt will show you whether and how many stashes you have stored.
Expand All @@ -155,17 +164,8 @@ Prompt | Meaning
---------------------------|---------------
![git:(master) 1≡] | We have one stash

**Bash**
```bash
export PS1="$PS1\$(git-radar --bash --fetch --no-remote-status) "
```
[(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution)

**Zsh**
```zsh
export PROMPT="$PROMPT\$(git-radar --zsh --fetch --no-remote-status) "
```
[(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution)
If you don't rely on this status, you can always hide this part of the prompt by
[customising your prompt](#customise-your-prompt)

### (Optional) Auto-fetch repos

Expand All @@ -190,6 +190,51 @@ export PROMPT="$PROMPT\$(git-radar --zsh --fetch) "
```
[(note: the `\` escaping the `$` is important)](#ensuring-prompt-execution)

## Customise your prompt

Git Radar is highly customisable using a prompt format string. The 4 features
above: remote commits, local commits, branch and file changes; are controlled
by the prompt format string.

Feature | Control string
---------------|---------------
Remote commits | `%{remote}`
Local commits | `%{local}`
Branch | `%{branch}`
File changes | `%{changes}`
Stashes | `%{stash}`

You can create any prompt shape you prefer by exporting `GIT_RADAR_FORMAT` with
your preferred shape. The control strings above will be replaced with the output
of the corresponding feature.

**Examples**

GIT_RADAR_FORMAT | Result
--------------------------------------|---------------------
`%{branch}%{local}%{changes}` | `master1↑1M`
`[%{branch}] - %{local} - %{changes}` | `[master] - 1↑ - 1M`

### Prefixing and Suffixing the features

Often you will want certain parts of the prompt to only appear when there is
content to render. For example, when in a repo you want `[branch]` but when out
of a repo you don't want the `[]` appearing.

To do this the control strings support prefixes and suffixes. Prefixes and
Suffixes are separated from the feature name by `:` and will only render if the
feature would render:

Format: `prompt > %{prefix - :changes: - suffix}`

In a repo: `prompt > prefix - 1M - suffix`

Outside a repo: `prompt > `

The default prompt format uses this to add spaces only if the feature would
render. In that way the prompt always looks well spaced out no matter how many
features are rendering.

## Support

### Ensuring prompt execution
Expand Down Expand Up @@ -493,6 +538,7 @@ Git Radar is licensed under the MIT license.
See [LICENSE] for the full license text.

[LICENSE]: https://github.com/michaeldfallen/git-radar/blob/master/LICENSE
[git:(master) 1≡]: https://raw.githubusercontent.com/michaeldfallen/git-radar/master/images/stash.png
[git:(master) 3A]: https://raw.githubusercontent.com/michaeldfallen/git-radar/master/images/untracked.png
[git:(master) 2D2M]: https://raw.githubusercontent.com/michaeldfallen/git-radar/master/images/unstaged.png
[git:(master) 1M1R]: https://raw.githubusercontent.com/michaeldfallen/git-radar/master/images/added.png
Expand Down
Binary file added images/stash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 1 addition & 9 deletions prompt.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,5 @@ source "$dot/radar-base.sh"

if is_repo; then
prepare_bash_colors
printf " \x01\033[1;30m\x02git:(\x01\033[0m\x02"
if show_remote_status $args; then
color_remote_commits
fi
readable_branch_name
color_local_commits
printf "\x01\033[1;30m\x02)\x01\033[0m\x02"
color_changes_status
stash_status
render_prompt
fi
10 changes: 1 addition & 9 deletions prompt.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,5 @@ if is_repo; then
autoload colors && colors

prepare_zsh_colors
printf '%s' "%{$fg_bold[black]%} git:(%{$reset_color%}"
if show_remote_status $args; then
color_remote_commits
fi
readable_branch_name
color_local_commits
printf '%s' "%{$fg_bold[black]%})%{$reset_color%}"
color_changes_status
stash_status
render_prompt
fi
97 changes: 85 additions & 12 deletions radar-base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ cwd=""
remote=""
rcfile_path="$HOME"

timethis() {
cmd="$@"
start=$(gdate +%s.%N)
eval $cmd
dur=$(echo "$(gdate +%s.%N) - $start" | bc)
echo "$1 - $dur" >> $HOME/duration.dat
}

prepare_bash_colors() {
if [ -f "$rcfile_path/.gitradarrc.bash" ]; then
source "$rcfile_path/.gitradarrc.bash"
Expand Down Expand Up @@ -33,6 +41,8 @@ prepare_bash_colors() {
COLOR_BRANCH="\x01${GIT_RADAR_COLOR_BRANCH:-"\\033[0m"}\x02"
MASTER_SYMBOL="${GIT_RADAR_MASTER_SYMBOL:-"\\x01\\033[0m\\x02\\xF0\\x9D\\x98\\xAE\\x01\\033[0m\\x02"}"

PROMPT_FORMAT="${GIT_RADAR_FORMAT:-" \\x01\\033[1;30m\\x02git:(\\x01\\033[0m\\x02%{remote: }%{branch}%{ :local}\\x01\\033[1;30m\\x02)\\x01\\033[0m\\x02%{ :stash}%{ :changes}"}"

RESET_COLOR_LOCAL="\x01${GIT_RADAR_COLOR_LOCAL_RESET:-"\\033[0m"}\x02"
RESET_COLOR_REMOTE="\x01${GIT_RADAR_COLOR_REMOTE_RESET:-"\\033[0m"}\x02"
RESET_COLOR_CHANGES="\x01${GIT_RADAR_COLOR_CHANGES_RESET:-"\\033[0m"}\x02"
Expand Down Expand Up @@ -71,6 +81,8 @@ prepare_zsh_colors() {
COLOR_BRANCH="%{${GIT_RADAR_COLOR_BRANCH:-$reset_color}%}"
MASTER_SYMBOL="${GIT_RADAR_MASTER_SYMBOL:-"%{$reset_color%}$italic_m%{$reset_color%}"}"

PROMPT_FORMAT="${GIT_RADAR_FORMAT:-" %{$fg_bold[grey]%}git:(%{$reset_color%}%{remote: }%{branch}%{ :local}%{$fg_bold[grey]%})%{$reset_color%}%{ :stash}%{ :changes}"}"

RESET_COLOR_LOCAL="%{${GIT_RADAR_COLOR_LOCAL_RESET:-$reset_color}%}"
RESET_COLOR_REMOTE="%{${GIT_RADAR_COLOR_REMOTE_RESET:-$reset_color}%}"
RESET_COLOR_CHANGES="%{${GIT_RADAR_COLOR_CHANGES_RESET:-$reset_color}%}"
Expand Down Expand Up @@ -397,7 +409,7 @@ color_changes_status() {

changes="$staged_changes$conflicted_changes$unstaged_changes$untracked_changes"
fi
printf $PRINT_F_OPTION "$changes"
printf $PRINT_F_OPTION "${changes:1}"
}

bash_color_changes_status() {
Expand All @@ -409,8 +421,6 @@ zsh_color_changes_status() {
}

color_local_commits() {
local separator="${1:- }"

local green_ahead_arrow="${COLOR_LOCAL_AHEAD}$RESET_COLOR_LOCAL"
local red_behind_arrow="${COLOR_LOCAL_BEHIND}$RESET_COLOR_LOCAL"
local yellow_diverged_arrow="${COLOR_LOCAL_DIVERGED}$RESET_COLOR_LOCAL"
Expand All @@ -421,11 +431,11 @@ color_local_commits() {
local_behind="$(commits_behind_of_remote "$remote_branch")"

if [[ "$local_behind" -gt "0" && "$local_ahead" -gt "0" ]]; then
local_commits="$separator$local_behind$yellow_diverged_arrow$local_ahead"
local_commits="$local_behind$yellow_diverged_arrow$local_ahead"
elif [[ "$local_behind" -gt "0" ]]; then
local_commits="$separator$local_behind$red_behind_arrow"
local_commits="$local_behind$red_behind_arrow"
elif [[ "$local_ahead" -gt "0" ]]; then
local_commits="$separator$local_ahead$green_ahead_arrow"
local_commits="$local_ahead$green_ahead_arrow"
fi
fi
printf $PRINT_F_OPTION "$local_commits"
Expand All @@ -450,14 +460,14 @@ color_remote_commits() {
remote_behind="$(remote_behind_of_master "$remote_branch")"

if [[ "$remote_behind" -gt "0" && "$remote_ahead" -gt "0" ]]; then
remote="$MASTER_SYMBOL $remote_behind $yellow_diverged_arrow $remote_ahead "
remote="$MASTER_SYMBOL $remote_behind $yellow_diverged_arrow $remote_ahead"
elif [[ "$remote_ahead" -gt "0" ]]; then
remote="$MASTER_SYMBOL $green_ahead_arrow $remote_ahead "
remote="$MASTER_SYMBOL $green_ahead_arrow $remote_ahead"
elif [[ "$remote_behind" -gt "0" ]]; then
remote="$MASTER_SYMBOL $remote_behind $red_behind_arrow "
remote="$MASTER_SYMBOL $remote_behind $red_behind_arrow"
fi
else
remote="upstream $not_upstream "
remote="upstream $not_upstream"
fi

printf $PRINT_F_OPTION "$remote"
Expand Down Expand Up @@ -493,12 +503,75 @@ show_remote_status() {
}

stashed_status() {
printf '%s' "$(git stash list | wc -l 2>/dev/null)"
printf '%s' "$(git stash list | wc -l 2>/dev/null | grep -oEi '[0-9][0-9]*')"
}

stash_status() {
local number_stashes="$(stashed_status)"
if [ $number_stashes -gt 0 ]; then
printf $PRINT_F_OPTION " $number_stashes$COLOR_STASH$RESET_COLOR_STASH"
printf $PRINT_F_OPTION "$number_stashes$COLOR_STASH$RESET_COLOR_STASH"
fi
}

render_prompt() {
output="$PROMPT_FORMAT"
branch_sed=""
remote_sed=""
local_sed=""
changes_sed=""
stash_sed=""


if_pre="%\{([^%{}]{1,}:){0,1}"
if_post="(:[^%{}]{1,}){0,1}\}"
sed_pre="%{\(\([^%^{^}]*\)\:\)\{0,1\}"
sed_post="\(\:\([^%^{^}]*\)\)\{0,1\}}"

if [[ $output =~ ${if_pre}remote${if_post} ]]; then
remote_result="$(color_remote_commits)"
if [[ -n "$remote_result" ]]; then
remote_sed="s/${sed_pre}remote${sed_post}/\2${remote_result}\4/"
else
remote_sed="s/${sed_pre}remote${sed_post}//"
fi
fi
if [[ $PROMPT_FORMAT =~ ${if_pre}branch${if_post} ]]; then
branch_result="$(readable_branch_name | sed -e 's/\//\\\//')"
if [[ -n "$branch_result" ]]; then
branch_sed="s/${sed_pre}branch${sed_post}/\2${branch_result}\4/"
else
branch_sed="s/${sed_pre}branch${sed_post}//"
fi
fi
if [[ $PROMPT_FORMAT =~ ${if_pre}local${if_post} ]]; then
local_result="$(color_local_commits)"
if [[ -n "$local_result" ]]; then
local_sed="s/${sed_pre}local${sed_post}/\2$local_result\4/"
else
local_sed="s/${sed_pre}local${sed_post}//"
fi
fi
if [[ $PROMPT_FORMAT =~ ${if_pre}changes${if_post} ]]; then
changes_result="$(color_changes_status)"
if [[ -n "$changes_result" ]]; then
changes_sed="s/${sed_pre}changes${sed_post}/\2${changes_result}\4/"
else
changes_sed="s/${sed_pre}changes${sed_post}//"
fi
fi
if [[ $PROMPT_FORMAT =~ ${if_pre}stash${if_post} ]]; then
stash_result="$(stash_status)"
if [[ -n "$stash_result" ]]; then
stash_sed="s/${sed_pre}stash${sed_post}/\2${stash_result}\4/"
else
stash_sed="s/${sed_pre}stash${sed_post}//"
fi
fi

printf '%b' "$output" | sed \
-e "$remote_sed" \
-e "$branch_sed" \
-e "$changes_sed" \
-e "$local_sed" \
-e "$stash_sed"
}
1 change: 1 addition & 0 deletions test
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
./test-status.sh
./test-stash.sh
./test-colors.sh
./test-format-config.sh
Loading

0 comments on commit 127e3e5

Please sign in to comment.