Skip to content

Instantly share code, notes, and snippets.

@mihassan
Created October 9, 2023 02:53
Show Gist options
  • Save mihassan/d13fd7006f47e7b60d6d1a9fa24788be to your computer and use it in GitHub Desktop.
Save mihassan/d13fd7006f47e7b60d6d1a9fa24788be to your computer and use it in GitHub Desktop.
Sample Haskell .ghci template
-- Turn off output for resource usage and types. This is to reduce verbosity when reloading this file.
:unset +s +t
-- Turn on multi-line input and remove the distracting verbosity.
:set +m -v0
-- Turn off all compiler warnings and turn on OverloadedStrings for interactive input.
:seti -w -XOverloadedStrings
-- Set the preferred editor for use with the :e command. I would recommend using an editor in a separate terminal, and using :r to reload, but :e can still be useful for quick edits from within GHCi.
:set editor vim
:def! hoogle \x -> return $ ":!hoogle --color \"" ++ x ++ "\""
:def! doc \x -> return $ ":!hoogle --info --color \"" ++ x ++ "\""
:set -fwarn-unused-binds -fwarn-unused-imports
:set -isrc
:set -package directory
:{
dotGHCI_myPrompt promptString ms _ = do
-- Get the current directory, replacing $HOME with a '~'.
pwd <- getpwd
-- Determine which is the main module.
let main_module = head' [ m' | (m:m') <- ms, m == '*' ]
-- Put together the final prompt string.
-- ANSI escape sequences allow for displaying colours in compatible terminals. See [http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html this guide] for help interpreting them.
return $ concat [ "\ESC[33m\STX", pwd, main_module, "\ESC[37m\STX", promptString, " \ESC[0m\STX" ]
where
head' (x:_) = " \ESC[38;5;227m\STX" ++ x
head' _ = ""
getpwd = getpwd' <$> (System.Environment.getEnv "HOME") <*> System.Directory.getCurrentDirectory
getpwd' home pwd = if zipWith const pwd home == home
then '~':drop (length home) pwd
else pwd
:}
:set prompt-function dotGHCI_myPrompt "\ESC[38;5;129m\STX\xe61f"
:set prompt-cont-function dotGHCI_myPrompt "∷"
:set -package pretty-show -package hscolour
import qualified Language.Haskell.HsColour as HSC
import qualified Language.Haskell.HsColour.Colourise as HSC
:{
dotGHCI_myPrint :: (Show a) => a -> IO ()
dotGHCI_myPrint a = putStrLn $ HSC.hscolour HSC.TTY myColourPrefs False False "" False $ Text.Show.Pretty.ppShow a
where
myColourPrefs = HSC.defaultColourPrefs -- { HSC.conop = [HSC.Foreground HSC.Yellow]
-- , HSC.conid = [HSC.Foreground HSC.Yellow, HSC.Bold]
-- , HSC.string = [HSC.Foreground $ HSC.Rgb 29 193 57]
-- , HSC.char = [HSC.Foreground HSC.Cyan]
-- , HSC.number = [HSC.Foreground $ HSC.Rgb 202 170 236]
-- , HSC.keyglyph = [HSC.Foreground HSC.Yellow]
-- }
:}
:seti -interactive-print dotGHCI_myPrint
:def! pretty \_ -> return ":set -interactive-print dotGHCI_myPrint"
:def! nopretty \_ -> return ":set -interactive-print System.IO.print"
:m -Language.Haskell.HsColour
:m -Language.Haskell.HsColour.Colourise
-- Use :rr to reload this file.
:def! rr \_ -> return ":script ~/.ghci"
-- Turn on output of types. This line should be last.
:set +s +t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment