In bash you can colorize your bash prompt using ANSI colors using a method like this:
This method of getting a colored prompt however messes up the word wrapping of the terminal. Since the entire sequence of "\e[1;31m" actually takes up zero characters when displayed in the terminal (all it does is cause everything that follows to be displayed as bright red), the terminal emulator can't calculate when to word-wrap and it causes symptoms such as:
* The line tries to wrap too soon (before you reach the end of the line)
* When it wraps it actually doesn't break to the next line, but goes back to the start of the current line, so you type over top of what's already been typed
* When it's doing this, if you backspace or correct your command, each correction you make causes the whole entire line to be copied to the next line with the correction made.
The way around this for bash is to use the \[\] format for the PS1 prompt, and use the tput command inside them:
The \[...\] format causes the result of everything inside those brackets to result in a zero-width output, so that the terminal emulator doesn't count the output when it calculates when to do word-wrapping at.
How this relates to Perl
If you make a Perl console application that gives the user a prompt to run commands (a MySQL client for instance), and the prompt is to be colorized, you'd probably use Term::ANSIColor like this:
But this would cause \e[1;32m and \e[0m to be added to the text "mysql> ", which messes with the word-wrapping in the terminal emulator and causes the same problem as in bash.
Is there a Perl equivalent of \[\] so that ANSI color codes can be used without messing with the word wrapping?
Cuvou.com | My personal homepage
Code:
PS1="[red]\e[1;31m[/red][\u@\h \W]\$[red]\e[0m[/red] "
This method of getting a colored prompt however messes up the word wrapping of the terminal. Since the entire sequence of "\e[1;31m" actually takes up zero characters when displayed in the terminal (all it does is cause everything that follows to be displayed as bright red), the terminal emulator can't calculate when to word-wrap and it causes symptoms such as:
* The line tries to wrap too soon (before you reach the end of the line)
* When it wraps it actually doesn't break to the next line, but goes back to the start of the current line, so you type over top of what's already been typed
* When it's doing this, if you backspace or correct your command, each correction you make causes the whole entire line to be copied to the next line with the correction made.
The way around this for bash is to use the \[\] format for the PS1 prompt, and use the tput command inside them:
Code:
PS1="[red]\[$(tput bold; tput setaf 1)\][/red][\u@\h \W]\$[red]\[$(tput sgr0)\][/red] "
The \[...\] format causes the result of everything inside those brackets to result in a zero-width output, so that the terminal emulator doesn't count the output when it calculates when to do word-wrapping at.
How this relates to Perl
If you make a Perl console application that gives the user a prompt to run commands (a MySQL client for instance), and the prompt is to be colorized, you'd probably use Term::ANSIColor like this:
Code:
print Term::ANSIColor::colored("mysql> ", 'bold green');
But this would cause \e[1;32m and \e[0m to be added to the text "mysql> ", which messes with the word-wrapping in the terminal emulator and causes the same problem as in bash.
Is there a Perl equivalent of \[\] so that ANSI color codes can be used without messing with the word wrapping?
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'