Skip to content

DRAFT: Update themes to use new Automatic History Management #1951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
19 changes: 19 additions & 0 deletions themes/barbuk/barbuk.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,25 @@ RBENV_THEME_PROMPT_SUFFIX=''
RBFU_THEME_PROMPT_PREFIX=''
RBFU_THEME_PROMPT_SUFFIX=''

SCM_GIT_SHOW_CURRENT_USER='false'
NVM_THEME_PROMPT_PREFIX=''
NVM_THEME_PROMPT_SUFFIX=''
RVM_THEME_PROMPT_PREFIX=''
RVM_THEME_PROMPT_SUFFIX=''
RBENV_THEME_PROMPT_PREFIX=' '
RBENV_THEME_PROMPT_SUFFIX=''
RBFU_THEME_PROMPT_PREFIX=''
RBFU_THEME_PROMPT_SUFFIX=''
case $HISTCONTROL in
*'auto'*)
: # Do nothing, already configured.
;;
*)
# Append new history lines to history file
HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autosave"
;;
esac

function _git-uptream-remote-logo() {
[[ -z "$(_git-upstream)" ]] && SCM_GIT_CHAR="${SCM_GIT_CHAR_DEFAULT:-}"

Expand Down
13 changes: 12 additions & 1 deletion themes/codeword/codeword.theme.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# shellcheck shell=bash
# shellcheck disable=SC2034 # Expected behavior for themes.

case $HISTCONTROL in
*'auto'*)
: # Do nothing, already configured.
;;
*)
# Append new history lines to history file
HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autosave"
;;
esac
safe_append_preexec '_bash-it-history-auto-load'
safe_append_prompt_command '_bash-it-history-auto-save'
Comment on lines +13 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compared to _bash-it-history-init (in lib/history.bash), the commands for preexec and prompt_command are reversed.

function _bash-it-history-init() {
safe_append_preexec '_bash-it-history-auto-save'
safe_append_prompt_command '_bash-it-history-auto-load'
}

Thinking of the functions that auto-load and auto-save perform, I think the "save" should be performed before running the command, and the "load" should be performed before the prompt command.


In addition, why don't they simply call _bash-it-history-init?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it seems _bash-it-history-init is anyway forcibly loaded at the end of bash_it.sh through _bash_it_library_finalize_hook.

_bash_it_library_finalize_hook+=('_bash-it-history-init')

One possibility is that the above "reversed hooks" are intended to complement load/save that haven't been registered by _bash-it-history-init to perform both load/save in both preexec and prompt_command. However, I don't see the reason for running both twice before running the command and after running the command. The default hooks set up by _bash-it-history-init seem sufficient to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion is to remove all safe_append_preexec '_bash-it-history-auto-load' and safe_append_prompt_command '_bash-it-history-auto-save' from this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in other words, it really needs to be redone. I'll dump it.


SCM_THEME_PROMPT_PREFIX="${SCM_THEME_PROMPT_SUFFIX:-}"
SCM_THEME_PROMPT_DIRTY="${bold_red?} ✗${normal?}"
SCM_THEME_PROMPT_CLEAN="${bold_green?} ✓${normal?}"
Expand All @@ -22,5 +34,4 @@ function prompt() {
PS1="$(user_host_path_prompt)$(virtualenv_prompt)$(scm_prompt) $(mark_prompt) "
}

safe_append_prompt_command '_save-and-reload-history 1'
safe_append_prompt_command prompt
16 changes: 13 additions & 3 deletions themes/doubletime/doubletime.theme.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# shellcheck shell=bash
# shellcheck disable=SC2034,SC2154

case $HISTCONTROL in
*'auto'*)
: # Do nothing, already configured.
;;
*)
# Append new history lines to history file
HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autosave"
;;
esac
safe_append_preexec '_bash-it-history-auto-load'
safe_append_prompt_command '_bash-it-history-auto-save'
Comment on lines +4 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, there are too many repetitions of this code section. These lines should be factorized into an independent function (defined in e.g. lib/history.bash), and I think each theme should contain a single line of the call to the function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed.


SCM_THEME_PROMPT_DIRTY=''
SCM_THEME_PROMPT_CLEAN=''
SCM_GIT_CHAR="${bold_cyan}±${normal}"
Expand Down Expand Up @@ -30,9 +42,7 @@ else
fi

function prompt_setter() {
# Save history
_save-and-reload-history 1
PS1="
PS1="
$(clock_prompt) $(scm_char) [${THEME_PROMPT_HOST_COLOR}\u@${THEME_PROMPT_HOST}$reset_color] $(virtualenv_prompt)$(ruby_version_prompt)\w
$(scm_prompt)$reset_color $ "
PS2='> '
Expand Down
18 changes: 14 additions & 4 deletions themes/doubletime_multiline/doubletime_multiline.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@

source "$BASH_IT/themes/doubletime/doubletime.theme.bash"

case $HISTCONTROL in
*'auto'*)
: # Do nothing, already configured.
;;
*)
# Append new history lines to history file
HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autosave"
;;
esac
safe_append_preexec '_bash-it-history-auto-load'
safe_append_prompt_command '_bash-it-history-auto-save'

function prompt_setter() {
# Save history
_save-and-reload-history 1
PS1="
$(clock_prompt) $(scm_char) [$THEME_PROMPT_HOST_COLOR\u@${THEME_PROMPT_HOST}${reset_color?}] $(virtualenv_prompt)$(ruby_version_prompt)
PS1="
$(clock_prompt) $(scm_char) [$THEME_PROMPT_HOST_COLOR\u@${THEME_PROMPT_HOST}$reset_color?] $(virtualenv_prompt)$(ruby_version_prompt)
\w
$(scm_prompt)$reset_color $ "
PS2='> '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@

source "$BASH_IT/themes/doubletime/doubletime.theme.bash"

case $HISTCONTROL in
*'auto'*)
: # Do nothing, already configured.
;;
*)
# Append new history lines to history file
HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autosave"
;;
esac
safe_append_preexec '_bash-it-history-auto-load'
safe_append_prompt_command '_bash-it-history-auto-save'

function prompt_setter() {
# Save history
_save-and-reload-history 1
PS1="
$(clock_prompt) $(scm_char) [$THEME_PROMPT_HOST_COLOR\u@${THEME_PROMPT_HOST}${reset_color?}] $(virtualenv_prompt)
PS1="
$(clock_prompt) $(scm_char) [$THEME_PROMPT_HOST_COLOR\u@${THEME_PROMPT_HOST}$reset_color?] $(virtualenv_prompt)
\w
$(scm_prompt)$reset_color $ "
PS2='> '
Expand Down
16 changes: 12 additions & 4 deletions themes/font/font.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ USER_HOST_THEME_PROMPT_SUFFIX=" "
VIRTUALENV_THEME_PROMPT_PREFIX='('
VIRTUALENV_THEME_PROMPT_SUFFIX=') '

case $HISTCONTROL in
*'auto'*)
: # Do nothing, already configured.
;;
*)
# Append new history lines to history file
HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autosave"
;;
esac

function prompt_command() {
# This needs to be first to save last command return code
local RC="$?"
Expand All @@ -55,10 +65,8 @@ function prompt_command() {
ret_status="${bold_red}"
fi

# Append new history lines to history file
history -a

PS1="$(clock_prompt)${virtualenv}$(user_host_prompt)${bold_cyan}\W $(scm_prompt_char_info)${ret_status}→ ${normal}"
PS1="$(clock_prompt)${virtualenv}$(user_host_prompt)${bold_cyan}\W $(scm_prompt_char_info)${ret_status}→ ${normal}"
}

save_append_preexec _bash-it-history-auto-save
safe_append_prompt_command prompt_command
18 changes: 7 additions & 11 deletions themes/nwinkler/nwinkler.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ function prompt_end() {
}

prompt_setter() {
local exit_status=$?
if [[ $exit_status -eq 0 ]]; then
PROMPT_END=$PROMPT_END_CLEAN
else
PROMPT_END=$PROMPT_END_DIRTY
fi
# Save history
_save-and-reload-history 1
PS1="($(clock_prompt)) $(scm_char) [${blue}\u${reset_color}@${green}\H${reset_color}] ${yellow}\w${reset_color}$(scm_prompt_info) ${reset_color}\n$(prompt_end) "
PS2='> '
PS4='+ '
local exit_status=$?
if [[ $exit_status -eq 0 ]]; then PROMPT_END=$PROMPT_END_CLEAN
else PROMPT_END=$PROMPT_END_DIRTY
fi
PS1="($(clock_prompt)) $(scm_char) [${blue}\u${reset_color}@${green}\H${reset_color}] ${yellow}\w${reset_color}$(scm_prompt_info) ${reset_color}\n$(prompt_end) "
PS2='> '
PS4='+ '
}

safe_append_prompt_command prompt_setter
Expand Down
30 changes: 19 additions & 11 deletions themes/nwinkler_random_colors/nwinkler_random_colors.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,27 @@ function prompt_end() {
}

prompt_setter() {
local exit_status=$?
if [[ $exit_status -eq 0 ]]; then
PROMPT_END=$PROMPT_END_CLEAN
else
PROMPT_END=$PROMPT_END_DIRTY
fi
# Save history
_save-and-reload-history 1
PS1="($(clock_prompt)${reset_color}) $(scm_char) [${USERNAME_COLOR}\u${reset_color}@${HOSTNAME_COLOR}\H${reset_color}] ${PATH_COLOR}\w${reset_color}$(scm_prompt_info) ${reset_color}\n$(prompt_end) "
PS2='> '
PS4='+ '
local exit_status=$?
if [[ $exit_status -eq 0 ]]; then PROMPT_END=$PROMPT_END_CLEAN
else PROMPT_END=$PROMPT_END_DIRTY
fi
PS1="($(clock_prompt)${reset_color}) $(scm_char) [${USERNAME_COLOR}\u${reset_color}@${HOSTNAME_COLOR}\H${reset_color}] ${PATH_COLOR}\w${reset_color}$(scm_prompt_info) ${reset_color}\n$(prompt_end) "
PS2='> '
PS4='+ '
}

case $HISTCONTROL in
*'auto'*)
: # Do nothing, already configured.
;;
*)
# Append new history lines to history file
HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autosave"
;;
esac
safe_append_preexec '_bash-it-history-auto-load'
safe_append_prompt_command '_bash-it-history-auto-save'

safe_append_prompt_command prompt_setter

SCM_THEME_PROMPT_DIRTY=" ${bold_red}✗${normal}"
Expand Down
10 changes: 9 additions & 1 deletion themes/pete/pete.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ function prompt_setter() {
scm_char="$(scm_char)"
scm_prompt_info="$(scm_prompt_info)"
ruby_version_prompt="$(ruby_version_prompt)"
_save-and-reload-history 1 # Save history
PS1="(${clock_prompt}) ${scm_char} [${blue?}\u${reset_color?}@${green?}\H${reset_color?}] ${yellow?}\w${reset_color?}${scm_prompt_info}${ruby_version_prompt} ${reset_color?} "
PS2='> '
PS4='+ '
}

case $HISTCONTROL in
*'auto'*)
: # Do nothing, already configured.
;;
*)
# Append new history lines to history file
HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autosave"
;;
esac
Copy link
Contributor

@akinomyoga akinomyoga May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hooks into preexec and prompt_command are missing.

All the themes touched in this PR seems to have called _save-and-reload-history in prompt_command, but auto-load/save are registered to preexec/prompt_command for some themes and not for other themes. What are the differences? Are they just oversights? Or is it intentional?

edit: Anyway, I suggest removing all these additional hooks as discussed in #1951 (comment).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question is do I keep fixing this or just close and bury it? Maybe it's just the wrong approach altogether.

safe_append_prompt_command prompt_setter

SCM_THEME_PROMPT_DIRTY=" ✗"
Expand Down
25 changes: 18 additions & 7 deletions themes/powerline-plain/powerline-plain.base.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# shellcheck shell=bash
. "$BASH_IT/themes/powerline/powerline.base.bash"

case $HISTCONTROL in
*'auto'*)
: # Do nothing, already configured.
;;
*)
# Append new history lines to history file
HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autosave"
;;
esac
safe_append_preexec '_bash-it-history-auto-load'
safe_append_prompt_command '_bash-it-history-auto-save'

function __powerline_left_segment {
local OLD_IFS="${IFS}"
IFS="|"
Expand Down Expand Up @@ -35,13 +48,11 @@ function __powerline_prompt_command {
LAST_SEGMENT_COLOR=""
PROMPT_AFTER="${POWERLINE_PROMPT_AFTER}"

_save-and-reload-history "${HISTORY_AUTOSAVE:-0}"

## left prompt ##
for segment in $POWERLINE_PROMPT; do
local info="$(__powerline_${segment}_prompt)"
[[ -n "${info}" ]] && __powerline_left_segment "${info}"
done
## left prompt ##
for segment in $POWERLINE_PROMPT; do
local info="$(__powerline_${segment}_prompt)"
[[ -n "${info}" ]] && __powerline_left_segment "${info}"
done

[[ "${last_status}" -ne 0 ]] && __powerline_left_segment $(__powerline_last_status_prompt ${last_status})

Expand Down
26 changes: 18 additions & 8 deletions themes/rainbowbrite/rainbowbrite.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
# ± ~/path/to (branch ✓) $
# in glorious red / blue / yellow color scheme

case $HISTCONTROL in
*'auto'*)
: # Do nothing, already configured.
;;
*)
# Append new history lines to history file
HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autosave"
;;
esac
safe_append_preexec '_bash-it-history-auto-load'
safe_append_prompt_command '_bash-it-history-auto-save'

prompt_setter() {
# Save history
_save-and-reload-history 1
# displays user@server in purple
# PS1="$red$(scm_char) $purple\u@\h$reset_color:$blue\w$yellow$(scm_prompt_info)$(ruby_version_prompt) $black\$$reset_color "
# no user@server
PS1="$red$(scm_char) $blue\w$yellow$(scm_prompt_info)$(ruby_version_prompt) $black\$$reset_color "
PS2='> '
PS4='+ '
# displays user@server in purple
# PS1="$red$(scm_char) $purple\u@\h$reset_color:$blue\w$yellow$(scm_prompt_info)$(ruby_version_prompt) $black\$$reset_color "
# no user@server
PS1="$red$(scm_char) $blue\w$yellow$(scm_prompt_info)$(ruby_version_prompt) $black\$$reset_color "
PS2='> '
PS4='+ '
}

safe_append_prompt_command prompt_setter
Expand Down
Loading