Skip to content

Commit 38376d5

Browse files
committed
lib: provide a prompt function for the dirstack
Providing this additional function for prompts allows anyone using pushd/popd/dirs to navigate directory trees to quickly view their stack and find which entry they want to switch to. This degrades gracefully for anyone not using the directory stack since dirs will always display the same as \w when the stack is only one entry deep. Setting DIRSTACK_EXPAND_TILDE to true in the environment will display the directory stack with ~ expanded to the full path of the user's home directory. Setting DIRSTACK_LIMIT to any value greater than 0 will limit the display to that number of entries, though the directory stack will still contain all entries in it. The bakke theme has been tweaked to use the new function as an example. Signed-off-by: Joe MacDonald <[email protected]>
1 parent 1c7f6d6 commit 38376d5

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/omb-prompt-base.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ SCM_SVN_CHAR='⑆'
5252
SCM_NONE='NONE'
5353
SCM_NONE_CHAR=''
5454

55+
DIRSTACK_EXPAND_TILDE=${DIRSTACK_EXPAND_TILDE:=false}
56+
DIRSTACK_LIMIT=${DIRSTACK_LIMIT:=0}
57+
5558
THEME_SHOW_USER_HOST=${THEME_SHOW_USER_HOST:=false}
5659
USER_HOST_THEME_PROMPT_PREFIX=''
5760
USER_HOST_THEME_PROMPT_SUFFIX=''
@@ -585,6 +588,29 @@ function aws_profile {
585588
fi
586589
}
587590

591+
function _omb_dirstack_limit {
592+
if [[ "${DIRSTACK_EXPAND_TILDE}" = true ]]; then
593+
dirargs="-l"
594+
fi
595+
if [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then
596+
IFS=$'\n' read -r -d '' -a DIRS < <( dirs -p ${dirargs} && printf '\0' )
597+
else
598+
readarray -t DIRS < <(dirs -p ${dirargs})
599+
fi
600+
echo "${DIRS[@]:0:${DIRSTACK_LIMIT}}"
601+
}
602+
603+
function dirs_prompt {
604+
if [[ "${DIRSTACK_LIMIT}" -gt 0 ]]; then
605+
_omb_dirstack_limit
606+
else
607+
if [[ "${DIRSTACK_EXPAND_TILDE}" = true ]]; then
608+
dirs -l
609+
else
610+
dirs
611+
fi
612+
fi
613+
}
588614

589615
# Returns true if $1 is a shell function.
590616
_omb_deprecate_function 20000 fn_exists _omb_util_function_exists

themes/bakke/bakke.theme.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function _omb_theme_PROMPT_COMMAND() {
1616
#PS1="${_omb_prompt_bold_teal}$(scm_char)${_omb_prompt_green}$(scm_prompt_info)${_omb_prompt_purple}$(_omb_prompt_print_ruby_env) ${_omb_prompt_olive}\h ${_omb_prompt_reset_color}in ${_omb_prompt_green}\w ${_omb_prompt_reset_color}\n${_omb_prompt_green}→${_omb_prompt_reset_color} "
1717
#PS1="\n${_omb_prompt_purple}\h: ${_omb_prompt_reset_color} ${_omb_prompt_green}\w\n${_omb_prompt_bold_teal}$(scm_char)${_omb_prompt_green}$(scm_prompt_info) ${_omb_prompt_green}→${_omb_prompt_reset_color} "
1818
#PS1="\n${_omb_prompt_teal}\h: ${_omb_prompt_reset_color} ${_omb_prompt_olive}\w\n${_omb_prompt_brown}$(scm_char)${_omb_prompt_brown}$(scm_prompt_info) ${_omb_prompt_green}→${_omb_prompt_reset_color} "
19-
PS1="\n${_omb_prompt_teal}\h: ${_omb_prompt_reset_color} ${_omb_prompt_olive}\w ${_omb_prompt_green}$(scm_prompt_info)\n${_omb_prompt_reset_color}"
19+
PS1="\n${_omb_prompt_teal}\h: ${_omb_prompt_reset_color} ${_omb_prompt_olive}$(dirs_prompt) ${_omb_prompt_green}$(scm_prompt_info)\n${_omb_prompt_reset_color}"
2020
}
2121

2222
_omb_util_add_prompt_command _omb_theme_PROMPT_COMMAND

0 commit comments

Comments
 (0)