Skip to content

Commit

Permalink
refactor prompt, work around pyenv perf nightmares
Browse files Browse the repository at this point in the history
  • Loading branch information
ExpHP committed Jul 25, 2021
1 parent d5e125f commit c5e63fe
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 39 deletions.
47 changes: 28 additions & 19 deletions shell/bashrc.include
Expand Up @@ -2,10 +2,18 @@

# To be source'd in .bashrc

# running 'which' can actually get pretty expensive (especially on wsl2) so we cache these
which git 1>/dev/null 2>/dev/null && __EXPHP_BASH_PROMPT__HAS_GIT=1

# FIXME: this currently always fails due to initialization order (common.include runs after this)
which pyenv 1>/dev/null 2>/dev/null && __EXPHP_BASH_PROMPT__HAS_PYENV=1

# Wrap in a function so that `local` can be used.
# Awkwardly long name to avoid collision.
doTheSpecialPromptyThing() {( # scope the colors
. $HOME/dotfiles/shell/colors.include
__exphpBashrcPromptCommand() {
local __PREV_EXIT_CODE="$?" # This needs to be first

. $HOME/dotfiles/shell/colors.ps1.include --local

local lbrace="$white["
local rbrace="$white]"
Expand All @@ -25,10 +33,15 @@ doTheSpecialPromptyThing() {( # scope the colors
dd-win10\
; do [[ $HOSTNAME == $tmp ]] && my_machine=1; done

# == STATIC REGION ==
# no command substitutions may be used before the error indicator,
# to ensure that the value of $? is preserved.
# == RESET ==
PS1=""

# == PYENV ==
if [[ $__EXPHP_BASH_PROMPT__HAS_PYENV ]]; then
PS1+="$($HOME/dotfiles/shell/bashrc.prompt.d/pyenv)"
fi

# == MAIN STUFF ==
PS1+="$lbrace "
if [[ $my_machine ]]; then
# Username & Time
Expand All @@ -46,29 +59,25 @@ doTheSpecialPromptyThing() {( # scope the colors
PS1+=" $rbrace "

# == ERROR INDICATOR ==
PS1+="\$(if [[ \$? != 0 ]];"
PS1+="then echo \"($red!!!$white) \"; "
PS1+="fi)"
if [[ $__PREV_EXIT_CODE -ne 0 ]]; then
PS1+="($red!!!$white) "
fi

# == DYNAMIC REGION ==
PS1+="\$(
\$HOME/dotfiles/shell/bashrc.prompt.d/pyenv
\$HOME/dotfiles/shell/bashrc.prompt.d/git
)"
# Git branch
if [[ $__EXPHP_BASH_PROMPT__HAS_GIT ]]; then
PS1+="$($HOME/dotfiles/shell/bashrc.prompt.d/git)"
fi

# Current directory
PS1+="$green\w$none"

# Prompt on newline
PS1+="\n"
PS1+="${white}\\\$${none} "
echo "$PS1" # let it escape the subshell
)}

PS1=$(doTheSpecialPromptyThing)
}

# cleanup
unset -f doTheSpecialPromptyThing
PROMPT_COMMAND=__exphpBashrcPromptCommand

__bashrc__is_bash_ge() {
local maj
Expand Down Expand Up @@ -105,5 +114,5 @@ else

# Add to the history after each command finishes
NEWLINE=$'\n'
PROMPT_COMMAND="history -a $NEWLINE $PROMPT_COMMAND"
PROMPT_COMMAND="$PROMPT_COMMAND $NEWLINE history -a"
fi
3 changes: 1 addition & 2 deletions shell/bashrc.prompt.d/git
@@ -1,7 +1,6 @@
#!/usr/bin/env bash
which git 1>/dev/null 2>/dev/null || exit 0

. $HOME/dotfiles/shell/colors.include
. $HOME/dotfiles/shell/colors.ps1.include
. $HOME/dotfiles/shell/gittool.ps1.include # compute git status into GITTOOL variables
if [[ $EXPHP_GITTOOL_BRANCH ]]; then
echo -n "$none("
Expand Down
13 changes: 7 additions & 6 deletions shell/bashrc.prompt.d/pyenv
@@ -1,10 +1,11 @@
#!/usr/bin/env bash

which pyenv 1>/dev/null 2>/dev/null || exit 0
. $HOME/dotfiles/shell/colors.ps1.include

. $HOME/dotfiles/shell/colors.include

version=$(pyenv version-name)
if [[ "$version" != 'system' ]]; then
echo -n "(${cyan}$version${none}${bold}) ${none}"
# DO NOT USE 'pyenv version-name'
# IT IS SO SLOW
# YOU WILL MAKE YOUR PROMPT SO SLOW
version=$PYENV_VERSION
if [[ $version && "$version" != 'system' ]]; then
echo -n "(${cyan}$version${none}) ${none}"
fi
12 changes: 0 additions & 12 deletions shell/colors.include

This file was deleted.

22 changes: 22 additions & 0 deletions shell/colors.ps1.include
@@ -0,0 +1,22 @@
if [[ $1 == "--local" ]]; then
# Note: The \[ and \] are PS1 stuff to fix the positioning when using escape codes.
local none='\['$'\033[00m''\]'
local bold='\['$'\033[01m''\]'
local red='\['$'\033[01;31m''\]'
local green='\['$'\033[01;32m''\]'
local yellow='\['$'\033[01;33m''\]'
local blue='\['$'\033[01;34m''\]'
local purple='\['$'\033[01;35m''\]'
local cyan='\['$'\033[01;36m''\]'
local white='\['$'\033[01;37m''\]'
else
none='\['$'\033[00m''\]'
bold='\['$'\033[01m''\]'
red='\['$'\033[01;31m''\]'
green='\['$'\033[01;32m''\]'
yellow='\['$'\033[01;33m''\]'
blue='\['$'\033[01;34m''\]'
purple='\['$'\033[01;35m''\]'
cyan='\['$'\033[01;36m''\]'
white='\['$'\033[01;37m''\]'
fi
15 changes: 15 additions & 0 deletions shell/common.include
Expand Up @@ -173,8 +173,23 @@ if [ -e $HOME/asd/manage/pyenv ]; then
# pyenv startup part 2: The part that actually belongs in .bashrc
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

# _pyenv_virtualenv_hook makes the shell absurdly slow so get rid of it
# https://github.com/pyenv/pyenv-virtualenv/issues/259
#
# Warning: unloading the following hook breaks command
# `pyenv activate/deactivate`. Please switch to
# `pyenv shell env_name`, `pyenv shell --unset` instead.
if [[ -n $ZSH_VERSION ]]; then
autoload -Uz add-zsh-hook
add-zsh-hook -D precmd _pyenv_virtualenv_hook
fi
if [[ -n $BASH_VERSION ]]; then
PROMPT_COMMAND="${PROMPT_COMMAND/_pyenv_virtualenv_hook;/}"
fi
fi


# # Bumper rail.
# # Limit memory for interactive bash prompts, because thrashing sucks.
# # To bypass, invoke 'nolimit' from *outside* of a bash process.
Expand Down

0 comments on commit c5e63fe

Please sign in to comment.