Search code examples
haskellpromptghci

How to set GHCi prompt to display modules separated by a custom separator?


Currently this is how my ghci prompt looks like:

enter image description here

And I want to make it look like this:

enter image description here

or like this

enter image description here

Any ideas on how I can do this? My configuration(ghci.conf) file's contents is as shown below

:set prompt "\ESC[33m\STX[%s]\ESC[38;5;86m\STX \x03BB > \ESC[0m\STX"

in which I followed the syntax written on:

https://downloads.haskell.org/ghc/8.8.1-alpha1/docs/html/users_guide/ghci.html#ghci-cmd-:set%20prompt


Solution

  • You can use prompt-function instead of prompt to run more advanced Haskell functions for the prompt. Here's an example ghci.conf for your second prompt:

    :{
    prompter :: [String] -> Int -> IO String
    prompter modules line = return $
        concat [ "\ESC[33m\STX["
               , Data.List.intercalate ", " modules
               , "]\ESC[38;5;86m\STX \x03BB > \ESC[0m\STX"
               ]
    :}
    
    :set prompt-function prompter
    

    Or, for numbers, you can utilize zipWith:

    :{
    prompter :: [String] -> Int -> IO String
    prompter modules line = return $
        concat [ "\ESC[33m\STX["
               -- this is the only line that changed
               , Data.List.intercalate ", " $ zipWith (\n m -> concat [show n, ".", m]) [1..] modules
               , "]\ESC[38;5;86m\STX \x03BB > \ESC[0m\STX"
               ]
    :}
    
    :set prompt-function prompter
    

    I'll leave it as an exercise to write the continuation line, but it's very similar and just involves prompt-cont-function.