Personal tools

GHCi in colour

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(Added a note about \001..\002 for readline and an example.)
m
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
This page documents efforts to colourise GHCi output.
 
This page documents efforts to colourise GHCi output.
  
== Example ==
+
== Prompt only ==
 +
 
 +
The prompt can be colored by altering GHCi's <code>prompt</code> setting including ANSI terminal color codes. For example
 +
 
 +
    :set prompt "\ESC[34m\STX%s > \ESC[m\STX"
 +
 
 +
will give the prompt blue color:
 +
 
 +
    [[Image:Colored_ghci_prompt.png‎]]
 +
 
 +
As usual, adding this to <code>ghci.conf</code> will auto-execute the command when running GHCi, making the prompt colored by default.
 +
 
 +
Remember to use <code>\STX</code> to end an escape sequence. Without this control character, the prompt length will be [http://trac.haskell.org/haskeline/wiki/ControlSequencesInPrompt computer incorrectly] and your prompt will look wrong when editing long commands.
 +
 
 +
== Full highlighting ==
 +
 
 +
=== Example ===
  
 
     [[Image:Colour-ghci.png]]
 
     [[Image:Colour-ghci.png]]
Line 15: Line 31:
 
and would appear in the console/xterm as ansi terminal coloured output.
 
and would appear in the console/xterm as ansi terminal coloured output.
  
== Implementation ==
+
=== Implementation ===
  
=== Using sed and ghci.conf ===
+
==== 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 [http://martijn.van.steenbergen.nl/journal/2010/02/27/colors-in-ghci/ this] blog post for details.
 
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 [http://martijn.van.steenbergen.nl/journal/2010/02/27/colors-in-ghci/ 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 [[:Image:Ghci 256color.tar.gz|this example]]. For what it can look like refer to [[:Image:GHCi 256color.jpg|this picture]].
+
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 [[:File:Ghci 256color.tar.gz|this example]]. Output:
 +
 
 +
    [[Image:GHCi 256color.jpg]]
 +
 
 
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.
 
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 ===
+
==== Using HsColour ====
  
 
An existing tool, [http://www.cs.york.ac.uk/fp/darcs/hscolour/ HsColour],  
 
An existing tool, [http://www.cs.york.ac.uk/fp/darcs/hscolour/ HsColour],  
Line 38: Line 57:
 
interaction with readline isn't ideal.
 
interaction with readline isn't ideal.
  
=== GuiHaskell ===
+
See http://www.reddit.com/r/haskell/comments/144biy/pretty_output_in_ghci_howto_in_comments/
 +
 
 +
==== GuiHaskell ====
  
Neil Mitchell has a prototype [http://www-users.cs.york.ac.uk/~ndm/projects/guihaskell.php gui haskell] wrapper, based on gtk. Does this contain a reasonable ghci wrapper we could steal?
+
Neil Mitchell has a prototype [http://community.haskell.org/~ndm/guihaskell/ gui haskell] wrapper, based on gtk. Does this contain a reasonable ghci wrapper we could steal?
  
  

Latest revision as of 08:12, 28 September 2015

This page documents efforts to colourise GHCi output.

Contents

[edit] 1 Prompt only

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

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

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.

Remember to use \STX to end an escape sequence. Without this control character, the prompt length will be computer incorrectly and your prompt will look wrong when editing long commands.

[edit] 2 Full highlighting

[edit] 2.1 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.

[edit] 2.2 Implementation

[edit] 2.2.1 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. Output:

   GHCi 256color.jpg

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.

[edit] 2.2.2 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
(thumbnail)
HsColour in action

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

See http://www.reddit.com/r/haskell/comments/144biy/pretty_output_in_ghci_howto_in_comments/

[edit] 2.2.3 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.