Last Updated: June 19, 2019
·
16.37K
· nobbz

Colored GHCi prompt with λ and modules on separate lines

When you take a look at other developers GHCi prompt you can often see a simple prompt like the following:

λ>

This is achieved by simply putting the following snippet into ~/.ghci:

:set prompt "\ESC[34mλ> \ESC[m"

Where the \ESC[34m is ANSI-code for a blue foreground color and the simple \ESC[m reverts everything into the defaults.

But since I want to know which modules are loaded into GHCi and which of them are interpreted and which are compiled, I extended the above prompt:

:set prompt "\ESC[1;34m%s\n\ESC[0;34mλ> \ESC[m"

\ESC[1;34m%s\n produces a first line which lists the loaded modules in bold blue, whereas \ESC[0;34mλ> \ESC[m sets the prompt like above, but removes the bold-attribute.

With this new prompt you have everything… There are colors to quickly recognize the last time the prompt was displayed, you have the cool λ that every “real” haskell-programmer must have, and you know which modules are loaded at any given time, and since they are colored you can distinguish the line from other output easily (it helps you to find last prompt also!).

More about ANSI-escape sequences

6 Responses
Add your response

nice!!, thank you for sharing this.

over 1 year ago ·

Good. thnx.

over 1 year ago ·

Thank you!

over 1 year ago ·

I wrote little wrapper
You can use it like this


:def color (_ -> return (":set -interactive-print=FunnyPrint.funnyPrint\n:set prompt " ++ FunnyPrint.prompt "λ " "%s" " > " ++ "\n:set prompt2 " ++ FunnyPrint.prompt2 "λ " "" "| "))
:def nocolor (_ -> return ":set -interactive-print=print\n:set prompt \"%s> \"\n:set prompt2 \"%s| \"")

:color

But with newline it don't work. Would be grateful if you may suggest, why.

over 1 year ago ·

Update: cozy multiline version:

Put next to your ~/.ghc/ghci.conf

-- OverloadedStrings is often useful.
:set -XOverloadedStrings

-- Scoped type variables is often useful so we can specify the types
-- of variables (for example, in lambda expressions).
:set -XScopedTypeVariables

-- useful for import from specified package
:set -XPackageImports

-- Force load dependencies
:set -package funnyprint

-- Show the types of evaluated expressions
:set +t

-- And stats
:set +s

-- Enable multi-line expressions with :{ and :}
:set +m

-- Make the prompt a little more colorful.
-- And pretty-printing values

:def color (\_ -> return (":set -interactive-print=FunnyPrint.funnyPrint\n:set prompt \"" ++ FunnyPrint.prompt "λ " "%s" " ¬\\nλ > " ++ "\"" ++ "\n:set prompt2 \"" ++ FunnyPrint.prompt2 "λ" "" " | " ++ "\""))
:def nocolor (\_ -> return ":set -interactive-print=print\n:set prompt \"%s> \"\n:set prompt2 \"%s| \"")

-- turn fun on
:color
over 1 year ago ·

Great and simple prompt; I have one improvement. GHCi's line editor (Haskeline) breaks in very confusing ways when lines wrap while using this prompt: it guesses the printed width of the prompt string incorrectly, and puts the cursor in the wrong place w.r.t. the text. The fix is to terminate ANSI escape sequences with \STX (the non-printing ASCII "start of text" character), which allows Haskeline to know the correct width of the prompt:

:set prompt "\ESC[1;34m\STX%s\n\ESC[0;34m\STXλ> \ESC[m\STX"

See https://github.com/judah/haskeline/blob/master/System/Console/Haskeline/LineState.hs#L109 for the code that makes this happen.

over 1 year ago ·