Browse code

Add colorful PS4

PS4 can include functions, so when running in the LOG_COLORS=True mode
provide a grey function line so that it's easier to visually
distinguish the content from the location.

Also make it so the main prompt chunks off all the common path, which
means we can printf to 40 characters and have a pretty reasonable and
readable PS4.

Change-Id: I4a10a49db97d413349bcfceeb8c4164936fbcc40

Sean Dague authored on 2016/03/22 06:00:51
Showing 4 changed files
... ...
@@ -30,6 +30,19 @@ function function_exists {
30 30
     declare -f -F $1 > /dev/null
31 31
 }
32 32
 
33
+# short_source prints out the current location of the caller in a way
34
+# that strips redundant directories. This is useful for PS4 usage.
35
+function short_source {
36
+    saveIFS=$IFS
37
+    IFS=" "
38
+    called=($(caller 0))
39
+    IFS=$saveIFS
40
+    file=${called[2]}
41
+    file=${file#$RC_DIR/}
42
+    printf "%-40s " "$file:${called[1]}:${called[0]}"
43
+}
44
+
45
+
33 46
 # Retrieve an image from a URL and upload into Glance.
34 47
 # Uses the following variables:
35 48
 #
... ...
@@ -14,9 +14,6 @@ unset LANGUAGE
14 14
 LC_ALL=C
15 15
 export LC_ALL
16 16
 
17
-# Make tracing more educational
18
-export PS4='+ ${BASH_SOURCE:-}:${FUNCNAME[0]:-}:L${LINENO:-}:   '
19
-
20 17
 # Find the other rc files
21 18
 RC_DIR=$(cd $(dirname "${BASH_SOURCE:-$0}") && pwd)
22 19
 
... ...
@@ -132,6 +129,16 @@ elif [[ -f $RC_DIR/.localrc.auto ]]; then
132 132
     source $RC_DIR/.localrc.auto
133 133
 fi
134 134
 
135
+# Use color for logging output (only available if syslog is not used)
136
+LOG_COLOR=$(trueorfalse True LOG_COLOR)
137
+
138
+# Make tracing more educational
139
+if [[ "$LOG_COLOR" == "True" ]]; then
140
+    export PS4='+\[$(tput setaf 242)\]$(short_source)\[$(tput sgr0)\] '
141
+else
142
+    export PS4='+ $(short_source):   '
143
+fi
144
+
135 145
 # Configure Identity API version: 2.0, 3
136 146
 IDENTITY_API_VERSION=${IDENTITY_API_VERSION:-2.0}
137 147
 
... ...
@@ -785,9 +792,6 @@ SYSLOG=$(trueorfalse False SYSLOG)
785 785
 SYSLOG_HOST=${SYSLOG_HOST:-$HOST_IP}
786 786
 SYSLOG_PORT=${SYSLOG_PORT:-516}
787 787
 
788
-# Use color for logging output (only available if syslog is not used)
789
-LOG_COLOR=$(trueorfalse True LOG_COLOR)
790
-
791 788
 # Set global ``GIT_DEPTH=<number>`` to limit the history depth of the git clone
792 789
 # Set to 0 to disable shallow cloning
793 790
 GIT_DEPTH=${GIT_DEPTH:-0}
... ...
@@ -7,6 +7,22 @@
7 7
 # Warning: This script just for development purposes
8 8
 
9 9
 set -o errexit
10
+
11
+# short_source prints out the current location of the caller in a way
12
+# that strips redundant directories. This is useful for PS4
13
+# usage. Needed before we start tracing due to how we set
14
+# PS4. Normally we'd pick this up from stackrc, but that's not sourced
15
+# here.
16
+function short_source {
17
+    saveIFS=$IFS
18
+    IFS=" "
19
+    called=($(caller 0))
20
+    IFS=$saveIFS
21
+    file=${called[2]}
22
+    file=${file#$RC_DIR/}
23
+    printf "%-40s " "$file:${called[1]}:${called[0]}"
24
+}
25
+
10 26
 set -o xtrace
11 27
 
12 28
 ACCOUNT_DIR=./accrc
... ...
@@ -11,7 +11,6 @@
11 11
 # - if USE_PYTHON3=True, PYTHON3_VERSION refers to a version already installed
12 12
 
13 13
 set -o errexit
14
-set -o xtrace
15 14
 
16 15
 # Keep track of the current directory
17 16
 TOOLS_DIR=$(cd $(dirname "$0") && pwd)
... ...
@@ -23,6 +22,9 @@ cd $TOP_DIR
23 23
 # Import common functions
24 24
 source $TOP_DIR/stackrc
25 25
 
26
+# don't start tracing until after we've sourced the world
27
+set -o xtrace
28
+
26 29
 FILES=$TOP_DIR/files
27 30
 
28 31
 PIP_GET_PIP_URL=https://bootstrap.pypa.io/get-pip.py