From c653778c9eb6008460bf8af205484810ceb333ba Mon Sep 17 00:00:00 2001 From: muritane <31107191+muritane@users.noreply.github.com> Date: Mon, 6 Mar 2023 21:04:44 +0100 Subject: [PATCH] [Terminal] git colors: red for changes, yellow if need to push, green otherwise (#91) Co-authored-by: Daniel Azanov --- scripts/configuration/terminal_coloring.bash | 39 +++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/scripts/configuration/terminal_coloring.bash b/scripts/configuration/terminal_coloring.bash index 03ce66a5..1c0aaba0 100644 --- a/scripts/configuration/terminal_coloring.bash +++ b/scripts/configuration/terminal_coloring.bash @@ -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}" @@ -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