Skip to content

Commit

Permalink
Added themes feature
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmonty committed Oct 4, 2014
2 parents 5e4d12b + 43a0d72 commit 084368f
Show file tree
Hide file tree
Showing 11 changed files with 482 additions and 9 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,34 @@ git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt
# GIT_PROMPT_END=... # uncomment for custom prompt end sequence

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

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

#### Themes

The most settings are now stored in theme files. To select a theme, set the variable `GIT_PROMPT_THEME` to the name
of the theme located in `<INSTALLDIR>/themes` without the extension `.bgptheme` like this:

```sh
GIT_PROMPT_THEME=Solarized
```

If you set `GIT_PROMPT_THEME` to `Custom`, then the `.git-prompt-colors.sh` in the home directory will be used.
This file can now be generated with the command `git_prompt_make_custom_theme [<Name of base theme>]`. If the name of
the base theme is ommitted or the theme file is not found, then the Default theme is used. If you have already a custom
`.git-prompt-colors.sh` in your home directory, a error message will be shown.

You can display a list of available themes with `git_prompt_list_themes` (the current theme is highlighted)

**If you omit the `GIT_PROMPT_THEME` variable, the Default theme is used or, if you have a custom `.git-prompt-colors.sh`
in your home directory, then the Custom theme is used.**

#### Further customizations

- You can define `GIT_PROMPT_START` and `GIT_PROMPT_END` to tweak your prompt.

- The default colors are defined within `prompt-colors.sh`, which is sourced by
Expand Down
4 changes: 2 additions & 2 deletions git-prompt-help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# being displayed.

git_prompt_help() {
source prompt-colors.sh
source git-prompt-colors.sh
source ${__GIT_PROMPT_DIR}/prompt-colors.sh
source themes/Default.bgptheme
cat <<EOF | sed 's/\\\[\\033//g' | sed 's/\\\]//g'
The git prompt format is [<BRANCH><TRACKING>|<LOCALSTATUS>]
Expand Down
101 changes: 96 additions & 5 deletions gitprompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,100 @@ function git_prompt_dir()
fi
}

function echoc() {
echo -e "${1}$2${ResetColor}" | sed 's/\\\]//g' | sed 's/\\\[//g'
}

function get_theme()
{
local CUSTOM_THEME_FILE="${HOME}/.git-prompt-colors.sh"
local DEFAULT_THEME_FILE="${__GIT_PROMPT_DIR}/themes/Default.bgptheme"

if [[ -z ${GIT_PROMPT_THEME} ]]; then
if [[ -r $CUSTOM_THEME_FILE ]]; then
GIT_PROMPT_THEME="Custom"
__GIT_PROMPT_THEME_FILE=$CUSTOM_THEME_FILE
else
GIT_PROMPT_THEME="Default"
__GIT_PROMPT_THEME_FILE=$DEFAULT_THEME_FILE
fi
else
if [[ "${GIT_PROMPT_THEME}" = "Custom" ]]; then
GIT_PROMPT_THEME="Custom"
__GIT_PROMPT_THEME_FILE=$CUSTOM_THEME_FILE

if [[ !(-r $__GIT_PROMPT_THEME_FILE) ]]; then
GIT_PROMPT_THEME="Default"
__GIT_PROMPT_THEME_FILE=$DEFAULT_THEME_FILE
fi
else
local theme=""

# use default theme, if theme was not found
for themefile in `ls $__GIT_PROMPT_DIR/themes`; do
if [[ "${themefile}" = "${GIT_PROMPT_THEME}.bgptheme" ]]; then
theme=$GIT_PROMPT_THEME
fi
done

if [[ "${theme}" = "" ]]; then
GIT_PROMPT_THEME="Default"
fi

__GIT_PROMPT_THEME_FILE="${__GIT_PROMPT_DIR}/themes/${GIT_PROMPT_THEME}.bgptheme"
fi
fi
}

function git_prompt_list_themes()
{
local oldTheme
local oldThemeFile

git_prompt_dir
get_theme

for themefile in `ls $__GIT_PROMPT_DIR/themes`; do
local theme="$(basename $themefile .bgptheme)"

if [[ "${GIT_PROMPT_THEME}" = "${theme}" ]]; then
echoc ${Red} "*${theme}"
else
echo $theme
fi
done

if [[ "${GIT_PROMPT_THEME}" = "Custom" ]]; then
echoc ${Magenta} "*Custom"
else
echoc ${Blue} "Custom"
fi
}

function git_prompt_make_custom_theme() {
if [[ -r "${HOME}/.git-prompt-colors.sh" ]]; then
echoc ${Red} "You alread have created a custom theme!"
else
git_prompt_dir

local base="Default"
if [[ -n $1 && -r "${__GIT_PROMPT_DIR}/themes/${1}.bgptheme" ]]; then
base=$1
echoc ${Green} "Using theme ${Magenta}\"${base}\"${Green} as base theme!"
else
echoc ${Green} "Using theme ${Magenta}\"Default\"${Green} as base theme!"
fi

if [[ "${base}" = "Custom" ]]; then
echoc ${Red} "You cannot use the custom theme as base"
else
echoc ${Green} "Creating new cutom theme in \"${HOME}/.git-prompt-colors.sh\""
echoc ${DimYellow} "Please add ${Magenta}\"GIT_PROMPT_THEME=Custom\"${DimYellow} to your .bashrc to use this theme"
cp "${__GIT_PROMPT_DIR}/themes/${base}.bgptheme" "${HOME}/.git-prompt-colors.sh"
fi
fi
}

# gp_set_file_var ENVAR SOMEFILE
#
# If ENVAR is set, check that it's value exists as a readable file. Otherwise,
Expand Down Expand Up @@ -99,11 +193,8 @@ function git_prompt_config()
# source the user's ~/.git-prompt-colors.sh file, or the one that should be
# sitting in the same directory as this script

if gp_set_file_var __GIT_PROMPT_COLORS_FILE git-prompt-colors.sh ; then
source "$__GIT_PROMPT_COLORS_FILE"
else
echo 1>&2 "Cannot find git-prompt-colors.sh!"
fi
get_theme
source "$__GIT_PROMPT_THEME_FILE"

if [ $GIT_PROMPT_LAST_COMMAND_STATE = 0 ]; then
LAST_COMMAND_INDICATOR="$GIT_PROMPT_COMMAND_OK";
Expand Down
4 changes: 2 additions & 2 deletions git-prompt-colors.sh → themes/Default.bgptheme
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# These are the color definitions used by gitprompt.sh
# This is the default theme for gitprompt.sh

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

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

# template for displaying the current virtual environment
# use the placeholder _VIRTUALENV_ will be replaced with
Expand Down
51 changes: 51 additions & 0 deletions themes/Default_NoExitState.bgptheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This is the default theme for gitprompt.sh
# without the indicator of the last command state

define_git_prompt_colors() {
Time12a="\$(date +%H:%M)"
PathShort="\w"

# These are the color definitions used by gitprompt.sh
GIT_PROMPT_PREFIX="[" # start of the git info string
GIT_PROMPT_SUFFIX="]" # the end of the git info string
GIT_PROMPT_SEPARATOR="|" # separates each item

GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory
GIT_PROMPT_STAGED="${Red}●" # the number of staged files/directories
GIT_PROMPT_CONFLICTS="${Red}✖ " # the number of files in conflict
GIT_PROMPT_CHANGED="${Blue}✚ " # the number of changed files

GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind
GIT_PROMPT_UNTRACKED="${Cyan}…" # the number of untracked files/dirs
GIT_PROMPT_STASHED="${BoldBlue}⚑ " # the number of stashed files/dir
GIT_PROMPT_CLEAN="${BoldGreen}✔" # a colored flag indicating a "clean" repo

# For the command indicator, the placeholder _LAST_COMMAND_STATE_
# will be replaced with the exit code of the last command
# e.g.
# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0

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

# template for displaying the current virtual environment
# use the placeholder _VIRTUALENV_ will be replaced with
# the name of the current virtual environment (currently CONDA and VIRTUAL_ENV)
GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "

# _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL
GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}"
GIT_PROMPT_START_ROOT="${GIT_PROMPT_START_USER}"
GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ "
GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # "

# Please do not add colors to these symbols
GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin"
GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin"
GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found
}

if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then
define_git_prompt_colors
fi
52 changes: 52 additions & 0 deletions themes/Default_NoExitState_Ubuntu.bgptheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This is the default theme for gitprompt.sh
# without the indicator of the last command state
# tweaked for Ubuntu terminal fonts

define_git_prompt_colors() {
Time12a="\$(date +%H:%M)"
PathShort="\w"

# These are the color definitions used by gitprompt.sh
GIT_PROMPT_PREFIX="[" # start of the git info string
GIT_PROMPT_SUFFIX="]" # the end of the git info string
GIT_PROMPT_SEPARATOR="|" # separates each item

GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory
GIT_PROMPT_STAGED="${Red}● " # the number of staged files/directories
GIT_PROMPT_CONFLICTS="${Red}✖ " # the number of files in conflict
GIT_PROMPT_CHANGED="${Blue}✚ " # the number of changed files

GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind
GIT_PROMPT_UNTRACKED="${Cyan}… " # the number of untracked files/dirs
GIT_PROMPT_STASHED="${BoldBlue}⚑ " # the number of stashed files/dir
GIT_PROMPT_CLEAN="${BoldGreen}✔ " # a colored flag indicating a "clean" repo

# For the command indicator, the placeholder _LAST_COMMAND_STATE_
# will be replaced with the exit code of the last command
# e.g.
# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0

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

# template for displaying the current virtual environment
# use the placeholder _VIRTUALENV_ will be replaced with
# the name of the current virtual environment (currently CONDA and VIRTUAL_ENV)
GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "

# _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL
GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}"
GIT_PROMPT_START_ROOT="${GIT_PROMPT_START_USER}"
GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ "
GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # "

# Please do not add colors to these symbols
GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin"
GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin"
GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found
}

if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then
define_git_prompt_colors
fi
51 changes: 51 additions & 0 deletions themes/Default_Ubuntu.bgptheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This is the default theme for gitprompt.sh
# tweaked for Ubuntu terminal fonts

define_git_prompt_colors() {
Time12a="\$(date +%H:%M)"
PathShort="\w"

# These are the color definitions used by gitprompt.sh
GIT_PROMPT_PREFIX="[" # start of the git info string
GIT_PROMPT_SUFFIX="]" # the end of the git info string
GIT_PROMPT_SEPARATOR="|" # separates each item

GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory
GIT_PROMPT_STAGED="${Red}● " # the number of staged files/directories
GIT_PROMPT_CONFLICTS="${Red}✖ " # the number of files in conflict
GIT_PROMPT_CHANGED="${Blue}✚ " # the number of changed files

GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind
GIT_PROMPT_UNTRACKED="${Cyan}… " # the number of untracked files/dirs
GIT_PROMPT_STASHED="${BoldBlue}⚑ " # the number of stashed files/dir
GIT_PROMPT_CLEAN="${BoldGreen}✔ " # a colored flag indicating a "clean" repo

# For the command indicator, the placeholder _LAST_COMMAND_STATE_
# will be replaced with the exit code of the last command
# e.g.
# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0

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

# template for displaying the current virtual environment
# use the placeholder _VIRTUALENV_ will be replaced with
# the name of the current virtual environment (currently CONDA and VIRTUAL_ENV)
GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "

# _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL
GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}${PathShort}${ResetColor}"
GIT_PROMPT_START_ROOT="_LAST_COMMAND_INDICATOR_ ${GIT_PROMPT_START_USER}"
GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ "
GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # "

# Please do not add colors to these symbols
GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin"
GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin"
GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found
}

if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then
define_git_prompt_colors
fi
51 changes: 51 additions & 0 deletions themes/Solarized.bgptheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This theme for gitprompt.sh is optimized for the "Solarized Dark" and "Solarized Light" color schemes
# tweaked for Ubuntu terminal fonts

define_git_prompt_colors() {
Time12a="\$(date +%H:%M)"
PathShort="\w"

# These are the color definitions used by gitprompt.sh
GIT_PROMPT_PREFIX="[" # start of the git info string
GIT_PROMPT_SUFFIX="]" # the end of the git info string
GIT_PROMPT_SEPARATOR="|" # separates each item

GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory
GIT_PROMPT_STAGED="${Yellow}●" # the number of staged files/directories
GIT_PROMPT_CONFLICTS="${Red}✖ " # the number of files in conflict
GIT_PROMPT_CHANGED="${Blue}✚ " # the number of changed files

GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind
GIT_PROMPT_UNTRACKED="${Cyan}…" # the number of untracked files/dirs
GIT_PROMPT_STASHED="${BoldMagenta}⚑ " # the number of stashed files/dir
GIT_PROMPT_CLEAN="${Green}✔" # a colored flag indicating a "clean" repo

# For the command indicator, the placeholder _LAST_COMMAND_STATE_
# will be replaced with the exit code of the last command
# e.g.
# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0

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

# template for displaying the current virtual environment
# use the placeholder _VIRTUALENV_ will be replaced with
# the name of the current virtual environment (currently CONDA and VIRTUAL_ENV)
GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) "

# _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL
GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}${PathShort}${ResetColor}"
GIT_PROMPT_START_ROOT="_LAST_COMMAND_INDICATOR_ ${GIT_PROMPT_START_USER}"
GIT_PROMPT_END_USER=" \n${BoldBlue}${Time12a}${ResetColor} $ "
GIT_PROMPT_END_ROOT=" \n${BoldBlue}${Time12a}${ResetColor} # "

# Please do not add colors to these symbols
GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin"
GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin"
GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found
}

if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then
define_git_prompt_colors
fi
Loading

0 comments on commit 084368f

Please sign in to comment.