[ros-users] rosbash: roscd magic for cleaner title/prompt

Top Page
Attachments:
Message as email
+ (text/plain)
+ rosbash.png (image/png)
Delete this message
Reply to this message
Author: User discussions
Date:  
To: User discussions
Subject: [ros-users] rosbash: roscd magic for cleaner title/prompt
Hi everyone,

My bash shell prompt and tab title normally look something like this:

"idryanov@idryanov-desktop:~/ros/some-stack/foo$ "

I like to have several tabs opened, each one for a different package
I'm working on. I tweaked my rosbash file to manipulate the bash
prompt i get when working in ROS. So every time I type in `roscd foo`
(where foo is some package or stack) the tab title is replaced by
"[foo]" and my prompt is replaced by "[foo]:$ ". Further, if I then
type in `cd src`, the prompt is replaced by "[foo\src]". This makes it
much quicker to identify the working directory for each of the
(possibly many) tabs I have open.

When I type in `roscd`, the tab title and prompt revert to their
default system behavior (whatever it was prior to typing in `roscd
foo`).

To play around with this, just replace the "function roscd" in
/ros/tools/rosbash with the code at the end of the email.

Cheers,
Ivan

###########################################
# To enter ROSCD_MODE, type in roscd name_of_package
# To exit ROSCD_MODE, type in roscd
# If ROSCD_MODE is set, replaces the default bash prompt and tab title
# with [name_of_package].

export PS1_BAK=$PS1

PROMPT_COMMAND='
if [ "$ROSCD_MODE" ]; then
  basedir="${PWD#${PWD%${ROSCD_MODE}*}}"
  export PS1="\[\e]0;[ ${ROSCD_MODE} ]\a\]\[\e[31;1m\][$basedir]:\$ \[\e[0m\]"
    else
  export PS1=$PS1_BAK
fi'


function roscd {
    local rosvals


    if [ -z $1 ]; then
      cd ${ROS_ROOT}
      unset ROSCD_MODE
      return 0
    fi


    _ros_decode_path $1 forceeval
    if [ $? != 0 ]; then
      echo "roscd: No such package '$1'"
      return 1
    elif [ -z $rosvals ]; then
      cd ${ROS_ROOT}
      return 0
    else
      cd ${rosvals[1]}${rosvals[2]}${rosvals[3]}
      export ROSCD_MODE=${rosvals[0]}
      return 0
    fi
}