Skip to content

Instantly share code, notes, and snippets.

@epsilonhalbe
Last active June 18, 2017 20:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save epsilonhalbe/e9af1d8dccc654ebd641 to your computer and use it in GitHub Desktop.
Save epsilonhalbe/e9af1d8dccc654ebd641 to your computer and use it in GitHub Desktop.
haskell-prompt-info (zsh)
function haskell_info() {
cabal_files=(*.cabal(N))
if [ $#cabal_files -gt 0 ]; then
if [ -f cabal.sandbox.config ]; then
cabal_sandbox_info
elif [ -f stack.yaml ]; then
stack_info
else
echo "%{$fg[red]%}no stack/sandbox%{$reset_color%}"
fi
fi
}
function stack_info() {
ghc_version=`ghc --version | rev | cut -d' ' -f 1 | rev`
resolver_yaml=`cat stack.yaml | grep resolver | cut -d' ' -f 2`
if [ -d ".stack-work/install/x86_64-linux/$resolver_yaml" ]; then
ghc_stack=`ls .stack-work/install/x86_64-linux/$resolver_yaml/ | grep $ghc_version`
if [ $ghc_stack ]; then
echo "[%{$fg[green]%}$resolver_yaml %{$reset_color%}| %{$fg[green]%}$ghc_version%{$reset_color%}]"
else
ghc_stack=`ls .stack-work/install/x86_64-linux/$resolver_yaml/ | xargs`
echo "[%{$fg[green]%}$resolver_yaml%{$reset_color%}|%{$fg[red]%}$ghc_stack%{$reset_color%}]"
fi
else
echo "%{$fg[red]%}[$resolver_yaml missing please do a 'stack build']%{$reset_color%}"
fi
}
function cabal_sandbox_info() {
ghc_version=`ghc --version | rev | cut -d' ' -f 1 | rev`
ghc_loc=`ls .cabal-sandbox | grep ghc | cut -d'-' -f 4 | grep $ghc_version`
if [ $ghc_loc ]; then
echo "[%{$fg[green]%}$ghc_version%{$reset_color%}]"
else
ghc_version=`ls .cabal-sandbox | grep ghc | cut -d'-' -f 4 | xargs`
echo "[%{$fg[red]%}$ghc_version%{$reset_color%}]"
fi
}
RPROMPT="\$(haskell_info) $RPROMPT"
@zimurgh
Copy link

zimurgh commented Aug 15, 2015

For me, RPROMPT should be just RPROPMT=$(haskell_info) so it prints and doesn't recursively call itself upon future sourcing of the .zshrc file(s).

Otherwise, it works for me. Good work!

@epsilonhalbe
Copy link
Author

cabal sandboxes are indicated by a ghc version liked [7.10.2]

  • green ghc version indicates that there is a cabal project, and there exists a directory in the sandbox with libraries built with the current ghc (in the path)
  • a list of red ghc versions indicates cabal projects, which would be available

for stack projects the indicator is like: [rts-3.0|7.10.0]

  • green resolver info and green ghc version indicate a proper stack project
  • red resolver info indicate that the current resolver is not initialized

the default choice if both cabal-sandbox and a stack project are coexisting is cabal for stack is quite new and there are more cabal projects out there

Note: stack itself is a moving target and changes in stack might lead to a breakage in this script
Note: this setup has been made on a linux machine - if you are working on a mac/windows it might need some adjustments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment