You are not logged in.

#1 2010-07-08 21:18:56

Mzg
Member
From: Norway
Registered: 2009-09-06
Posts: 43

[SOLVED] terminals & line drawing characters problem

I use quite some escape sequences in my bash prompt, amongst them a few that produce line drawing characters. Works fine in the virtual consoles (tty), but no terminals in X (tried xterm, terminal, urxvt) display them correctly, for some reason. My locale settings seem fine, and I've tried various fonts (including the one I use in the virtual consoles).

What could the issue be?

FYI, my bash prompt variable. Does it display correctly for you?

DRED='\e[0;31m'
DGREEN='\e[0;32m'
DYELLOW='\e[0;33m'
DBLUE='\e[0;34m' 
DPURPLE='\e[0;35m'
DCYAN='\e[0;36m'
DWHITE='\e[0;37m'
LRED='\e[1;31m'
LGREEN='\e[1;32m'
LYELLOW='\e[1;33m'
LBLUE='\e[1;34m' 
LPURPLE='\e[1;35m'
LCYAN='\e[1;36m'
LWHITE='\e[1;37m'
COLRESET='\e[0m'

PS1="\n\[$DBLUE\]\[\016\]l\[\017\]\[$DRED\](\u@\h)\[$DBLUE\]\[\016\]qqq\[\017\](\[$LBLUE\]H\!\[$DBLUE\])\[\016\]qq\[\017\](\[$LBLUE\]C\#\[$DBLUE\])\[\016\]qq\[\017\](\[$LBLUE\]J\j\[$DBLUE\])\[\016\]qqq\[\017\](\[$LBLUE\]\D{%H:%M %A %d - %B %Y}\[$DBLUE\])\n\[\016\]t\[\017\]\[\016\]q\[\017\](\[\[$LBLUE\]\w\[$DBLUE\])\n\[\016\]mqq\[\017\]> \[$LBLUE\]"

Last edited by Mzg (2010-07-09 10:00:41)


Archer.

Offline

#2 2010-07-09 09:57:12

Mzg
Member
From: Norway
Registered: 2009-09-06
Posts: 43

Re: [SOLVED] terminals & line drawing characters problem

After some detective work I have managed to come up with an explanation as well as what seems to be a working solution.

If you have not tried the prompt above, basically I tried to use some line drawing characters in my bash prompt, but these refused to display correctly under any X terminals I tried (though it looked fine on virtual consoles).

The problem was in the

\[\016\] ... \[\017\]

sequences. The \016 escape sequence is supposed to enter the alternate character set mode (common way to draw e.g. lines in text mode) while \017 returns the terminal to the standard character set. These work fine on the virtual consoles, but are apparently a no-no in (some) X terminals. If you tried the prompt you would notice that the color escape sequences worked fine, only the line drawing characters failed.

So that is the explanation part. So for the solution...

For urxvt, which is my preferred terminal emulator, the solution is quite simple. This should also work with other rxvt-derived terminals (including the popular aterm), though I have not tested this.

These terminals offer the same functionality as the virtual consoles, though the escape sequences are different. "\033(0" enters alternate charset mode while "\033(B" returns to standard charset mode. So the solution is simply to replace all previous

\[\016\] ... \[\017\]

sequences with

\[\033(0\] ... \[\033(B\]

But this poses a problem, as the virtual consoles do not recognize these sequences and you'll get the same problem as before, only in text mode instead of X. I solved this by using the PROMPT_COMMAND variable to run fgconsole every time a command is ran in bash, checking if the command is ran in tty7 (i.e. X11) or in one of the (other) virtual consoles. This is my code for the root prompt:

DRED='\e[0;31m'
DGREEN='\e[0;32m'
DYELLOW='\e[0;33m'
DBLUE='\e[0;34m' 
DPURPLE='\e[0;35m'
DCYAN='\e[0;36m'
DWHITE='\e[0;37m'
LRED='\e[1;31m'
LGREEN='\e[1;32m'
LYELLOW='\e[1;33m'
LBLUE='\e[1;34m' 
LPURPLE='\e[1;35m'
LCYAN='\e[1;36m'
LWHITE='\e[1;37m'
COLRESET='\e[0m'

function prompt_func
{
    # Running in tty7 or larger (X11)
    if [ `sudo fgconsole` -gt 6 ] 
    then
        PS1="\n\[$DBLUE\]\[\033(0\]l\[\033(B\]\[$DRED\](\u@\h)\[$DBLUE\]\[\033(0\]qqq\[\033(B\](\[$LBLUE\]H\!\[$DBLUE\])\[\033(0\]qq\[\033(B\](\[$LBLUE\]C\#\[$DBLUE\])\[\033(0\]qq\[\033(B\](\[$LBLUE\]J\j\[$DBLUE\])\[\033(0\]qqq\[\033(B\](\[$LBLUE\]\D{%H:%M %A %d - %B %Y}\[$DBLUE\])\n\[\033(0\]t\[\033(B\]\[\033(0\]q\[\033(B\](\[\[$LBLUE\]\w\[$DBLUE\])\n\[\033(0\]mqq\[\033(B\]> \[$LBLUE\]"
    else
        PS1="\n\[$DBLUE\]\[\016\]l\[\017\]\[$DRED\](\u@\h)\[$DBLUE\]\[\016\]qqq\[\017\](\[$LBLUE\]H\!\[$DBLUE\])\[\016\]qq\[\017\](\[$LBLUE\]C\#\[$DBLUE\])\[\016\]qq\[\017\](\[$LBLUE\]J\j\[$DBLUE\])\[\016\]qqq\[\017\](\[$LBLUE\]\D{%H:%M %A %d - %B %Y}\[$DBLUE\])\n\[\016\]t\[\017\]\[\016\]q\[\017\](\[\[$LBLUE\]\w\[$DBLUE\])\n\[\016\]mqq\[\017\]> \[$LBLUE\]"
    fi    
}
PROMPT_COMMAND=prompt_func

Note that I do not claim to be a good bash programmer, so there might be better ways. This has however worked flawlessly so far, including when su'ing to other users, and switching between tty's and X.

The code is the same for my other users, except from the colors. Note the

`sudo fgconsole`

Sudo proved necessary for non-root users. I added an entry in the sudoers config file to let all users run fgconsole as root without providing a password.


Okay, that's it. It's a bit dirty, but it solved my problem at least.

Last edited by Mzg (2010-07-10 07:06:44)


Archer.

Offline

Board footer

Powered by FluxBB