Difference between revisions of "GHCi in colour"

From HaskellWiki
Jump to navigation Jump to search
(Added section on how to color the prompt only, updated GuiHaskell link)
m (→‎Prompt only: Corrected code markup)
Line 3: Line 3:
 
== Prompt only ==
 
== Prompt only ==
   
The prompt can be colored by altering GHCi's 'prompt' setting including ANSI terminal color codes. For example
+
The prompt can be colored by altering GHCi's <code>prompt</code> setting including ANSI terminal color codes. For example
   
 
:set prompt "\ESC[34m%s > \ESC[m"
 
:set prompt "\ESC[34m%s > \ESC[m"
Line 11: Line 11:
 
[[Image:Colored_ghci_prompt.png‎]]
 
[[Image:Colored_ghci_prompt.png‎]]
   
As usual, adding this to ghci.conf will auto-execute the command when running GHCi, making the prompt colored by default.
+
As usual, adding this to <code>ghci.conf</code> will auto-execute the command when running GHCi, making the prompt colored by default.
 
 
   
 
== Full highlighting ==
 
== Full highlighting ==

Revision as of 12:29, 6 December 2012

This page documents efforts to colourise GHCi output.

Prompt only

The prompt can be colored by altering GHCi's prompt setting including ANSI terminal color codes. For example

   :set prompt "\ESC[34m%s > \ESC[m"

will give the prompt blue color:

   Colored ghci prompt.png

As usual, adding this to ghci.conf will auto-execute the command when running GHCi, making the prompt colored by default.

Full highlighting

Example

   Colour-ghci.png

Or a type error:

   Coloured-error.png

Output like this would be the result of running, for example:

   ghci --colour

and would appear in the console/xterm as ansi terminal coloured output.

Implementation

Using sed and ghci.conf

You can use sed to color the non-prompt output, and color the prompt with ghci.conf. This works better than piping into HsColour for me. See this blog post for details.

This allows you to colorize everything you can catch with a regexp. Note that when coloring GHCi prompt using ghci.conf one should, in ultimate escapity, escape the escape codes with \001..\002, otherwise readline will mess up when editing long lines. For a working example of how this can be done refer to this example. For what it can look like refer to this picture. This approach doesn't work well if you try to output an infinite sequence and then hit CTRL+C, ideas on how to fix that are welcome.

Using HsColour

An existing tool, HsColour, could be modified to operate interactively. In fact, HsColour is already interactive, and with a small patch added on 2006-12-14 to control ouput buffering better, this works relatively nicely:

   ghci 2>&1 | HsColour -tty
HsColour in action

There are small delays however, when lexing certain tokens, and the interaction with readline isn't ideal.

GuiHaskell

Neil Mitchell has a prototype gui haskell wrapper, based on gtk. Does this contain a reasonable ghci wrapper we could steal?


If you have an idea of how to do this nicely, add your proposal here.