Browse Source

Refactor title and "lastrv" setting

This likely breaks hostname setting when accessing via SSH, but:

 *  it's first version that is at least somehow readable,

 *  and also WE *MAY* have fixed the long-standing PITA escaping
    errors that we used to get in urxvt if BASH_COMMAND had backslashes
    in it!
Alois Mahdal 9 years ago
parent
commit
b3a226a0a7
1 changed files with 49 additions and 30 deletions
  1. 49
    30
      dotfiles/bash/post.bashrc

+ 49
- 30
dotfiles/bash/post.bashrc View File

@@ -49,44 +49,63 @@ export PS1=$(__bashum__mkps1)
49 49
 export PS2=$(__bashum__mkps2)
50 50
 
51 51
 
52
-__bashum__setup_prompt_command() {
52
+__bashum__set_title() {
53
+    #
54
+    # Show the currently running command in the terminal title:
55
+    #
56
+    # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
57
+    #
58
+    case "$BASH_COMMAND" in
59
+        *\033]0*)
60
+            # The command is trying to set the title bar as well;
61
+            # this is most likely the execution of $PROMPT_COMMAND.
62
+            # In any case nested escapes confuse the terminal, so don't
63
+            # output them.
64
+            ;;
65
+        "")
66
+            echo -ne "\033]0;"
67
+            echo -n    "$(__bashum__lastrv)$(__bashum__mkhostid)${PWD/$HOME/\~}\$"
68
+            echo -ne "\007"
69
+            ;;
70
+        *)
71
+            echo -ne "\033]0;"
72
+            echo -n    "${BASH_COMMAND} ($(__bashum__mkhostid)${PWD/$HOME/\~})"
73
+            echo -ne "\007"
74
+            ;;
75
+    esac
76
+}
53 77
 
78
+__bashum__mkpc() {
79
+    #
80
+    # Compose PROMPT_COMMAND body
81
+    #
82
+    echo -n '__bashum__save_rv $?;'
54 83
     case "$TERM" in
55
-
56 84
         xterm*|rxvt*|screen*)
57
-            test -n "$SSH_CONNECTION" \
58
-             && __host_id="${HOSTNAME%%.*}:"      # i.e. a remote session
59
-            PROMPT_COMMAND='__bashum__save_rv $?; echo -ne "\033]0;$(__bashum__lastrv)$__host_id${PWD/$HOME/\~}\$\007"'
60
-
61
-            # Show the currently running command in the terminal title:
62
-            # http://www.davidpashley.com/articles/xterm-titles-with-bash.html
63
-            show_command_in_title_bar()
64
-            {
65
-                case "$BASH_COMMAND" in
66
-                    *\033]0*)
67
-                        # The command is trying to set the title bar as well;
68
-                        # this is most likely the execution of $PROMPT_COMMAND.
69
-                        # In any case nested escapes confuse the terminal, so don't
70
-                        # output them.
71
-                        ;;
72
-                    "")
73
-                        echo -ne "\033]0;$(__bashum__lastrv)$__host_id${PWD/$HOME/\~}\$\007"
74
-                        ;;
75
-                    *)
76
-                        echo -ne "\033]0;${BASH_COMMAND} ($__host_id${PWD/$HOME/\~})\007"
77
-                        ;;
78
-                esac
79
-            }
80
-            trap show_command_in_title_bar DEBUG
85
+            echo -n 'echo -ne "\033]0;'
86
+            echo -n   '$(__bashum__lastrv)'
87
+            echo -n   "$(__bashum__mkhostid)"
88
+            echo -n   '${PWD/$HOME/\~}'
89
+            echo -n   '\$'
90
+            echo -n '\007"'
81 91
             ;;
92
+    esac
93
+}
82 94
 
83
-        *)
84
-            PROMPT_COMMAND='__bashum__save_rv $?;'
85
-            ;;
95
+__bashum__mkhostid() {
96
+    test -n "$SSH_CONNECTION" || return
97
+    echo "__bashum__mkhostid(): HOSTNAME='$HOSTNAME'" >&2
98
+    echo"${HOSTNAME%%.*}:"
99
+}
86 100
 
101
+__bashum__setup_traps() {
102
+    case "$TERM" in
103
+        xterm*|rxvt*|screen*) trap __bashum__set_title DEBUG ;;
87 104
     esac
88 105
 }
89 106
 
90
-__bashum__setup_prompt_command
107
+__bashum__setup_traps
108
+
109
+PROMPT_COMMAND=$(__bashum__mkpc)
91 110
 
92 111
 tasknag