Skip to content

Commit

Permalink
[Terminal] git colors: red for changes, yellow if need to push, green…
Browse files Browse the repository at this point in the history
… otherwise (StoglRobotics#91)

Co-authored-by: Daniel Azanov <[email protected]>
  • Loading branch information
muritane and Daniel Azanov authored Mar 6, 2023
1 parent 9d884e3 commit c653778
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions scripts/configuration/terminal_coloring.bash
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,39 @@ export TERMINAL_BG_COLOR_WHITE='\e[1;47m'
if [ -n "$SSH_CLIENT" ]; then text="-ssh-session"
fi

function get_gitbranch {
echo `git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
}

function parse_git_bracket {
if [[ "$(get_gitbranch)" != '' ]]; then
echo "<"
fi
}

function set_git_color {
if [[ "$(get_gitbranch)" != '' ]]; then
# Get the status of the repo and chose a color accordingly
local STATUS=`LANG=en_GB git status 2>&1`
if [[ "$STATUS" != *'working tree clean'* ]]; then
# red if need to commit
color=${TERMINAL_COLOR_RED}
else
if [[ "$STATUS" == *'Your branch is ahead'* ]]; then
# yellow if need to push
color=${TERMINAL_COLOR_YELLOW}
else
# else green
color=${TERMINAL_COLOR_GREEN}
fi
fi

echo -e "${color}"
fi
}

function parse_git_branch_and_add_brackets {
gitbranch=`git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
gitbranch="$(get_gitbranch)"

if [[ "$gitbranch" != '' ]]; then
echo "<${gitbranch}"
Expand Down Expand Up @@ -91,7 +122,11 @@ function parse_ros_workspace {
# Version mit time infront of values
# export PS1="\[\e]0;"'$(parse_ros_workspace)'"\a\]\[${TERMINAL_COLOR_LIGHT_GRAY}\]"'[\t]\['"\[${TERMINAL_COLOR_LIGHT_GREEN}\]"'\u\['"\[${TERMINAL_COLOR_LIGHT_GRAY}\]"'@\['"\[${TERMINAL_COLOR_BROWN}\]"'\h\['"\[${TERMINAL_COLOR_YELLOW}\]"'${text}\['"\[${TERMINAL_COLOR_LIGHT_GRAY}\]"':'"\["'$(set_ros_workspace_color)'"\]"'$(parse_ros_workspace)\['"\[${TERMINAL_COLOR_GREEN}\]"'$(parse_git_branch_and_add_brackets)>\['"\[${TERMINAL_COLOR_LIGHT_PURPLE}\]"'\W\['"\[${TERMINAL_COLOR_LIGHT_PURPLE}\]"'$\['"\[${TERMINAL_COLOR_NC}\]"'\[\e[m\] '

export PS1="\[\e]0;"'$(parse_ros_workspace)'"\a\]\[${TERMINAL_COLOR_LIGHT_GREEN}\]"'\u\['"\[${TERMINAL_COLOR_LIGHT_GRAY}\]"'@\['"\[${TERMINAL_COLOR_BROWN}\]"'\h\['"\[${TERMINAL_COLOR_YELLOW}\]"'${text}\['"\[${TERMINAL_COLOR_LIGHT_GRAY}\]"':'"\["'$(set_ros_workspace_color)'"\]"'$(parse_ros_workspace)\['"\[${TERMINAL_COLOR_GREEN}\]"'$(parse_git_branch_and_add_brackets)>\['"\[${TERMINAL_COLOR_LIGHT_PURPLE}\]"'\W\['"\[${TERMINAL_COLOR_LIGHT_PURPLE}\]"'$\['"\[${TERMINAL_COLOR_NC}\]"'\[\e[m\] '
# Version without git color
# export PS1="\[\e]0;"'$(parse_ros_workspace)'"\a\]\[${TERMINAL_COLOR_LIGHT_GREEN}\]"'\u\['"\[${TERMINAL_COLOR_LIGHT_GRAY}\]"'@\['"\[${TERMINAL_COLOR_BROWN}\]"'\h\['"\[${TERMINAL_COLOR_YELLOW}\]"'${text}\['"\[${TERMINAL_COLOR_LIGHT_GRAY}\]"':'"\["'$(set_ros_workspace_color)'"\]"'$(parse_ros_workspace)\['"\[${TERMINAL_COLOR_GREEN}\]"'$(parse_git_branch_and_add_brackets)>\['"\[${TERMINAL_COLOR_LIGHT_PURPLE}\]"'\W\['"\[${TERMINAL_COLOR_LIGHT_PURPLE}\]"'$\['"\[${TERMINAL_COLOR_NC}\]"'\[\e[m\] '

# Version with git color
export PS1="\[\e]0;"'$(parse_ros_workspace)'"\a\]\[${TERMINAL_COLOR_LIGHT_GREEN}\]"'\u\['"\[${TERMINAL_COLOR_LIGHT_GRAY}\]"'@\['"\[${TERMINAL_COLOR_BROWN}\]"'\h\['"\[${TERMINAL_COLOR_YELLOW}\]"'${text}\['"\[${TERMINAL_COLOR_LIGHT_GRAY}\]"':'"\["'$(set_ros_workspace_color)'"\]"'$(parse_ros_workspace)\['"\[${TERMINAL_COLOR_GREEN}\]"'$(parse_git_bracket)'"\["'$(set_git_color)'"\]"'$(get_gitbranch)'"\[${TERMINAL_COLOR_GREEN}\]"'>'"\[${TERMINAL_COLOR_LIGHT_PURPLE}\]"'\W\['"\[${TERMINAL_COLOR_LIGHT_PURPLE}\]"'$\['"\[${TERMINAL_COLOR_NC}\]"'\[\e[m\] '


# END: Stogl Robotics custom setup for nice colors and showing ROS workspace

0 comments on commit c653778

Please sign in to comment.