twolfson / sexy-bash-prompt

Bash prompt with colors, git statuses, and git branches.
MIT License
1.14k stars 154 forks source link

Do not duplicate `sexy_bash_prompt_command` in PROMPT_COMMAND #93

Closed Tropicananass closed 3 years ago

Tropicananass commented 3 years ago

Hi, When sourcing a second time my bashrc, PROMPT_COMMAND value is "sexy_bash_prompt_command ; sexy_bash_prompt_command ; others_stuffs". The second call of sexy_bash_prompt_command reset sexy_bash_prompt_exit_code to 0. Grepping the function name in PROMPT_COMMAND is used to avoid this.

Have a good day !

rpdelaney commented 3 years ago

Why do you need to source the prompt twice? I'm not sure idempotence was ever an explicit requirement before.

twolfson commented 3 years ago

I've got the same curiousity as @rpdelaney here o_o

Additionally, as a heads up, the grep check won't guarantee idempotence:

A more robust fix would be a global boolean (e.g. sexy_bash_prompt_command_has_run=1)

(don't do this yet, mostly pointing out for awareness)


For reference, here's how I extend my PROMPT_COMMAND:

https://github.com/twolfson/dotfiles/blob/15df11a59b7a378e38cdba2dbcca78e0e3266bc3/.bashrc#L409-L427

Tropicananass commented 3 years ago

Hi, I don't need to source the prompt twice, but my .bashrc source it and it also source files from others projects that I need to reload often. I just wanted to share an improvement I use but I realize that this solution is not the best may be I should have created an issue before 😓. A better solution may be to check if prompt has been sourced already before sourcing it in .bashrc. What do you think ?

Anyway, thanks a lot for this piece of code !!

twolfson commented 3 years ago

Heh, no worries about creating an issue before. It more would save your time than ours =/

If the only script loading sexy-bash-prompt's .bash_prompt file is your .bashrc and your .bashrc needs to be re-sourced often, then it sounds like the idempotence should live in the .bashrc

Example:

if test -z "${BASHRC_HAS_RUN}"; then
  . ~/.bash_prompt
fi

# End of file
BASHRC_HAS_RUN=1

This will also add-in nicely with other places you should have idempotence (e.g. sourcing other files, adding to $PATH variable)

I will also note that you're mostly making more work for yourself here. Usually when I change my .bashrc, I don't re-source it. I'll close the tab and re-open a new one -- thus avoiding needing to source + verify the command worked (and also fewer keystrokes)

Closing PR as wontfix for now