Tag Archives: zsh

Cabal sandbox status in your ZSH prompt

Sometime ago I made a simple script for my zsh setup that allows me to see whether am I in a cabalized sandbox environment or not. I posted it to Haskell-cafe a month ago, but totally forgot to post it to this blog.

The result of checking for the sandbox is cached, which is probably unnecessary; it updates only when the user performs a cabal command or changes a directory.


#
# This simple script displays whether you are in a cabal sandbox
# the the result of checking for a sandbox is cached, but now that I
# actually think about it, it was probably an unnecessary step.
# Don't forget to customize the PROMPT variable at the bottom
#
# Looks like this with my prompt: https://files.app.net/rjphjAG9.png
#
function update_cabal_sandbox_info () {
if [[ -a cabal.sandbox.config ]]
then
export __CABAL_SANDBOX=" sandboxed"
else
export __CABAL_SANDBOX=
fi
export __CABAL_SANDBOX_VARS_INVALID=
}
function get_cabal_sandbox_info () {
test -n "$__CABAL_SANDBOX_VARS_INVALID" && update_cabal_sandbox_info
echo $__CABAL_SANDBOX
}
typeset -ga preexec_functions
typeset -ga precmd_functions
typeset -ga chpwd_functions
setopt prompt_subst
export __CABAL_SANDBOX=
export __CABAL_SANDBOX_VARS_INVALID=1
chpwd_functions+='update_cabal_sandbox_chpwd'
update_cabal_sandbox_chpwd () {
export __CABAL_SANDBOX_VARS_INVALID=1
}
preexec_functions+='update_cabal_sandbox_preexec'
update_cabal_sandbox_preexec () {
case "$(history $HISTCMD)" in
*cabal*) export __CABAL_SANDBOX_VARS_INVALID=1 ;;
esac
}
SLR_DARKSEA=%{$fg_bold[green]%}
sandbox_prompt () {
test -n $__CABAL_SANDBOX && echo "$SLR_DARKSEA$(get_cabal_sandbox_info)${reset_color}"
}
PROMPT="$(sandbox_prompt) , %~ \n"

view raw

.zshrc

hosted with ❤ by GitHub

After you place the contents of the script in your .zshrc file (or in a similar location), you should update your $PROMPT to use $(sandbox_prompt). The prompt I am using, by the way, is

HOST_COLOR=%{$fg_bold[yellow]%}
local ret_status="%(?:%{$fg_bold[green]%}:%{$fg_bold[red]%})%?%{$reset_color%}"
PROMPT=$'\n$(ssh_connection)$HOST_COLOR%n@%m%{$reset_color%}$(my_git_prompt)$(sandbox_prompt) : %~\n[${ret_status}] %# '

and it is based on the oh-my-solarized theme.