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

Customize colors in bash #127

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ If you prefer to keep oh-my-git repository in a different directory, just modify

source /wherever-you-want/oh-my-git/prompt.sh

## Change colors in bash

You can easily change theme colors in bash. For example to change the red background to green just put OMG_THEME variable with overridden colors into ~/.bashrc before including prompt.sh:

declare -A OMG_THEME=(["right_side_bg"]='green')
source $HOME/oh-my-git/prompt.sh

**List of color variables**

| OMG_THEME key | default value |
| -------------- | ------------- |
|left_side_color | black |
|left_side_bg | white |
|left_icon_color | red |
|stash_color | yellow |
|right_side_color| black |
|right_side_bg | red |
|right_icon_color| white |
|default_bg | black |

**Possible colors:** "black", "red", "green", "yellow", "blue", "purple", "cyan" and "white".


## zsh

With antigen installed, just add
Expand Down
132 changes: 74 additions & 58 deletions prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,42 @@ if [ -n "${BASH_VERSION}" ]; then
PROMPT='$(build_prompt)'
RPROMPT='%{$reset_color%}%T %{$fg_bold[white]%} %n@%m%{$reset_color%}'

declare -A colors=(
["black"]=30
["red"]=31
["green"]=32
["yellow"]=33
["blue"]=34
["purple"]=35
["cyan"]=36
["white"]=37
)

declare -A backgrounds=(
["black"]=40
["red"]=41
["green"]=42
["yellow"]=43
["blue"]=44
["purple"]=45
["cyan"]=46
["white"]=47
)

if [ ${#OMG_THEME[@]} -eq 0 ]; then
declare -A OMG_THEME
fi

#Assign default theme colors if they are not already defined in ~/.bashrc
[ ! ${OMG_THEME["left_side_color"]+abc} ] && OMG_THEME["left_side_color"]="black"
[ ! ${OMG_THEME["left_side_bg"]+abc} ] && OMG_THEME["left_side_bg"]="white"
[ ! ${OMG_THEME["left_icon_color"]+abc} ] && OMG_THEME["left_icon_color"]="red"
[ ! ${OMG_THEME["stash_color"]+abc} ] && OMG_THEME["stash_color"]="yellow"
[ ! ${OMG_THEME["right_side_color"]+abc} ] && OMG_THEME["right_side_color"]="black"
[ ! ${OMG_THEME["right_side_bg"]+abc} ] && OMG_THEME["right_side_bg"]="red"
[ ! ${OMG_THEME["right_icon_color"]+abc} ] && OMG_THEME["right_icon_color"]="white"
[ ! ${OMG_THEME["default_bg"]+abc} ] && OMG_THEME["default_bg"]="black"

function enrich_append {
local flag=$1
local symbol=$2
Expand All @@ -43,6 +79,13 @@ if [ -n "${BASH_VERSION}" ]; then
echo -n "${color}${symbol} "
}

function get_color {
local color=${colors[$1]}
local background=${backgrounds[$2]}
local attr=${3:-0}
echo "\e[${attr};${color};${background}m"
}

function custom_build_prompt {
local enabled=${1}
local current_commit_hash=${2}
Expand Down Expand Up @@ -71,70 +114,44 @@ if [ -n "${BASH_VERSION}" ]; then
local prompt=""
local original_prompt=$PS1


# foreground
local black='\e[0;30m'
local red='\e[0;31m'
local green='\e[0;32m'
local yellow='\e[0;33m'
local blue='\e[0;34m'
local purple='\e[0;35m'
local cyan='\e[0;36m'
local white='\e[0;37m'

#background
local background_black='\e[40m'
local background_red='\e[41m'
local background_green='\e[42m'
local background_yellow='\e[43m'
local background_blue='\e[44m'
local background_purple='\e[45m'
local background_cyan='\e[46m'
local background_white='\e[47m'

local reset='\e[0m' # Text Reset]'

local black_on_white="${black}${background_white}"
local yellow_on_white="${yellow}${background_white}"
local red_on_white="${red}${background_white}"
local red_on_black="${red}${background_black}"
local black_on_red="${black}${background_red}"
local white_on_red="${white}${background_red}"
local yellow_on_red="${yellow}${background_red}"


# Flags
local omg_default_color_on="${black_on_white}"
#Theme Variables: Text Color + Background
local left_side=$(get_color ${OMG_THEME["left_side_color"]} ${OMG_THEME["left_side_bg"]})
local left_icon=$(get_color ${OMG_THEME["left_icon_color"]} ${OMG_THEME["left_side_bg"]})
local stash=$(get_color ${OMG_THEME["stash_color"]} ${OMG_THEME["left_side_bg"]})
local right_side=$(get_color ${OMG_THEME["right_side_color"]} ${OMG_THEME["right_side_bg"]})
local right_icon=$(get_color ${OMG_THEME["right_icon_color"]} ${OMG_THEME["right_side_bg"]})
local omg_last_symbol_color=$(get_color ${OMG_THEME["right_side_bg"]} ${OMG_THEME["default_bg"]})

if [[ $is_a_git_repo == true ]]; then
# on filesystem
prompt="${black_on_white} "
prompt+=$(enrich_append $is_a_git_repo $omg_is_a_git_repo_symbol "${black_on_white}")
prompt+=$(enrich_append $has_stashes $omg_has_stashes_symbol "${yellow_on_white}")
prompt="${left_side}"
prompt+=$(enrich_append $is_a_git_repo $omg_is_a_git_repo_symbol "${left_side}")
prompt+=$(enrich_append $has_stashes $omg_has_stashes_symbol "${stash}")

prompt+=$(enrich_append $has_untracked_files $omg_has_untracked_files_symbol "${red_on_white}")
prompt+=$(enrich_append $has_modifications $omg_has_modifications_symbol "${red_on_white}")
prompt+=$(enrich_append $has_deletions $omg_has_deletions_symbol "${red_on_white}")

prompt+=$(enrich_append $has_untracked_files $omg_has_untracked_files_symbol "${left_icon}")
prompt+=$(enrich_append $has_modifications $omg_has_modifications_symbol "${left_icon}")
prompt+=$(enrich_append $has_deletions $omg_has_deletions_symbol "${left_icon}")

# ready
prompt+=$(enrich_append $has_adds $omg_has_adds_symbol "${black_on_white}")
prompt+=$(enrich_append $has_modifications_cached $omg_has_cached_modifications_symbol "${black_on_white}")
prompt+=$(enrich_append $has_deletions_cached $omg_has_cached_deletions_symbol "${black_on_white}")
prompt+=$(enrich_append $has_adds $omg_has_adds_symbol "${left_side}")
prompt+=$(enrich_append $has_modifications_cached $omg_has_cached_modifications_symbol "${left_side}")
prompt+=$(enrich_append $has_deletions_cached $omg_has_cached_deletions_symbol "${left_side}")

# next operation

prompt+=$(enrich_append $ready_to_commit $omg_ready_to_commit_symbol "${red_on_white}")
prompt+=$(enrich_append $ready_to_commit $omg_ready_to_commit_symbol "${left_icon}")

# where

prompt="${prompt} ${white_on_red} ${black_on_red}"
prompt="${prompt} ${right_icon} ${right_side}"
if [[ $detached == true ]]; then
prompt+=$(enrich_append $detached $omg_detached_symbol "${white_on_red}")
prompt+=$(enrich_append $detached "(${current_commit_hash:0:7})" "${black_on_red}")
else
prompt+=$(enrich_append $detached $omg_detached_symbol "${right_icon}")
prompt+=$(enrich_append $detached "(${current_commit_hash:0:7})" "${right_side}")
else
if [[ $has_upstream == false ]]; then
prompt+=$(enrich_append true "-- ${omg_not_tracked_branch_symbol} -- (${current_branch})" "${black_on_red}")
prompt+=$(enrich_append true "-- ${omg_not_tracked_branch_symbol} -- (${current_branch})" "${right_side}")
else
if [[ $will_rebase == true ]]; then
local type_of_upstream=$omg_rebase_tracking_branch_symbol
Expand All @@ -143,35 +160,34 @@ if [ -n "${BASH_VERSION}" ]; then
fi

if [[ $has_diverged == true ]]; then
prompt+=$(enrich_append true "-${commits_behind} ${omg_has_diverged_symbol} +${commits_ahead}" "${white_on_red}")
prompt+=$(enrich_append true "-${commits_behind} ${omg_has_diverged_symbol} +${commits_ahead}" "${right_icon}")
else
if [[ $commits_behind -gt 0 ]]; then
prompt+=$(enrich_append true "-${commits_behind} ${white_on_red}${omg_can_fast_forward_symbol}${black_on_red} --" "${black_on_red}")
prompt+="${right_side}-${commits_behind} ${right_icon}${omg_can_fast_forward_symbol}${right_side} --"
fi
if [[ $commits_ahead -gt 0 ]]; then
prompt+=$(enrich_append true "-- ${white_on_red}${omg_should_push_symbol}${black_on_red} +${commits_ahead}" "${black_on_red}")
prompt+="${right_side}-- ${right_icon}${omg_should_push_symbol}${right_side} +${commits_ahead}"
fi
if [[ $commits_ahead == 0 && $commits_behind == 0 ]]; then
prompt+=$(enrich_append true " -- -- " "${black_on_red}")
prompt+="${right_side} -- -- "
fi

fi
prompt+=$(enrich_append true "(${current_branch} ${type_of_upstream} ${upstream//\/$current_branch/})" "${black_on_red}")
prompt+="${right_side}(${current_branch} ${type_of_upstream} ${upstream//\/$current_branch/})"
fi
fi
prompt+=$(enrich_append ${is_on_a_tag} "${omg_is_on_a_tag_symbol} ${tag_at_current_commit}" "${black_on_red}")
prompt+=$(enrich_append ${is_on_a_tag} "${omg_is_on_a_tag_symbol} ${tag_at_current_commit}" "${right_side}")
prompt+="${omg_last_symbol_color}${reset}\n"
prompt+="$(eval_prompt_callback_if_present)"
prompt+="${omg_second_line}"
else
prompt+="$(eval_prompt_callback_if_present)"
prompt+="${omg_ungit_prompt}"
fi

echo "${prompt}"
}

PS2="${yellow}→${reset} "
PS2="\e[0;33m→${reset} "

function bash_prompt() {
PS1="$(build_prompt)"
Expand Down