|
| 1 | +# copied from colcon_cd/function/colcon_cd.sh |
| 2 | + |
| 3 | +colcon_cd() { |
| 4 | + if [ $# = 0 ]; then |
| 5 | + # change the working directory to the previously saved path |
| 6 | + if [ "$_colcon_cd_root" = "" ]; then |
| 7 | + echo "No previous path saved. Either call 'colcon_cd <pkgname>' from a" \ |
| 8 | + "directory where <pkgname> can be found or 'colcon_cd --set' to" \ |
| 9 | + "directly save the current working directory." 1>&2 |
| 10 | + return 1 |
| 11 | + fi |
| 12 | + if [ "$_colcon_cd_root" != "$(pwd)" ]; then |
| 13 | + cd "$_colcon_cd_root" |
| 14 | + fi |
| 15 | + |
| 16 | + elif [ $# = 1 ]; then |
| 17 | + if [ "$1" = "--set" ]; then |
| 18 | + # store the current working directory for future invocations |
| 19 | + _colcon_cd_root="$(pwd)" |
| 20 | + echo "Saved the current working directory for future invocations of" \ |
| 21 | + "'colcon_cd <pkgname>' as well as to return to using 'colcon_cd'." \ |
| 22 | + "Call 'colcon_cd --reset' to unset the saved path." |
| 23 | + return 0 |
| 24 | + elif [ "$1" = "--reset" ]; then |
| 25 | + # unset the save path |
| 26 | + unset _colcon_cd_root |
| 27 | + return 0 |
| 28 | + fi |
| 29 | + |
| 30 | + if [ "$_colcon_cd_root" != "" ]; then |
| 31 | + # try to find the given package from the saved path |
| 32 | + _colcon_cd_pwd="$(pwd)" |
| 33 | + cd "$_colcon_cd_root" |
| 34 | + |
| 35 | + _colcon_cd_pkg_path="$(COLCON_LOG_PATH=/dev/null colcon list --packages-select $1 --paths-only 2> /dev/null)" |
| 36 | + if [ $? -eq 0 ] && [ "$_colcon_cd_pkg_path" != "" ]; then |
| 37 | + # count number of returned paths |
| 38 | + if (( $(grep -c . <<< "$_colcon_cd_pkg_path") > 1 )); then |
| 39 | + echo "Found in multiple directories:" |
| 40 | + echo "$_colcon_cd_pkg_path" |
| 41 | + _colcon_cd_pkg_path="$(grep -m 1 . <<< "$_colcon_cd_pkg_path")" |
| 42 | + echo "cd to the first one" |
| 43 | + fi |
| 44 | + cd "$_colcon_cd_pkg_path" |
| 45 | + unset _colcon_cd_pkg_path |
| 46 | + unset _colcon_cd_pwd |
| 47 | + return 0 |
| 48 | + fi |
| 49 | + unset _colcon_cd_pkg_path |
| 50 | + |
| 51 | + cd "$_colcon_cd_pwd" |
| 52 | + unset _colcon_cd_pwd |
| 53 | + fi |
| 54 | + |
| 55 | + # try to find the given package from the current working directory |
| 56 | + _colcon_cd_pkg_path="$(COLCON_LOG_PATH=/dev/null colcon list --packages-select $1 --paths-only 2> /dev/null)" |
| 57 | + if [ $? -eq 0 ] && [ "$_colcon_cd_pkg_path" != "" ]; then |
| 58 | + if [ "$_colcon_cd_root" = "" ]; then |
| 59 | + # store the current working directory for future invocations |
| 60 | + _colcon_cd_root="$(pwd)" |
| 61 | + echo "Saved the directory '$_colcon_cd_root' for future invocations" \ |
| 62 | + "of 'colcon_cd <pkgname>' as well as to return to using " \ |
| 63 | + "'colcon_cd'. Call 'colcon_cd --reset' to unset the saved path." |
| 64 | + fi |
| 65 | + # count number of returned paths |
| 66 | + if (( $(grep -c . <<< "$_colcon_cd_pkg_path") > 1 )); then |
| 67 | + echo "Found in multiple directories:" |
| 68 | + echo "$_colcon_cd_pkg_path" |
| 69 | + _colcon_cd_pkg_path="$(grep -m 1 . <<< "$_colcon_cd_pkg_path")" |
| 70 | + echo "cd to the first one" |
| 71 | + fi |
| 72 | + cd "$_colcon_cd_pkg_path" |
| 73 | + unset _colcon_cd_pkg_path |
| 74 | + return 0 |
| 75 | + fi |
| 76 | + unset _colcon_cd_pkg_path |
| 77 | + |
| 78 | + if [ "$_colcon_cd_root" != "" ]; then |
| 79 | + echo "Could neither find package '$1' from '$_colcon_cd_root' nor from" \ |
| 80 | + "the current working directory" 1>&2 |
| 81 | + else |
| 82 | + echo "Could not find package '$1' from the current working" \ |
| 83 | + "directory" 1>&2 |
| 84 | + fi |
| 85 | + return 1 |
| 86 | + |
| 87 | + else |
| 88 | + echo "'colcon_cd' only supports zero or one arguments" 1>&2 |
| 89 | + return 1 |
| 90 | + fi |
| 91 | +} |
0 commit comments