Skip to content

Instantly share code, notes, and snippets.

@nasyxx
Last active November 13, 2019 05:42
Show Gist options
  • Save nasyxx/84cff2eacd60fed659b79af81b2018c8 to your computer and use it in GitHub Desktop.
Save nasyxx/84cff2eacd60fed659b79af81b2018c8 to your computer and use it in GitHub Desktop.
ghci configuration
-- ghci configuration
-- REQUIRES
-- pretty-simple
-- To use this configuration, `cat` lines inside `{--}`, to a file named ghci-color in your $PATH,
-- Run with `ghci-color`, and for which if you use stack, `stack exec -- ghci-color` is suitable.
-- Also, you could set an alias like `alias ghcrepl=stack exec -- ghci-color`, then run with `ghcrepl`.
:{
{-
#!/usr/bin/env bash
# borrowed from https://github.com/rhysd/ghci-color
# with license
# Copyright (c) 2012 rhysd
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
# THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# I have changed some color. -- Nasy
GREEN=`echo -e '\033[0;32m'`
RED=`echo -e '\033[0;31m'`
CYAN=`echo -e '\033[0;36m'`
BLUE=`echo -e '\033[0;34m'`
YELLOW=`echo -e '\033[1;33m'`
PURPLE=`echo -e '\033[0;35m'`
RESET=`echo -e '\033[0m'`
load_failed="s/^Failed, modules loaded:/$RED&$RESET/;"
load_done="s/done./$GREEN&$RESET/g;"
double_colon="s/::/$PURPLE&$RESET/g;"
right_arrow="s/\->/$PURPLE&$RESET/g;"
right_arrow2="s/=>/$PURPLE&$RESET/g;"
calc_operators="s/[+\-\/*]/$GREEN&$RESET/g;"
char="s/'\\?.'/$RED&$RESET/g;"
string="s/\"[^\"]*\"/$YELLOW&$RESET/g;"
parenthesis="s/[{}()]/$BLUE&$RESET/g;"
left_blacket="s/\[\([^09]\)/$BLUE[$RESET\1/g;"
right_blacket="s/\]/$BLUE&$RESET/g;"
no_instance="s/^\s*No instance/$RED&$RESET/g;"
interactive="s/^<[^>]*>/$RED&$RESET/g;"
exec "`which ghc`" --interactive ${1+"$@"} 2>&1 |\
sed "$load_failed\
$load_done\
$no_instance\
$interactive\
$double_colon\
$right_arrow\
$right_arrow2\
$parenthesis\
$left_blacket\
$right_blacket\
$double_colon\
$calc_operators\
$string\
$char"
-}
:}
-- 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
-- Collect type/location info after loading modules
:set +c
-- Force load dependencies
:set -package pretty-simple
-- Show the types of evaluated expressions
:set +t
:set -fprint-explicit-foralls
-- And stats
:set +s
-- Enable multi-line expressions with :{ and :}
:set +m
-- Give us a `hoogle` function to if the hoogle binary is available.
:def hoogle \s -> return $ ":! hoogle --count=15 \"" ++ s ++ "\""
-- Some options
:set -Wall
:set -Wno-type-defaults
:set -fdefer-type-errors
-- Make the prompt a little more colorful.
-- And pretty-printing values
-- :set -interactive-print=Text.Pretty.Simple.pPrint -- if you do not want color with ghci-color script.
:set -interactive-print=Text.Pretty.Simple.pPrintNoColor
:set prompt "\n\STX\ESC[1;33m\STX%u·\ESC[1;31m\STX%A·\ESC[1;36m\STX%V·\ESC[1;35m\STX%w\ESC[1;33m\STX\n\STX%s\ESC[1;32m\STX¬\n\ESC[1;32m\STXλ> \ESC[m\STX"
:set prompt-cont " | "
-- Hlint
-- Begin copied material.
-- <http://www.cs.kent.ac.uk/people/staff/cr3/toolbox/haskell/dot-squashed.ghci641>
:{
:def redir \varcmd -> return $
case break Data.Char.isSpace varcmd of
(var,_:cmd) -> unlines
[":set -fno-print-bind-result"
,"tmp <- System.Directory.getTemporaryDirectory"
,"(f,h) <- System.IO.openTempFile tmp \"ghci\""
,"sto <- GHC.IO.Handle.hDuplicate System.IO.stdout"
,"GHC.IO.Handle.hDuplicateTo h System.IO.stdout"
,"System.IO.hClose h"
,cmd
,"GHC.IO.Handle.hDuplicateTo sto System.IO.stdout"
,"let readFileNow f = readFile f >>= \\t->Data.List.length t `seq` return t"
,var++" <- readFileNow f"
,"System.Directory.removeFile f"
]
_ -> "putStrLn \"usage: :redir <var> <cmd>\""
:}
--- Integration with the hlint code style tool
:{
:def hlint \extra -> return $ unlines
[":unset +t +s"
,":set -w"
,":redir hlintvar1 :show modules"
,":cmd return $ \":! hlint \" ++ unwords (map (takeWhile (/=',') . drop 2 . dropWhile (/= '(')) $ lines hlintvar1) ++ \" \" ++ " ++ show extra
,":set +t +s -Wall"
]
:}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment