From b30fe4aa9994231159a4f9c143f80b90b1535bbc Mon Sep 17 00:00:00 2001 From: Edwin Jackson Date: Tue, 14 Jan 2025 16:11:46 -0500 Subject: [PATCH] plugs and themes --- .../per-directory-history.plugin.zsh | 175 +++++++++++++++++- themes/macovsky-ruby.zsh-theme | 13 +- 2 files changed, 186 insertions(+), 2 deletions(-) mode change 120000 => 100644 plugins/per-directory-history/per-directory-history.plugin.zsh mode change 120000 => 100644 themes/macovsky-ruby.zsh-theme diff --git a/plugins/per-directory-history/per-directory-history.plugin.zsh b/plugins/per-directory-history/per-directory-history.plugin.zsh deleted file mode 120000 index 142d9541d..000000000 --- a/plugins/per-directory-history/per-directory-history.plugin.zsh +++ /dev/null @@ -1 +0,0 @@ -per-directory-history.zsh \ No newline at end of file diff --git a/plugins/per-directory-history/per-directory-history.plugin.zsh b/plugins/per-directory-history/per-directory-history.plugin.zsh new file mode 100644 index 000000000..7cd673cdb --- /dev/null +++ b/plugins/per-directory-history/per-directory-history.plugin.zsh @@ -0,0 +1,174 @@ +#!/usr/bin/env zsh +# +# This is a implementation of per directory history for zsh, some +# implementations of which exist in bash[1,2]. It also implements +# a per-directory-history-toggle-history function to change from using the +# directory history to using the global history. In both cases the history is +# always saved to both the global history and the directory history, so the +# toggle state will not effect the saved histories. Being able to switch +# between global and directory histories on the fly is a novel feature as far +# as I am aware. +# +#------------------------------------------------------------------------------- +# Configuration +#------------------------------------------------------------------------------- +# +# HISTORY_BASE a global variable that defines the base directory in which the +# directory histories are stored +# +#------------------------------------------------------------------------------- +# History +#------------------------------------------------------------------------------- +# +# The idea/inspiration for a per directory history is from Stewart MacArthur[1] +# and Dieter[2], the implementation idea is from Bart Schaefer on the the zsh +# mailing list[3]. The implementation is by Jim Hester in September 2012. +# +# [1]: http://www.compbiome.com/2010/07/bash-per-directory-bash-history.html +# [2]: http://dieter.plaetinck.be/per_directory_bash +# [3]: http://www.zsh.org/mla/users/1997/msg00226.html +# +################################################################################ +# +# Copyright (c) 2014 Jim Hester +# +# This software is provided 'as-is', without any express or implied warranty. +# In no event will the authors be held liable for any damages arising from the +# use of this software. +# +# Permission is granted to anyone to use this software for any purpose, +# including commercial applications, and to alter it and redistribute it +# freely, subject to the following restrictions: +# +# 1. The origin of this software must not be misrepresented; you must not claim +# that you wrote the original software. If you use this software in a product, +# an acknowledgment in the product documentation would be appreciated but is +# not required. +# +# 2. Altered source versions must be plainly marked as such, and must not be +# misrepresented as being the original software. +# +# 3. This notice may not be removed or altered from any source distribution.. +# +################################################################################ + +#------------------------------------------------------------------------------- +# configuration, the base under which the directory histories are stored +#------------------------------------------------------------------------------- + +[[ -z $HISTORY_BASE ]] && HISTORY_BASE="$HOME/.directory_history" +[[ -z $HISTORY_START_WITH_GLOBAL ]] && HISTORY_START_WITH_GLOBAL=false +[[ -z $PER_DIRECTORY_HISTORY_TOGGLE ]] && PER_DIRECTORY_HISTORY_TOGGLE='^G' + +#------------------------------------------------------------------------------- +# toggle global/directory history used for searching - ctrl-G by default +#------------------------------------------------------------------------------- + +function per-directory-history-toggle-history() { + if [[ $_per_directory_history_is_global == true ]]; then + _per-directory-history-set-directory-history + _per_directory_history_is_global=false + print -n "\nusing local history" + else + _per-directory-history-set-global-history + _per_directory_history_is_global=true + print -n "\nusing global history" + fi + zle .push-line + zle .accept-line +} + +autoload per-directory-history-toggle-history +zle -N per-directory-history-toggle-history +bindkey $PER_DIRECTORY_HISTORY_TOGGLE per-directory-history-toggle-history + +#------------------------------------------------------------------------------- +# implementation details +#------------------------------------------------------------------------------- + +_per_directory_history_directory="$HISTORY_BASE${PWD:A}/history" + +function _per-directory-history-change-directory() { + _per_directory_history_directory="$HISTORY_BASE${PWD:A}/history" + mkdir -p ${_per_directory_history_directory:h} + if [[ $_per_directory_history_is_global == false ]]; then + #save to the global history + fc -AI $HISTFILE + #save history to previous file + local prev="$HISTORY_BASE${OLDPWD:A}/history" + mkdir -p ${prev:h} + fc -AI $prev + + #discard previous directory's history + local original_histsize=$HISTSIZE + HISTSIZE=0 + HISTSIZE=$original_histsize + + #read history in new file + if [[ -e $_per_directory_history_directory ]]; then + fc -R $_per_directory_history_directory + fi + fi +} + +function _per-directory-history-addhistory() { + # respect hist_ignore_space + if [[ -o hist_ignore_space ]] && [[ "$1" == \ * ]]; then + true + else + print -Sr -- "${1%%$'\n'}" + # instantly write history if set options require it. + if [[ -o share_history ]] || \ + [[ -o inc_append_history ]] || \ + [[ -o inc_append_history_time ]]; then + fc -AI $HISTFILE + fc -AI $_per_directory_history_directory + fi + fc -p $_per_directory_history_directory + fi +} + +function _per-directory-history-precmd() { + if [[ $_per_directory_history_initialized == false ]]; then + _per_directory_history_initialized=true + + if [[ $HISTORY_START_WITH_GLOBAL == true ]]; then + _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 +} + +function _per-directory-history-set-directory-history() { + fc -AI $HISTFILE + local original_histsize=$HISTSIZE + HISTSIZE=0 + HISTSIZE=$original_histsize + if [[ -e "$_per_directory_history_directory" ]]; then + fc -R "$_per_directory_history_directory" + fi +} + +function _per-directory-history-set-global-history() { + fc -AI $_per_directory_history_directory + local original_histsize=$HISTSIZE + HISTSIZE=0 + HISTSIZE=$original_histsize + if [[ -e "$HISTFILE" ]]; then + fc -R "$HISTFILE" + fi +} + +mkdir -p ${_per_directory_history_directory:h} + +#add functions to the exec list for chpwd and zshaddhistory +autoload -U add-zsh-hook +add-zsh-hook chpwd _per-directory-history-change-directory +add-zsh-hook zshaddhistory _per-directory-history-addhistory +add-zsh-hook precmd _per-directory-history-precmd + +# set initialized flag to false +_per_directory_history_initialized=false diff --git a/themes/macovsky-ruby.zsh-theme b/themes/macovsky-ruby.zsh-theme deleted file mode 120000 index d3ee200a9..000000000 --- a/themes/macovsky-ruby.zsh-theme +++ /dev/null @@ -1 +0,0 @@ -macovsky.zsh-theme \ No newline at end of file diff --git a/themes/macovsky-ruby.zsh-theme b/themes/macovsky-ruby.zsh-theme new file mode 100644 index 000000000..f527e5861 --- /dev/null +++ b/themes/macovsky-ruby.zsh-theme @@ -0,0 +1,12 @@ +# ZSH Theme - Preview: https://i.gyazo.com/8becc8a7ed5ab54a0262a470555c3eed.png +local return_code="%(?..%{$fg[red]%}%? ↵%{$reset_color%})" + +PROMPT='%{$fg[green]%}%~%{$reset_color%} $(ruby_prompt_info) $(git_prompt_info)%{$reset_color%}%B$%b ' +RPROMPT="${return_code}" + +ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[yellow]%}‹" +ZSH_THEME_GIT_PROMPT_SUFFIX="› %{$reset_color%}" + + +ZSH_THEME_RUBY_PROMPT_PREFIX="%{$fg[red]%}‹" +ZSH_THEME_RUBY_PROMPT_SUFFIX="› %{$reset_color%}"