Skip to content
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

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

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ completion/available/wpscan.completion.bash
# libraries
lib/colors.bash
lib/helpers.bash
lib/history.bash
lib/log.bash
lib/preexec.bash
lib/search.bash
Expand Down
49 changes: 49 additions & 0 deletions lib/history.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# shellcheck shell=bash
#
# Functions for working with Bash's command history.

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

function _bash-it-history-auto-save() {
case $HISTCONTROL in
*'noauto'* | *'autoload'*)
: # Do nothing, as configured.
;;
*'auto'*)
# Append new history from this session to the $HISTFILE
history -a
;;
*)
# Append *only* if shell option `histappend` has been enabled.
shopt -q histappend && history -a && return
;;
esac
}

function _bash-it-history-auto-load() {
case $HISTCONTROL in
*'noauto'*)
: # Do nothing, as configured.
;;
*'autosave'*)
# Append new history from this session to the $HISTFILE
history -a
;;
*'autoloadnew'*)
# Read new entries from $HISTFILE
history -n
;;
*'auto'*)
# Blank in-memory history, then read entire $HISTFILE fresh from disk.
history -a && history -c && history -r
;;
*)
: # Do nothing, default.
;;
esac
}

_bash_it_library_finalize_hook+=('_bash-it-history-init')
12 changes: 7 additions & 5 deletions plugins/available/history-eternal.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# shellcheck shell=bash
about-plugin 'eternal bash history'

# Load after the history plugin
# BASH_IT_LOAD_PRIORITY: 375
if [[ ${BASH_VERSINFO[0]} -lt 4 ]] || [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 3 ]]; then
_log_warning "Bash version 4.3 introduced the 'unlimited' history size capability."
return 1
fi

# Modify history sizes before changing location to avoid unintentionally
# truncating the history file early.

# "Numeric values less than zero result in every command being saved on the history list (there is no limit)"
export HISTSIZE=-1
readonly HISTSIZE=-1 2> /dev/null || true

# "Non-numeric values and numeric values less than zero inhibit truncation"
export HISTFILESIZE='unlimited'
readonly HISTFILESIZE='unlimited' 2> /dev/null || true

# Use a custom history file location so history is not truncated
# if the environment ever loses this "eternal" configuration.
HISTDIR="${XDG_STATE_HOME:-${HOME?}/.local/state}/bash"
[[ -d ${HISTDIR?} ]] || mkdir -p "${HISTDIR?}"
export HISTFILE="${HISTDIR?}/history"
readonly HISTFILE="${HISTDIR?}/history" 2> /dev/null || true
3 changes: 0 additions & 3 deletions plugins/available/history-search.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# shellcheck shell=bash
about-plugin 'search history using the prefix already entered'

# Load after the history plugin
# BASH_IT_LOAD_PRIORITY: 375

# enter a few characters and press UpArrow/DownArrow
# to search backwards/forwards through the history
if [[ ${SHELLOPTS} =~ (vi|emacs) ]]; then
Expand Down
3 changes: 0 additions & 3 deletions plugins/available/history-substring-search.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# shellcheck shell=bash
about-plugin 'search history using the substring already entered'

# Load after the history plugin
# BASH_IT_LOAD_PRIORITY: 375

# enter a few characters and press UpArrow/DownArrow
# to search backwards/forwards through the history
if [[ ${SHELLOPTS} =~ (vi|emacs) ]]; then
Expand Down
11 changes: 5 additions & 6 deletions plugins/available/history.plugin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ about-plugin 'improve history handling with sane defaults'
# variable when the shell exits, rather than overwriting the file.
shopt -s histappend

# erase duplicates; alternative option: export HISTCONTROL=ignoredups
export HISTCONTROL=${HISTCONTROL:-ignorespace:erasedups}
# 'ignorespace': don't save command lines which begin with a space to history
# 'erasedups' (alternative 'ignoredups'): don't save duplicates to history
# 'autoshare': automatically share history between multiple running shells
: "${HISTCONTROL:=ignorespace:erasedups:autoshare}"

# resize history to 100x the default (500)
export HISTSIZE=${HISTSIZE:-50000}

# Flush history to disk after each command.
export PROMPT_COMMAND="history -a;${PROMPT_COMMAND}"
: "${HISTSIZE:=50000}"

function top-history() {
about 'print the name and count of the most commonly run tools'
Expand Down
13 changes: 11 additions & 2 deletions themes/barbuk/barbuk.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ SCM_THEME_BRANCH_TRACK_PREFIX="${normal} ⤏ ${cyan}"
SCM_THEME_CURRENT_USER_PREFFIX='  '
SCM_GIT_SHOW_CURRENT_USER=false

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 {
[[ "$(_git-upstream)" == "" ]] && SCM_GIT_CHAR="$SCM_GIT_CHAR_DEFAULT"

Expand Down Expand Up @@ -80,8 +90,6 @@ function _prompt {
_exit-code exit_code
_git-uptream-remote-logo

history -a

# Detect root shell
if [ "$(whoami)" = root ]; then
dir_color=$red
Expand Down Expand Up @@ -109,4 +117,5 @@ function _prompt {
PS1="${PS1}${wrap_char}❯${normal} "
}

safe_append_preexec _bash-it-history-auto-save
safe_append_prompt_command _prompt
5 changes: 3 additions & 2 deletions themes/base.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ function aws_profile {
}

function _save-and-reload-history() {
local autosave=${1:-0}
[[ $autosave -eq 1 ]] && history -a && history -c && history -r
local autosave="${1:-${HISTORY_AUTOSAVE:-0}}"
[[ ${autosave} -eq 1 ]] && local HISTCONTROL="${HISTCONTROL:-}${HISTCONTROL:+:}autoshare"
_bash-it-history-auto-save && _bash-it-history-auto-load
}
13 changes: 12 additions & 1 deletion themes/codeword/codeword.theme.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# shellcheck shell=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'

SCM_THEME_PROMPT_PREFIX="${SCM_THEME_PROMPT_SUFFIX:-}"
SCM_THEME_PROMPT_DIRTY="${bold_red?} ✗${normal?}"
SCM_THEME_PROMPT_CLEAN="${bold_green?} ✓${normal?}"
Expand All @@ -21,5 +33,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
14 changes: 12 additions & 2 deletions themes/doubletime/doubletime.theme.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# shellcheck shell=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'

SCM_THEME_PROMPT_DIRTY=''
SCM_THEME_PROMPT_CLEAN=''
SCM_GIT_CHAR="${bold_cyan}±${normal}"
Expand Down Expand Up @@ -29,8 +41,6 @@ else
fi

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)\w
$(scm_prompt)$reset_color $ "
Expand Down
14 changes: 12 additions & 2 deletions themes/doubletime_multiline/doubletime_multiline.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

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)
\w
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

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)
\w
Expand Down
14 changes: 11 additions & 3 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}"
}

save_append_preexec _bash-it-history-auto-save
safe_append_prompt_command prompt_command
14 changes: 12 additions & 2 deletions themes/nwinkler/nwinkler.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
# since 'fasd' is messing with the $PROMPT_COMMAND


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_END_CLEAN="${green}→${reset_color}"
PROMPT_END_DIRTY="${red}→${reset_color}"

Expand All @@ -28,8 +40,6 @@ prompt_setter() {
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='+ '
Expand Down
14 changes: 12 additions & 2 deletions themes/nwinkler_random_colors/nwinkler_random_colors.theme.bash
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,23 @@ prompt_setter() {
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='+ '
}

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
safe_append_prompt_command prompt_setter

SCM_THEME_PROMPT_DIRTY=" ✗"
Expand Down
15 changes: 13 additions & 2 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="|"
local params=( $1 )
Expand Down Expand Up @@ -34,8 +47,6 @@ 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)"
Expand Down
Loading