[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: man pdksh, I don't understand this - [ was: how to make colour prompts for pdksh ]



2006. July 27. 00:40, LeVA:

Here I will post a solution and an explanation which was provided by 
Brian T. Brunner in a private mail conversation.

- START OF QUOTE -
> > > Hi!
> > >
> > >
> > > "... Note that since the command line editors try to figure out
> > > how 
> > > long the prompt is  (so  they  know how far it is to edge of the
> > > screen), escape codes in the prompt tend to mess things up. ..."
> > >
> > > Okay, I get it, I'm using escape codes, so I need the following
> > > hack. 
> > >
> > > "... You can tell the shell not to count certain sequences (such
> > > as 
> > > escape codes) by  prefixing your prompt with a non-printing
> > > character 
> > > (such as control-A) followed by a carriage return and then
> > > delimiting 
> > > the escape codes with this non-printing  character."
> > >
> > > I've put my escape character to a variable to make things cleaner:
> > >
> > > ESC="^"
> >
> > This look like an up-arrow, which is not what you need.
> > Where you see ^[ there is to be just one character, the escape
> > character.
> >
> > In vi/vim/gvim, as others have posted, in insert mode, use
> > "control-v ESC"  
> > making the obvious substitution) to insert a single escape character
> > that will show up as "^[".
>
> It is the escape character, I was just using mcedit which displays it 
> like that. I'm just learning vim and can not use it as fast as other 
> editors.
>
> >
> > >
> > > So if I have a simple prompt like this:
> > >
> > > PS1='$ESC[01;32m$USER> '
> > >
> > > now if I prefix my prompt with a non-printing char (with control-A
> > > like suggested) then I get an error when starting the shell:
> > > /bin/pdksh: /home/leva/.profile[11]: no closing quote
> > >
> > > I see that I must put a carriage return after that, but I don't
> > > know how. How can I put a CR after the control-A (if control-A is
> > > what I need at all)? 
> > >
> >
> > To put a carriage return in a line, use "control-v control-m"
> >
> Now that I know that :) Please tell me how to figure out those key 
> combos.
>
It's bleeding obvious, as soon as somebody explains (like many things).

Control characters start at 0 and go upwards from there to the
"printable characters".

Control characters have a mapping to the characters @ABCDEFG...

So if you need a NULL character, ^@ will provide it, to insert a null
character in a 
string in a shell script you're editing with vi, control-v control-@
will do it.
If you need an STX character (binary 0x02), it's control-v control-b.
^m is the line feed, so to insert a line feed in a text string it's
control-v control-m
Likewise, the ^[ escape char is ...

now you need to figure out which control characters you'll need, and the
binary number 
of that control character, and what printable character that maps to,
then insert 
control-v control-mumble into your application-oriented
text-string-with-embedded-control-characters.

check http://en.wikipedia.org/wiki/ASCII about 2 screens down.
>
> And finally please allow me to post this to the debian-user list 
> including an example for the solution. I won't post until you ACK it.
>
> Thanks!
>
> Daniel

ACK!

- END OF QUOTE -

--------------------
An example solution:
--------------------
# This hack is needed so the command line doesn't get wrapped too soon
# with a coloured prompt

# Escape key entered with control+v + ESC
ESC="^["

# The non-printing character entered with control+v + control+a
NP="^A"

# The carriage return character entered with control+v + control+m
CR="^M"

PS1='$NP$CR $NP $ESC[01;32m $NP $USER $ $NP$ESC[0m$NP'
#    ^^^^^^ ^^^ ^^^^^^^^^^^ ^^^         ^^^^^^^^^^^^^
#    |||||| ||| ||||||||||| |||         same as before
#    |||||| ||| colour-code |||
#    |||||| |||             |||
#    prefix \\\ delimiter   ///


This will result in a green prompt like this:
    leva $

The leading spaces are caused by the spaces surrounding the second and 
the third $NP strings. It goes without saying that those are not 
necessary, I just put them there to make the example cleaner.

And finally here is my fully functional final prompt:
PS1='$NP$CR$NP$ESC[01;32m$NP$USER$NP$ESC[0m$NP($NP$ESC[01;34m$NP$PWD\
$NP$ESC[0m$NP)$NP$ESC[01;32m$NP@`hostname -s` $ $NP$ESC[0m$NP'

The prefix is needed only once (at the beginning), and the delimiter is 
needed to surround every instance of a colour-code.


Daniel

--
LeVA



Reply to: