PRY colorized prompt pry

The solution, now !

After few hours of digging around several solution, I’ve finally have a working colorized prompt for PRY !*

So the final solution now : (puts those lines in your .pryrc at you $HOME)

    Pry.prompt = [
                  proc { |target_self, nest_level, pry|
                        "[#{pry.input_array.size}]\001\e[0;32m\002#{Pry.config.prompt_name}\001\e[0m\002(\001\e[0;33m\002#{Pry.view_clip(target_self)}\001\e[0m\002)#{":#{nest_level}" unless nest_level.zero?}> "
                       },
                  proc { |target_self, nest_level, pry|
                        "[#{pry.input_array.size}]\001\e[1;32m\002#{Pry.config.prompt_name}\001\e[0m\002(\001\e[1;33m\002#{Pry.view_clip(target_self)}\001\e[0m\002)#{":#{nest_level}" unless nest_level.zero?}* "
                       }
                  ]
    

Explications

First the color code :

    Black       0;30     Dark Gray     1;30
    Blue        0;34     Light Blue    1;34
    Green       0;32     Light Green   1;32
    Cyan        0;36     Light Cyan    1;36
    Red         0;31     Light Red     1;31
    Purple      0;35     Light Purple  1;35
    Brown       0;33     Yellow        1;33
    Light Gray  0;37     White         1;37
    

The color format is \e[0;31m for a red and \e[1;31m for a light red, ending with a \e[0m sequence (full ANSI sequence : {ESC}[{attr};{bg};{256colors};{fg}m )

Take a look a this for further detail : SGR (Select Graphic Rendition) parameters

0   Reset current attributes
1   Set BrightOrBold
2   Unset BrightOrBold
3   Set ItalicOrInverse
4   Set BackOrUnderline
5   Set BackOrUnderline
30...37   Set ANSI text color
38 ; 5 ; n  Set xterm text color, n is color index from 0 to 255
39  Reset text color to defaults
40...47   Set ANSI background color
48 ; 5 ; n  Set xterm background color, n is color index from 0 to 255
49  Reset background color to defaults 

But you have to escape those special characters because the readline library doesn’t count well the line length. So this why we got a \001 in starting and a \001 in finish.

Further reading

Pry prompt can change in real-time in the pry shell with the _pry_ variable, ie :

    _pry_.prompt = proc { "> " }
    _pry_.prompt = Pry::DEFAULT_PROMPT
    

Check those links :


comments powered by Disqus