Skip to content

Commit 084368f

Browse files
committed
Added themes feature
2 parents 5e4d12b + 43a0d72 commit 084368f

11 files changed

+482
-9
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,34 @@ git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt
103103
# GIT_PROMPT_END=... # uncomment for custom prompt end sequence
104104

105105
# as last entry source the gitprompt script
106+
# GIT_PROMPT_THEME=Custom # use custom .git-prompt-colors.sh
107+
# GIT_PROMPT_THEME=Solarized # use theme optimized for solarized color scheme
106108
source ~/.bash-git-prompt/gitprompt.sh
107109
```
108110

109111
- `cd` to a git repository and test it!
110112

113+
#### Themes
114+
115+
The most settings are now stored in theme files. To select a theme, set the variable `GIT_PROMPT_THEME` to the name
116+
of the theme located in `<INSTALLDIR>/themes` without the extension `.bgptheme` like this:
117+
118+
```sh
119+
GIT_PROMPT_THEME=Solarized
120+
```
121+
122+
If you set `GIT_PROMPT_THEME` to `Custom`, then the `.git-prompt-colors.sh` in the home directory will be used.
123+
This file can now be generated with the command `git_prompt_make_custom_theme [<Name of base theme>]`. If the name of
124+
the base theme is ommitted or the theme file is not found, then the Default theme is used. If you have already a custom
125+
`.git-prompt-colors.sh` in your home directory, a error message will be shown.
126+
127+
You can display a list of available themes with `git_prompt_list_themes` (the current theme is highlighted)
128+
129+
**If you omit the `GIT_PROMPT_THEME` variable, the Default theme is used or, if you have a custom `.git-prompt-colors.sh`
130+
in your home directory, then the Custom theme is used.**
131+
132+
#### Further customizations
133+
111134
- You can define `GIT_PROMPT_START` and `GIT_PROMPT_END` to tweak your prompt.
112135

113136
- The default colors are defined within `prompt-colors.sh`, which is sourced by

git-prompt-help.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# being displayed.
44

55
git_prompt_help() {
6-
source prompt-colors.sh
7-
source git-prompt-colors.sh
6+
source ${__GIT_PROMPT_DIR}/prompt-colors.sh
7+
source themes/Default.bgptheme
88
cat <<EOF | sed 's/\\\[\\033//g' | sed 's/\\\]//g'
99
The git prompt format is [<BRANCH><TRACKING>|<LOCALSTATUS>]
1010

gitprompt.sh

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,100 @@ function git_prompt_dir()
2222
fi
2323
}
2424

25+
function echoc() {
26+
echo -e "${1}$2${ResetColor}" | sed 's/\\\]//g' | sed 's/\\\[//g'
27+
}
28+
29+
function get_theme()
30+
{
31+
local CUSTOM_THEME_FILE="${HOME}/.git-prompt-colors.sh"
32+
local DEFAULT_THEME_FILE="${__GIT_PROMPT_DIR}/themes/Default.bgptheme"
33+
34+
if [[ -z ${GIT_PROMPT_THEME} ]]; then
35+
if [[ -r $CUSTOM_THEME_FILE ]]; then
36+
GIT_PROMPT_THEME="Custom"
37+
__GIT_PROMPT_THEME_FILE=$CUSTOM_THEME_FILE
38+
else
39+
GIT_PROMPT_THEME="Default"
40+
__GIT_PROMPT_THEME_FILE=$DEFAULT_THEME_FILE
41+
fi
42+
else
43+
if [[ "${GIT_PROMPT_THEME}" = "Custom" ]]; then
44+
GIT_PROMPT_THEME="Custom"
45+
__GIT_PROMPT_THEME_FILE=$CUSTOM_THEME_FILE
46+
47+
if [[ !(-r $__GIT_PROMPT_THEME_FILE) ]]; then
48+
GIT_PROMPT_THEME="Default"
49+
__GIT_PROMPT_THEME_FILE=$DEFAULT_THEME_FILE
50+
fi
51+
else
52+
local theme=""
53+
54+
# use default theme, if theme was not found
55+
for themefile in `ls $__GIT_PROMPT_DIR/themes`; do
56+
if [[ "${themefile}" = "${GIT_PROMPT_THEME}.bgptheme" ]]; then
57+
theme=$GIT_PROMPT_THEME
58+
fi
59+
done
60+
61+
if [[ "${theme}" = "" ]]; then
62+
GIT_PROMPT_THEME="Default"
63+
fi
64+
65+
__GIT_PROMPT_THEME_FILE="${__GIT_PROMPT_DIR}/themes/${GIT_PROMPT_THEME}.bgptheme"
66+
fi
67+
fi
68+
}
69+
70+
function git_prompt_list_themes()
71+
{
72+
local oldTheme
73+
local oldThemeFile
74+
75+
git_prompt_dir
76+
get_theme
77+
78+
for themefile in `ls $__GIT_PROMPT_DIR/themes`; do
79+
local theme="$(basename $themefile .bgptheme)"
80+
81+
if [[ "${GIT_PROMPT_THEME}" = "${theme}" ]]; then
82+
echoc ${Red} "*${theme}"
83+
else
84+
echo $theme
85+
fi
86+
done
87+
88+
if [[ "${GIT_PROMPT_THEME}" = "Custom" ]]; then
89+
echoc ${Magenta} "*Custom"
90+
else
91+
echoc ${Blue} "Custom"
92+
fi
93+
}
94+
95+
function git_prompt_make_custom_theme() {
96+
if [[ -r "${HOME}/.git-prompt-colors.sh" ]]; then
97+
echoc ${Red} "You alread have created a custom theme!"
98+
else
99+
git_prompt_dir
100+
101+
local base="Default"
102+
if [[ -n $1 && -r "${__GIT_PROMPT_DIR}/themes/${1}.bgptheme" ]]; then
103+
base=$1
104+
echoc ${Green} "Using theme ${Magenta}\"${base}\"${Green} as base theme!"
105+
else
106+
echoc ${Green} "Using theme ${Magenta}\"Default\"${Green} as base theme!"
107+
fi
108+
109+
if [[ "${base}" = "Custom" ]]; then
110+
echoc ${Red} "You cannot use the custom theme as base"
111+
else
112+
echoc ${Green} "Creating new cutom theme in \"${HOME}/.git-prompt-colors.sh\""
113+
echoc ${DimYellow} "Please add ${Magenta}\"GIT_PROMPT_THEME=Custom\"${DimYellow} to your .bashrc to use this theme"
114+
cp "${__GIT_PROMPT_DIR}/themes/${base}.bgptheme" "${HOME}/.git-prompt-colors.sh"
115+
fi
116+
fi
117+
}
118+
25119
# gp_set_file_var ENVAR SOMEFILE
26120
#
27121
# If ENVAR is set, check that it's value exists as a readable file. Otherwise,
@@ -99,11 +193,8 @@ function git_prompt_config()
99193
# source the user's ~/.git-prompt-colors.sh file, or the one that should be
100194
# sitting in the same directory as this script
101195

102-
if gp_set_file_var __GIT_PROMPT_COLORS_FILE git-prompt-colors.sh ; then
103-
source "$__GIT_PROMPT_COLORS_FILE"
104-
else
105-
echo 1>&2 "Cannot find git-prompt-colors.sh!"
106-
fi
196+
get_theme
197+
source "$__GIT_PROMPT_THEME_FILE"
107198

108199
if [ $GIT_PROMPT_LAST_COMMAND_STATE = 0 ]; then
109200
LAST_COMMAND_INDICATOR="$GIT_PROMPT_COMMAND_OK";

git-prompt-colors.sh renamed to themes/Default.bgptheme

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# These are the color definitions used by gitprompt.sh
1+
# This is the default theme for gitprompt.sh
22

33
define_git_prompt_colors() {
44
Time12a="\$(date +%H:%M)"
@@ -26,7 +26,7 @@ define_git_prompt_colors() {
2626
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0
2727

2828
GIT_PROMPT_COMMAND_OK="${Green}✔" # indicator if the last command returned with an exit code of 0
29-
GIT_PROMPT_COMMAND_FAIL="${Red}" # indicator if the last command returned with an exit code of other than 0
29+
GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_" # indicator if the last command returned with an exit code of other than 0
3030

3131
# template for displaying the current virtual environment
3232
# use the placeholder _VIRTUALENV_ will be replaced with

themes/Default_NoExitState.bgptheme

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This is the default theme for gitprompt.sh
2+
# without the indicator of the last command state
3+
4+
define_git_prompt_colors() {
5+
Time12a="\$(date +%H:%M)"
6+
PathShort="\w"
7+
8+
# These are the color definitions used by gitprompt.sh
9+
GIT_PROMPT_PREFIX="[" # start of the git info string
10+
GIT_PROMPT_SUFFIX="]" # the end of the git info string
11+
GIT_PROMPT_SEPARATOR="|" # separates each item
12+
13+
GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory
14+
GIT_PROMPT_STAGED="${Red}●" # the number of staged files/directories
15+
GIT_PROMPT_CONFLICTS="${Red}✖ " # the number of files in conflict
16+
GIT_PROMPT_CHANGED="${Blue}✚ " # the number of changed files
17+
18+
GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind
19+
GIT_PROMPT_UNTRACKED="${Cyan}…" # the number of untracked files/dirs
20+
GIT_PROMPT_STASHED="${BoldBlue}⚑ " # the number of stashed files/dir
21+
GIT_PROMPT_CLEAN="${BoldGreen}✔" # a colored flag indicating a "clean" repo
22+
23+
# For the command indicator, the placeholder _LAST_COMMAND_STATE_
24+
# will be replaced with the exit code of the last command
25+
# e.g.
26+
# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0
27+
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0
28+
29+
GIT_PROMPT_COMMAND_OK="${Green}✔" # indicator if the last command returned with an exit code of 0
30+
GIT_PROMPT_COMMAND_FAIL="${Red}✘" # indicator if the last command returned with an exit code of other than 0
31+
32+
# template for displaying the current virtual environment
33+
# use the placeholder _VIRTUALENV_ will be replaced with
34+
# the name of the current virtual environment (currently CONDA and VIRTUAL_ENV)
35+
GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "
36+
37+
# _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL
38+
GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}"
39+
GIT_PROMPT_START_ROOT="${GIT_PROMPT_START_USER}"
40+
GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ "
41+
GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # "
42+
43+
# Please do not add colors to these symbols
44+
GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin"
45+
GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin"
46+
GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found
47+
}
48+
49+
if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then
50+
define_git_prompt_colors
51+
fi
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# This is the default theme for gitprompt.sh
2+
# without the indicator of the last command state
3+
# tweaked for Ubuntu terminal fonts
4+
5+
define_git_prompt_colors() {
6+
Time12a="\$(date +%H:%M)"
7+
PathShort="\w"
8+
9+
# These are the color definitions used by gitprompt.sh
10+
GIT_PROMPT_PREFIX="[" # start of the git info string
11+
GIT_PROMPT_SUFFIX="]" # the end of the git info string
12+
GIT_PROMPT_SEPARATOR="|" # separates each item
13+
14+
GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory
15+
GIT_PROMPT_STAGED="${Red}● " # the number of staged files/directories
16+
GIT_PROMPT_CONFLICTS="${Red}✖ " # the number of files in conflict
17+
GIT_PROMPT_CHANGED="${Blue}✚ " # the number of changed files
18+
19+
GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind
20+
GIT_PROMPT_UNTRACKED="${Cyan}… " # the number of untracked files/dirs
21+
GIT_PROMPT_STASHED="${BoldBlue}⚑ " # the number of stashed files/dir
22+
GIT_PROMPT_CLEAN="${BoldGreen}✔ " # a colored flag indicating a "clean" repo
23+
24+
# For the command indicator, the placeholder _LAST_COMMAND_STATE_
25+
# will be replaced with the exit code of the last command
26+
# e.g.
27+
# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0
28+
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0
29+
30+
GIT_PROMPT_COMMAND_OK="${Green}✔ " # indicator if the last command returned with an exit code of 0
31+
GIT_PROMPT_COMMAND_FAIL="${Red}✘ " # indicator if the last command returned with an exit code of other than 0
32+
33+
# template for displaying the current virtual environment
34+
# use the placeholder _VIRTUALENV_ will be replaced with
35+
# the name of the current virtual environment (currently CONDA and VIRTUAL_ENV)
36+
GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "
37+
38+
# _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL
39+
GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}"
40+
GIT_PROMPT_START_ROOT="${GIT_PROMPT_START_USER}"
41+
GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ "
42+
GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # "
43+
44+
# Please do not add colors to these symbols
45+
GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin"
46+
GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin"
47+
GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found
48+
}
49+
50+
if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then
51+
define_git_prompt_colors
52+
fi

themes/Default_Ubuntu.bgptheme

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This is the default theme for gitprompt.sh
2+
# tweaked for Ubuntu terminal fonts
3+
4+
define_git_prompt_colors() {
5+
Time12a="\$(date +%H:%M)"
6+
PathShort="\w"
7+
8+
# These are the color definitions used by gitprompt.sh
9+
GIT_PROMPT_PREFIX="[" # start of the git info string
10+
GIT_PROMPT_SUFFIX="]" # the end of the git info string
11+
GIT_PROMPT_SEPARATOR="|" # separates each item
12+
13+
GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory
14+
GIT_PROMPT_STAGED="${Red}● " # the number of staged files/directories
15+
GIT_PROMPT_CONFLICTS="${Red}✖ " # the number of files in conflict
16+
GIT_PROMPT_CHANGED="${Blue}✚ " # the number of changed files
17+
18+
GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind
19+
GIT_PROMPT_UNTRACKED="${Cyan}… " # the number of untracked files/dirs
20+
GIT_PROMPT_STASHED="${BoldBlue}⚑ " # the number of stashed files/dir
21+
GIT_PROMPT_CLEAN="${BoldGreen}✔ " # a colored flag indicating a "clean" repo
22+
23+
# For the command indicator, the placeholder _LAST_COMMAND_STATE_
24+
# will be replaced with the exit code of the last command
25+
# e.g.
26+
# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0
27+
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0
28+
29+
GIT_PROMPT_COMMAND_OK="${Green}✔ " # indicator if the last command returned with an exit code of 0
30+
GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_" # indicator if the last command returned with an exit code of other than 0
31+
32+
# template for displaying the current virtual environment
33+
# use the placeholder _VIRTUALENV_ will be replaced with
34+
# the name of the current virtual environment (currently CONDA and VIRTUAL_ENV)
35+
GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "
36+
37+
# _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL
38+
GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}${PathShort}${ResetColor}"
39+
GIT_PROMPT_START_ROOT="_LAST_COMMAND_INDICATOR_ ${GIT_PROMPT_START_USER}"
40+
GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ "
41+
GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # "
42+
43+
# Please do not add colors to these symbols
44+
GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin"
45+
GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin"
46+
GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found
47+
}
48+
49+
if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then
50+
define_git_prompt_colors
51+
fi

themes/Solarized.bgptheme

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This theme for gitprompt.sh is optimized for the "Solarized Dark" and "Solarized Light" color schemes
2+
# tweaked for Ubuntu terminal fonts
3+
4+
define_git_prompt_colors() {
5+
Time12a="\$(date +%H:%M)"
6+
PathShort="\w"
7+
8+
# These are the color definitions used by gitprompt.sh
9+
GIT_PROMPT_PREFIX="[" # start of the git info string
10+
GIT_PROMPT_SUFFIX="]" # the end of the git info string
11+
GIT_PROMPT_SEPARATOR="|" # separates each item
12+
13+
GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory
14+
GIT_PROMPT_STAGED="${Yellow}●" # the number of staged files/directories
15+
GIT_PROMPT_CONFLICTS="${Red}✖ " # the number of files in conflict
16+
GIT_PROMPT_CHANGED="${Blue}✚ " # the number of changed files
17+
18+
GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind
19+
GIT_PROMPT_UNTRACKED="${Cyan}…" # the number of untracked files/dirs
20+
GIT_PROMPT_STASHED="${BoldMagenta}⚑ " # the number of stashed files/dir
21+
GIT_PROMPT_CLEAN="${Green}✔" # a colored flag indicating a "clean" repo
22+
23+
# For the command indicator, the placeholder _LAST_COMMAND_STATE_
24+
# will be replaced with the exit code of the last command
25+
# e.g.
26+
# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0
27+
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0
28+
29+
GIT_PROMPT_COMMAND_OK="${Green}✔" # indicator if the last command returned with an exit code of 0
30+
GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_" # indicator if the last command returned with an exit code of other than 0
31+
32+
# template for displaying the current virtual environment
33+
# use the placeholder _VIRTUALENV_ will be replaced with
34+
# the name of the current virtual environment (currently CONDA and VIRTUAL_ENV)
35+
GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "
36+
37+
# _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL
38+
GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}${PathShort}${ResetColor}"
39+
GIT_PROMPT_START_ROOT="_LAST_COMMAND_INDICATOR_ ${GIT_PROMPT_START_USER}"
40+
GIT_PROMPT_END_USER=" \n${BoldBlue}${Time12a}${ResetColor} $ "
41+
GIT_PROMPT_END_ROOT=" \n${BoldBlue}${Time12a}${ResetColor} # "
42+
43+
# Please do not add colors to these symbols
44+
GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin"
45+
GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin"
46+
GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found
47+
}
48+
49+
if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then
50+
define_git_prompt_colors
51+
fi

0 commit comments

Comments
 (0)