Skip to content

Instantly share code, notes, and snippets.

@taktoa
Last active December 27, 2017 18:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taktoa/900372f06164cc058ea0 to your computer and use it in GitHub Desktop.
Save taktoa/900372f06164cc058ea0 to your computer and use it in GitHub Desktop.
My .ghci dotfile
--------------------------------------------------------------------------------
---- Reset all metaflags -------------------------------------------------------
:unset +m
:unset +r
:unset +s
:unset +t
:unset +c
--------------------------------------------------------------------------------
---- Import some useful modules ------------------------------------------------
import Flow ((|>), (.>))
import Data.Monoid
import Control.Monad
import Control.Arrow ((***), (&&&))
import qualified Data.Aeson as Aeson
import qualified Control.Lens as Lens
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.Text.Lazy as LText
import qualified Data.Text.Lazy.Encoding as LText
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Scientific as Scientific
import qualified Data.UUID as UUID
import qualified Control.Monad.Catch as MonadThrow
import qualified Control.Monad.Catch as MonadCatch
import qualified Control.Monad.Catch as MonadMask
--------------------------------------------------------------------------------
---- Override `:def` to include help information. ------------------------------
:set -XOverloadedStrings
ghciCustomHelp <- pure ([] :: [(String, [String])])
:{
let ghciAddHelp str = do
let p = Text.pack
let (cmd, doc) = Text.breakOn (p " ") (p str)
let result = [ "ghciCustomHelp <- pure ("
, "(\"", cmd, "\"", ", ", doc, ")"
, " : ghciCustomHelp)"
] |> mconcat |> Text.unpack
pure result
:}
:def add-help ghciAddHelp
:{
let ghciDefine :: String -> IO String
ghciDefine str = do
let p = Text.pack
let (cmd, rest) = Text.breakOn (p " ") (p str)
case map Text.strip (Text.splitOn (p "%%%") rest) of
[doc, expr] -> [ p ":add-help " <> cmd <> " " <> doc
, p "::def " <> cmd <> " " <> expr
] |> Text.unlines |> Text.unpack |> pure
_ -> fail ("Bad arguments to `:define`: " <> str)
:}
:def define ghciDefine
:set -XNoOverloadedStrings
--------------------------------------------------------------------------------
---- Define `:enable`, `:disable`, `:enablei`, and `:disablei`. ----------------
-- Define some helper functions
let ghciSGR c i = "\ESC[" ++ c ++ "m" ++ i ++ "\ESC[0m"
let ghciEnableMsg e = putStrLn (mconcat [ghciSGR "1;32" "Enabling", " ", e])
let ghciDisableMsg e = putStrLn (mconcat [ghciSGR "1;31" "Disabling", " ", e])
-- Enable a language extension with the given name.
:{
let ghciEnable i = ghciEnableMsg i >> pure (":set -X" ++ i)
ghciEnableDoc = [ [ ":enable <ext>"
, " "
, "enable the given language extension"
]
] |> map mconcat
:}
:define enable ghciEnableDoc %%% ghciEnable
-- Disable a language extension with the given name.
:{
let ghciDisable i = ghciDisableMsg i >> pure (":set -XNo" ++ i)
ghciDisableDoc = [ [ ":disable <ext>"
, " "
, "disable the given language extension"
]
] |> map mconcat
:}
:define disable ghciDisableDoc %%% ghciDisable
-- Enable a language extension for interactive use only.
:{
let ghciEnableI i = ghciEnableMsg i >> pure (":seti -X" ++ i)
ghciEnableIDoc = [ [ ":enablei <ext>"
, " "
, "interactively enable the given extension"
]
] |> map mconcat
:}
:define enablei ghciEnableIDoc %%% ghciEnableI
-- Disable a language extension for interactive use only.
:{
let ghciDisableI i = ghciDisableMsg i >> pure (":seti -XNo" ++ i)
ghciDisableIDoc = [ [ ":disablei <ext>"
, " "
, "interactively disable the given extension"
]
] |> map mconcat
:}
:define disablei ghciDisableIDoc %%% ghciDisableI
--------------------------------------------------------------------------------
---- Define other commands -----------------------------------------------------
-- This allows you to "source" (as in Bash) a file containing GHCi commands.
:{
let ghciSource i = readFile i
ghciSourceDoc = [ [ ":source <file>"
, " "
, "source the given GHCi command file"
]
] |> map mconcat
:}
:define source ghciSourceDoc %%% ghciSource
-- Clear the screen, then `:reload`.
:{
let ghciReset _ = putStrLn "\ESCc\ESC[0m\ESC[?25h" >> pure ":reload"
ghciResetDoc = [ [ ":reset"
, " "
, "clear the screen, then `:reload`"
]
] |> map mconcat
:}
:define reset ghciResetDoc %%% ghciReset
-- Clear the screen, then `:reload!`.
:{
let ghciResetB _ = putStrLn "\ESCc\ESC[0m\ESC[?25h" >> pure ":reload!"
ghciResetBDoc = [ [ ":reset!"
, " "
, "clear the screen, then `:reload!`"
]
] |> map mconcat
:}
:define reset! ghciResetBDoc %%% ghciResetB
-- Run `hlint` on the current directory.
:{
let ghciHLint _ = pure ":!hlint ."
ghciHLintDoc = [ [ ":hlint"
, " "
, "run `hlint .` in the current directory"
]
] |> map mconcat
:}
:define hlint ghciHLintDoc %%% ghciHLint
-- Search, using `hoogle`, for the given expression or type.
:{
let ghciHoogle i = pure $ ":!hoogle --count=15 \"" ++ i ++ "\""
ghciHoogleDoc = [ [ ":hoogle [<args> ...]"
, " "
, "run `hoogle --count=15 <args>`"
]
] |> map mconcat
:}
:define hoogle ghciHoogleDoc %%% ghciHoogle
-- Run `haskell-docs` with the given arguments.
:{
let ghciDoc i = pure $ ":!haskell-docs " ++ i
ghciDocDoc = [ [ ":doc <mod> <ident> [<pkg>]"
, " "
, "run `haskell-docs` with the given args"
]
, [ ":doc <ident>"
, " "
, "run `haskell-docs` with the given args"
]
] |> map mconcat
:}
:define doc ghciDocDoc %%% ghciDoc
-- Run the `ls` command with the given arguments.
:{
let ghciLs i = pure (":!ls " ++ i)
ghciLsDoc = [ [ ":ls [<args> ...]"
, " "
, "run `ls` with the given arguments"
]
] |> map mconcat
:}
:define ls ghciLsDoc %%% ghciLs
--------------------------------------------------------------------------------
---- Add defined commands to the `:help` prompt --------------------------------
:{
let ghciDisplayHelp = do
let h = reverse ghciCustomHelp
putStrLn " -- Commands defined in the `.ghci` file:\n"
putStrLn (concatMap (unlines . map (" " <>) . snd) h)
putStr " --"
:}
:def ? \i -> ghciDisplayHelp >> pure "::help"
:def help \i -> ghciDisplayHelp >> pure "::help"
--------------------------------------------------------------------------------
---- Set the desired metaflags and language extensions -------------------------
-- Collect type/location info after loading modules
:set +c
-- Show the type after each evaluation
:set +t
:enable UnicodeSyntax
:enablei OverloadedStrings
:enablei TupleSections
:enablei LambdaCase
:enablei NegativeLiterals
:enablei TypeOperators
:enablei BangPatterns
:enablei ViewPatterns
:enablei PatternSynonyms
:enablei NamedFieldPuns
:enablei KindSignatures
:enablei DeriveFunctor
:enablei DeriveFoldable
:enablei DeriveTraversable
:enablei DeriveDataTypeable
:enablei DeriveGeneric
:enablei FlexibleContexts
:enablei FlexibleInstances
:enablei ConstraintKinds
:enablei ScopedTypeVariables
--------------------------------------------------------------------------------
---- Set the prompts -----------------------------------------------------------
:set prompt "\ESC[1;34m\STXλ> \ESC[0m\STX"
:set prompt2 "\ESC[1;34m\STX~> \ESC[0m\STX"
--------------------------------------------------------------------------------
---- Locally-defined commands should be `:undef`ed here ------------------------
:undef add-help
--------------------------------------------------------------------------------
---- Commented out code goes here ----------------------------------------------
-- import IPPrint.Colored
-- :set -interactive-print=IPPrint.Colored.cpprint
-- :def cp (\_ -> pure ":set -interactive-print=IPPrint.Colored.cpprint")
-- :def ncp (\_ -> pure ":set -interactive-print=print")
--------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment