better way of detecting environment and doing linedraw with utf8 and console and foo
authorJoerg Jaspert <joerg@ganneff.de>
Tue, 2 Apr 2013 13:06:34 +0000 (15:06 +0200)
committerJoerg Jaspert <joerg@ganneff.de>
Tue, 2 Apr 2013 13:06:34 +0000 (15:06 +0200)
i hate it, why are there so many ways? anyways, for me it works in tmux, screen,
linux console, ssh sessions, x (rxvt, xterm, konsole, gnome-terminal) and various
locales (utf-8 based, posix, iso88591). So i hope it works in other places too.

.zsh/00_Basics.zsh
.zsh/01_Terminfo.zsh
.zsh/05_Functions.zsh
.zsh/functions/prompt_ganneff_setup

index f052dcc..69d5d0a 100644 (file)
@@ -104,6 +104,16 @@ isfreebsd(){
     return 1
 }
 
+isutf8(){
+    [[ $(locale charmap) == "UTF-8" ]] && return 0
+    return 1
+}
+
+isconsole(){
+    [[ $TERM == "linux" ]] && return 0
+    return 1
+}
+
 if ! [[ ${ZSH_VERSION} == 3.1.<7->*      \
      || ${ZSH_VERSION} == 3.<2->.<->*    \
      || ${ZSH_VERSION} == <4->.<->*   ]] ; then
index 3a4b7af..d8538b4 100644 (file)
@@ -82,13 +82,8 @@ __ () {
     fi
 
     # Is linedrawing supported?
-    if tput acsc >/dev/null; then
-        # For some reason the console says it can do, but then
-        # does not actually do so. At least, not with the way we are
-        # going later.
-        if [[ $TERM != "linux" ]]; then
-            LINEDRAW="true"
-        fi
+    if tput acsc >/dev/null || isutf8 || isconsole; then
+        LINEDRAW="true"
     fi
 
     setvar COLORS ${COLORS}
index 2d2f98a..f533481 100644 (file)
@@ -13,20 +13,31 @@ preprint()
     fi
     if [[ $LINEDRAW == "true" ]]; then
         # Some stuff to help us draw nice lines
-        start="$terminfo[smacs]"
-        stop="$terminfo[rmacs]"
-        hbar="${start}${(l:$(( 74 - ${#1} - 5 ))::q:)}${stop}"
-        pipe1="u"
-        pipe2="t"
-        draw="q"
+        if isutf8 || isconsole; then
+            start=""
+            stop=""
+            hbar="─"
+            PR_ULCORNER="┌"
+            PR_LLCORNER="└"
+            PR_LRCORNER="┘"
+            PR_URCORNER="┐"
+        else
+            start="$terminfo[smacs]"
+            stop="$terminfo[rmacs]"
+            hbar="q"
+            pipe1="u"
+            pipe2="t"
+            draw="q"
+        fi
     else
         start=""
         stop=""
-        hbar="${start}${(l:$((74 - ${#1} - 5))::-:)}${stop}"
+        hbar="-"
         pipe1="|"
         pipe2="|"
         draw="-"
     fi
+    hbar="${start}${${(l:$(( 74 - ${#1} - 5 ))::X:)}//X/$hbar}${stop}"
     out="${my_color}${hbar}${start}"
 
        if [[ "${1}" != "" ]]; then
index f434f25..c651696 100644 (file)
@@ -196,17 +196,28 @@ prompt_ganneff_setup () {
 
     # See if we can and should use extended characters to look nicer.
     if zstyle -t ':prompt:ganneff' nicelines; then
-        typeset -A altchar
-        set -A altchar ${(s..)terminfo[acsc]}
-        # Some stuff to help us draw nice lines
-        PR_SET_CHARSET="%{$terminfo[enacs]%}"
-        PR_SHIFT_IN="%{$terminfo[smacs]%}"
-        PR_SHIFT_OUT="%{$terminfo[rmacs]%}"
-        PR_HBAR=${altchar[q]:--}
-        PR_ULCORNER=${altchar[l]:--}
-        PR_LLCORNER=${altchar[m]:--}
-        PR_LRCORNER=${altchar[j]:--}
-        PR_URCORNER=${altchar[k]:--}
+        if [[ $(locale charmap) == "UTF-8" ]]; then
+            PR_SET_CHARSET=""
+            PR_SHIFT_IN=""
+            PR_SHIFT_OUT=""
+            PR_HBAR="─"
+            PR_ULCORNER="┌"
+            PR_LLCORNER="└"
+            PR_LRCORNER="┘"
+            PR_URCORNER="┐"
+        else
+            typeset -A altchar
+            set -A altchar ${(s..)terminfo[acsc]}
+            # Some stuff to help us draw nice lines
+            PR_SET_CHARSET="%{$terminfo[enacs]%}"
+            PR_SHIFT_IN="%{$terminfo[smacs]%}"
+            PR_SHIFT_OUT="%{$terminfo[rmacs]%}"
+            PR_HBAR=${altchar[q]:--}
+            PR_ULCORNER=${altchar[l]:--}
+            PR_LLCORNER=${altchar[m]:--}
+            PR_LRCORNER=${altchar[j]:--}
+            PR_URCORNER=${altchar[k]:--}
+        fi
     else
         PR_SET_CHARSET=""
         PR_SHIFT_IN=""
@@ -481,14 +492,7 @@ prompt_ganneff_precmd () {
     # Now a kind-of-hack to reduce the prompt when we run out of space.
     promptsize=${#PR_PS1}
     if [[ ${promptsize} -lt ${TERMWIDTH} ]]; then
-        # the q here should be ${PR_HBAR}, but for some reason zsh didn't like me,
-        # so meh
-        if zstyle -t ':prompt:ganneff' nicelines && \
-            [[ "$TERM" != dumb ]] && [[ "$TERM" != "linux" ]]; then
-            PR_FLEXLINE="${(l.(($TERMWIDTH - $promptsize ))..q.)}"
-        else
-            PR_FLEXLINE="${(l.(($TERMWIDTH - $promptsize ))..-.)}"
-        fi
+        PR_FLEXLINE=${${(el.(($TERMWIDTH - $promptsize ))..X.)}//X/$PR_HBAR}
     else
         # Small size prompt is needed
         left_items=(ulcorner line openparentheses user at host closeparentheses line openparentheses