Skip to content

bash completion breaks exit status display in prompt #44

Closed
grosser opened this Issue Jan 14, 2012 · 7 comments

3 participants

@grosser
grosser commented Jan 14, 2012

I have a prompt that displays red if the last exit status was a failure,
this no longer works if z.sh bash completion is loaded (the lower part of z.sh)

function prompt_func() {
  previous_return_value=$?;
  PS1="${previous_return_value}"
}

PROMPT_COMMAND=prompt_func
@rupa
Owner
rupa commented Feb 11, 2012

I'm not sure how the tab completion would break it.

I suppose it's possible that the tab completion is setting $? when you actually tab complete and that could lead to weird values, but it shouldn't mess with other commands.

It seems more likely that z and your function aren't playing together well in the PROMPT_COMMAND. Can you provide the output of echo $PROMPT_COMMAND?

@grosser
grosser commented Feb 12, 2012

If the PROMT_COMMAND is defined before z.sh the exit status is always 0,
if PROMT_COMMAND is defined after z.sh it works normally.
It seems like the usage of the PROMT_COMMAND in z.sh could have something to do with it...

@cowboy
cowboy commented Jul 3, 2012

I have this same issue. Let me try to better articulate it. My current prompt should look something like this:

function prompt_command() {
  local exit_code=$?

  # Manually load z here, after $? is checked, to keep $? from being clobbered.
  _z --add "$(pwd -P 2>/dev/null)" 2>/dev/null

  # (Tons of formatting stuff removed here)
  PS1="$exit_code \$ "
}

PROMPT_COMMAND="prompt_command"

Because z.sh greps $PROMPT_COMMAND looking for _z --add and doesn't find it—because that string isn't contained in the variable, but is instead contained in the function the variable references—it modifies $PROMPT_COMMAND to look like this:

_z --add "$(pwd -P 2>/dev/null)" 2>/dev/null;prompt_command

As a result, in the prompt_command function, $exit_code is always set to the exit code of the _z function, not the command last run in the shell.

It would be great if there was a way to tell z to not do this. The way I've hacked around this right now is to do this:

function prompt_command() {
  local exit_code=$?

  # Manually load z here, after $? is checked, to keep $? from being clobbered.
  _z --add "$(pwd -P 2>/dev/null)" 2>/dev/null

  # (Tons of formatting stuff removed here)
  PS1="$exit_code \$ "
}

# The " || _z -- add" part is a hack to keep z from modifying $PROMPT_COMMAND,
# and will never actually be executed.
PROMPT_COMMAND="prompt_command || _z --add"
@rupa
Owner
rupa commented Jul 3, 2012

This makes sense but I don't see any way to deal with this in code. I could have it respect some environment variable, like $_Z_IVE_SET_PROMPT_COMMAND_MANUALLY, but seems like thats not any easier than just commenting out the relevant lines (188-189) in z.sh.

@cowboy
cowboy commented Jul 3, 2012

I'd much rather set a var than have to edit third-party code.

@rupa
Owner
rupa commented Jul 3, 2012

Fair enough, I'll push a change in a bit for you :)

@rupa rupa added a commit that closed this issue Jul 3, 2012
@rupa respect _Z_NO_PROMPT_COMMAND environment variable
For those who wish to set their PROMPT_COMMAND manually. Closes #44.
8272e6f
@rupa rupa closed this in 8272e6f Jul 3, 2012
@cowboy
cowboy commented Jul 3, 2012

Thanks! FWIW, I've posted the bash scripts where I'm using this, in case any others want to see my approach.

  1. setting environment vars, sourcing z.sh
  2. running the _z function in my prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.