feat(per-directory-history): add a PER_DIRECTORY_HISTORY_AUTO_TOGGLE config option

* `PER_DIRECTORY_HISTORY_AUTO_TOGGLE` if set to true, when you change
  directories, the mode will be toggled to match the mode that was used 
  the last time you were in the directory.


Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
This commit is contained in:
Hiram Chirino 2024-10-03 18:44:34 -04:00
parent dae5a41159
commit 66f595427f
2 changed files with 45 additions and 6 deletions

View file

@ -35,6 +35,9 @@ toggle set the `PER_DIRECTORY_HISTORY_TOGGLE` environment variable.
function above (default `^G`) function above (default `^G`)
* `PER_DIRECTORY_HISTORY_PRINT_MODE_CHANGE` is a variable which toggles whether * `PER_DIRECTORY_HISTORY_PRINT_MODE_CHANGE` is a variable which toggles whether
the current mode is printed to the screen following a mode change (default `true`) the current mode is printed to the screen following a mode change (default `true`)
* `PER_DIRECTORY_HISTORY_AUTO_TOGGLE` if set to true, when you change
directories, the mode will be toggled to match the mode that was used
the last time you were in the directory.
## History ## History

View file

@ -60,6 +60,7 @@
[[ -z $HISTORY_START_WITH_GLOBAL ]] && HISTORY_START_WITH_GLOBAL=false [[ -z $HISTORY_START_WITH_GLOBAL ]] && HISTORY_START_WITH_GLOBAL=false
[[ -z $PER_DIRECTORY_HISTORY_TOGGLE ]] && PER_DIRECTORY_HISTORY_TOGGLE='^G' [[ -z $PER_DIRECTORY_HISTORY_TOGGLE ]] && PER_DIRECTORY_HISTORY_TOGGLE='^G'
[[ -z $PER_DIRECTORY_HISTORY_PRINT_MODE_CHANGE ]] && PER_DIRECTORY_HISTORY_PRINT_MODE_CHANGE=true [[ -z $PER_DIRECTORY_HISTORY_PRINT_MODE_CHANGE ]] && PER_DIRECTORY_HISTORY_PRINT_MODE_CHANGE=true
[[ -z $PER_DIRECTORY_HISTORY_AUTO_TOGGLE ]] && PER_DIRECTORY_HISTORY_AUTO_TOGGLE=false
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# toggle global/directory history used for searching - ctrl-G by default # toggle global/directory history used for searching - ctrl-G by default
@ -94,6 +95,26 @@ _per_directory_history_directory="$HISTORY_BASE${PWD:A}/history"
function _per-directory-history-change-directory() { function _per-directory-history-change-directory() {
_per_directory_history_directory="$HISTORY_BASE${PWD:A}/history" _per_directory_history_directory="$HISTORY_BASE${PWD:A}/history"
# Should we use the toggle mode that was last used in this directory?
if [[ $PER_DIRECTORY_HISTORY_AUTO_TOGGLE == true ]]; then
if [[ -f "${_per_directory_history_directory}-on" ]]; then
if [[ $_per_directory_history_is_global == true ]]; then
_per_directory_history_is_global=false
if [[ $PER_DIRECTORY_HISTORY_PRINT_MODE_CHANGE == true ]]; then
echo "using local history"
fi
fi
else
if [[ $_per_directory_history_is_global == false ]]; then
_per_directory_history_is_global=true
if [[ $PER_DIRECTORY_HISTORY_PRINT_MODE_CHANGE == true ]]; then
echo "using global history"
fi
fi
fi
fi
mkdir -p ${_per_directory_history_directory:h} mkdir -p ${_per_directory_history_directory:h}
if [[ $_per_directory_history_is_global == false ]]; then if [[ $_per_directory_history_is_global == false ]]; then
#save to the global history #save to the global history
@ -135,13 +156,22 @@ function _per-directory-history-addhistory() {
function _per-directory-history-precmd() { function _per-directory-history-precmd() {
if [[ $_per_directory_history_initialized == false ]]; then if [[ $_per_directory_history_initialized == false ]]; then
_per_directory_history_initialized=true _per_directory_history_initialized=true
if [[ $PER_DIRECTORY_HISTORY_AUTO_TOGGLE == false ]] ; then
if [[ $HISTORY_START_WITH_GLOBAL == true ]]; then if [[ $HISTORY_START_WITH_GLOBAL == true ]]; then
_per-directory-history-set-global-history _per-directory-history-set-global-history
_per_directory_history_is_global=true _per_directory_history_is_global=true
else
_per-directory-history-set-directory-history
_per_directory_history_is_global=false
fi
else else
_per-directory-history-set-directory-history if [[ ! -f "${_per_directory_history_directory}-on" ]]; then
_per_directory_history_is_global=false _per-directory-history-set-global-history
_per_directory_history_is_global=true
else
_per-directory-history-set-directory-history
_per_directory_history_is_global=false
fi
fi fi
fi fi
} }
@ -151,6 +181,9 @@ function _per-directory-history-set-directory-history() {
local original_histsize=$HISTSIZE local original_histsize=$HISTSIZE
HISTSIZE=0 HISTSIZE=0
HISTSIZE=$original_histsize HISTSIZE=$original_histsize
if [[ $PER_DIRECTORY_HISTORY_AUTO_TOGGLE == true ]]; then
echo "" > "${_per_directory_history_directory}-on"
fi
if [[ -e "$_per_directory_history_directory" ]]; then if [[ -e "$_per_directory_history_directory" ]]; then
fc -R "$_per_directory_history_directory" fc -R "$_per_directory_history_directory"
fi fi
@ -161,6 +194,9 @@ function _per-directory-history-set-global-history() {
local original_histsize=$HISTSIZE local original_histsize=$HISTSIZE
HISTSIZE=0 HISTSIZE=0
HISTSIZE=$original_histsize HISTSIZE=$original_histsize
if [[ $PER_DIRECTORY_HISTORY_AUTO_TOGGLE == true ]]; then
rm "${_per_directory_history_directory}-on" 2> /dev/null
fi
if [[ -e "$HISTFILE" ]]; then if [[ -e "$HISTFILE" ]]; then
fc -R "$HISTFILE" fc -R "$HISTFILE"
fi fi