bashject: hide vars
authorBen Doumenc <bdoumenc@gmail.com>
Wed, 17 Jul 2013 17:30:03 +0000 (19:30 +0200)
committerBen Doumenc <bdoumenc@gmail.com>
Wed, 17 Jul 2013 17:30:03 +0000 (19:30 +0200)
bash/bashject

index 4f4f504..f17b4ab 100644 (file)
@@ -2,77 +2,77 @@
 # Project manager
 
 # This project manager allows you to have a quick way to switch between several different work environments
+# Source this file to enable it
 # Projects config is stored on a centralized file
 # Each project contains a local bash configuration, sourced when setting it
 #  
-
-BASHJECT_HELP_ADD="bashject a|add      name path     # Adds a project"
-BASHJECT_HELP_SET="bashject s|set      name          # Enter a project"
-BASHJECT_HELP_SOURCE="bashject source     name          # Source the project file of the specified project (useful to handle common envs)"
-BASHJECT_HELP_EDIT="bashject edit       name          # Edit the project file of the specified project with ${EDITOR}"
-BASHJECT_HELP_REMOVE="bashject rm|remove  name          # Remove the project (no filesystem modifications)"
-BASHJECT_HELP_RENAME="bashject rename     old    new    # Rename the project (no filesystem modifications)"
-BASHJECT_HELP_PATH="bashject path       name          # Echoes a project's path"
-BASHJECT_HELP_NAMES="bashject names                    # Echoes all projects, separated by whitespaces"
-BASHJECT_HELP_LIST="bashject list                     # Echoes all projects, separated by newlines (useful with dmenu, for example)"
-BASHJECT_HELP_CD="bashject cd                       # cd to the current project root path"
-BASHJECT_HELP_HELP="bashject help       [cmd]         # Display global or specific command help"
-
+# run 'bashject help' to get help
+#
 # Configuration defaults
 
-BASHJECT_PRJ_STORE_DEFAULT=${HOME}/work/.projects
-BASHJECT_PRJ_FILE_DEFAULT=".prj_env"
-BASHJECT_CHANGE_PROMPT_DEFAULT="1"
-BASHJECT_ALIAS_DEFAULT=""
-BASHJECT_TMP_DEFAULT="/tmp/bashject.tmp"
+_bj_get_config_default() {
+    local BASHJECT_PRJ_STORE_DEFAULT=${HOME}/work/.projects
+    local BASHJECT_PRJ_FILE_DEFAULT=".prj_env"
+    local BASHJECT_CHANGE_PROMPT_DEFAULT="1"
+    local BASHJECT_ALIAS_DEFAULT=""
+    local BASHJECT_TMP_DEFAULT="/tmp/bashject.tmp"
+    local VAR="BASHJECT_$1_DEFAULT"
+    echo ${!VAR}
+}
 
 # Configuration values
 
-BASHJECT_PRJ_STORE=${BASHJECT_PRJ_STORE:-${BASHJECT_PRJ_STORE_DEFAULT}}
-BASHJECT_PRJ_FILE=${BASHJECT_PRJ_FILE:-${BASHJECT_PRJ_FILE_DEFAULT}}
-BASHJECT_CHANGE_PROMPT=${BASHJECT_CHANGE_PROMPT:-${BASHJECT_CHANGE_PROMPT_DEFAULT}}
-BASHJECT_ALIAS=${BASHJECT_ALIAS:-${BASHJECT_ALIAS_DEFAULT}}
-BASHJECT_TMP=${BASHJECT_TMP:-${BASHJECT_TMP_DEFAULT}}
-
-read -d '' BASHJECT_HELP << EOF
-Usage:
-    - Add a project with 'bashject add name path'
-    - Enter a project with 'bashject set name'
-    - Customize his environment with 'bashject edit name' (or editing his ${BASHJECT_PRJ_DIR}/${BASHJECT_PRJ_FILE})
-
-Exported vars:
-    - BASHJECT_PRJ_NAME: name of the project
-    - BASHJECT_PRJ_PATH: path to the root of the project
-
-Commands:
-    - ${BASHJECT_HELP_ADD}
-    - ${BASHJECT_HELP_SET}
-    - ${BASHJECT_HELP_SOURCE}
-    - ${BASHJECT_HELP_EDIT}
-    - ${BASHJECT_HELP_REMOVE}
-    - ${BASHJECT_HELP_RENAME}
-    - ${BASHJECT_HELP_PATH}
-    - ${BASHJECT_HELP_NAMES}
-    - ${BASHJECT_HELP_LIST}
-    - ${BASHJECT_HELP_CD}
-    - ${BASHJECT_HELP_HELP}
-
-Configuration (to override values, define them before sourcing this file)
-    - BASHJECT_PRJ_STORE                # File in which we store all projects configuration  (default: '${BASHJECT_PRJ_STORE_DEFAULT}', current: '${BASHJECT_PRJ_STORE}')
-    - BASHJECT_PRJ_FILE                 # Name of the project file, created at his root and sourced on 'bashject set'  (default: '${BASHJECT_PRJ_FILE_DEFAULT}', current: '${BASHJECT_PRJ_FILE}')
-    - BASHJECT_CHANGE_PROMPT            # 1 to change the prompt to '[ BASHJECT_PRJ_NAME ] PS1' > on 'bashject set'  (default: '${BASHJECT_CHANGE_PROMPT_DEFAULT}', current: '${BASHJECT_CHANGE_PROMPT}')
-    - BASHJECT_ALIAS                    # Name of an alias for bashject to define (i.e: 'bj'), autocompletion will be handled (default: '${BASHJECT_ALIAS_DEFAULT}', current: '${BASHJECT_ALIAS}')
-    - BASHJECT_TMP                      # Temp file used for rename and remove operations (default: '${BASHJECT_TMP_DEFAULT}', current: '${BASHJECT_TMP}')
-EOF
-#
-# End of help file
-
-OLD_PS1="$PS1"
-BASHJECT_COMMANDS="help add set source remove rename edit path names list cd"
-
+BASHJECT_PRJ_STORE=${BASHJECT_PRJ_STORE:-$(_bj_get_config_default PRJ_STORE)}
+BASHJECT_PRJ_FILE=${BASHJECT_PRJ_FILE:-$(_bj_get_config_default PRJ_FILE)}
+BASHJECT_CHANGE_PROMPT=${BASHJECT_CHANGE_PROMPT:-$(_bj_get_config_default CHANGE_PROMPT)}
+BASHJECT_ALIAS=${BASHJECT_ALIAS:-$(_bj_get_config_default ALIAS)}
+BASHJECT_TMP=${BASHJECT_TMP:-$(_bj_get_config_default TMP)}
 
-# Real stuff
 function _bj_help() {
+    local BASHJECT_HELP_ADD="bashject a|add      name path     # Adds a project"
+    local BASHJECT_HELP_SET="bashject s|set      name          # Enter a project"
+    local BASHJECT_HELP_SOURCE="bashject source     name          # Source the project file of the specified project (useful to handle common envs)"
+    local BASHJECT_HELP_EDIT="bashject edit       name          # Edit the project file of the specified project with ${EDITOR}"
+    local BASHJECT_HELP_REMOVE="bashject rm|remove  name          # Remove the project (no filesystem modifications)"
+    local BASHJECT_HELP_RENAME="bashject rename     old    new    # Rename the project (no filesystem modifications)"
+    local BASHJECT_HELP_PATH="bashject path       name          # Echoes a project's path"
+    local BASHJECT_HELP_NAMES="bashject names                    # Echoes all projects, separated by whitespaces"
+    local BASHJECT_HELP_LIST="bashject list                     # Echoes all projects, separated by newlines (useful with dmenu, for example)"
+    local BASHJECT_HELP_CD="bashject cd                       # cd to the current project root path"
+    local BASHJECT_HELP_HELP="bashject help       [cmd]         # Display global or specific command help"
+    local BASHJECT_HELP=""
+
+    read -d '' BASHJECT_HELP << EOF
+    Usage:
+        - Add a project with 'bashject add name path'
+        - Enter a project with 'bashject set name'
+        - Customize his environment with 'bashject edit name' (or editing his ${BASHJECT_PRJ_DIR}/${BASHJECT_PRJ_FILE})
+
+    Exported vars:
+        - BASHJECT_PRJ_NAME: name of the project
+        - BASHJECT_PRJ_PATH: path to the root of the project
+
+    Commands:
+        - ${BASHJECT_HELP_ADD}
+        - ${BASHJECT_HELP_SET}
+        - ${BASHJECT_HELP_SOURCE}
+        - ${BASHJECT_HELP_EDIT}
+        - ${BASHJECT_HELP_REMOVE}
+        - ${BASHJECT_HELP_RENAME}
+        - ${BASHJECT_HELP_PATH}
+        - ${BASHJECT_HELP_NAMES}
+        - ${BASHJECT_HELP_LIST}
+        - ${BASHJECT_HELP_CD}
+        - ${BASHJECT_HELP_HELP}
+
+    Configuration (to override values, define them before sourcing this file)
+        - BASHJECT_PRJ_STORE                # File in which we store all projects configuration  (default: '${BASHJECT_PRJ_STORE_DEFAULT}', current: '${BASHJECT_PRJ_STORE}')
+        - BASHJECT_PRJ_FILE                 # Name of the project file, created at his root and sourced on 'bashject set'  (default: '${BASHJECT_PRJ_FILE_DEFAULT}', current: '${BASHJECT_PRJ_FILE}')
+        - BASHJECT_CHANGE_PROMPT            # 1 to change the prompt to '[ BASHJECT_PRJ_NAME ] PS1' > on 'bashject set'  (default: '${BASHJECT_CHANGE_PROMPT_DEFAULT}', current: '${BASHJECT_CHANGE_PROMPT}')
+        - BASHJECT_ALIAS                    # Name of an alias for bashject to define (i.e: 'bj'), autocompletion will be handled (default: '${BASHJECT_ALIAS_DEFAULT}', current: '${BASHJECT_ALIAS}')
+        - BASHJECT_TMP                      # Temp file used for rename and remove operations (default: '${BASHJECT_TMP_DEFAULT}', current: '${BASHJECT_TMP}')
+EOF
+
     if [ -z $1 ]; then 
         echo -e "${BASHJECT_HELP}" >&2
     else
@@ -96,6 +96,12 @@ function _bj_help() {
     fi
 }
 
+
+function _bj_get_commands() {
+    echo "help add set source remove rename edit path names list cd"
+}
+
+# Real stuff
 _bj_add_project() {
     if [ "$#" -eq "2" ]; then
         local name=$1
@@ -153,6 +159,7 @@ _bj_edit_project() {
 }
 
 _bj_set_project() {
+    local OLD_PS1="$PS1"
     # Find dir
     BASHJECT_PRJ_NAME=$1
     BASHJECT_PRJ_DIR=$(_bj_get_project_path $BASHJECT_PRJ_NAME)
@@ -238,7 +245,7 @@ function bashject {
         help)        _bj_help                  "$@" ; return $? ;;
         "") 
             echo "No command provided. Available:"
-            for cmd in ${BASHJECT_COMMANDS}; do
+            for cmd in $(_bj_get_commands); do
                 echo "  - $cmd "
             done
             return 1
@@ -267,7 +274,7 @@ _bj_complete_command()
         local cur
         _get_comp_words_by_ref cur
         COMPREPLY=()
-        COMPREPLY=($(compgen -W "${BASHJECT_COMMANDS}" -- "$cur"))
+        COMPREPLY=($(compgen -W "$(_bj_get_commands)" -- "$cur"))
         return 0
 }