From d8e7b173b5b96e3ed861f20ce900e046d1245939 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Fri, 31 Dec 2021 00:11:23 -0800 Subject: [PATCH 1/3] plugins/dirs: use XDG_STATE_HOME Locate the bookmarks file in $XDG_STATE_HOME, and migrate an existing file from the old location if it exists. --- plugins/available/dirs.plugin.bash | 108 ++++++++++++++++------------- 1 file changed, 59 insertions(+), 49 deletions(-) diff --git a/plugins/available/dirs.plugin.bash b/plugins/available/dirs.plugin.bash index a455ff95..7cbd6684 100644 --- a/plugins/available/dirs.plugin.bash +++ b/plugins/available/dirs.plugin.bash @@ -32,74 +32,84 @@ alias pu="pushd" alias po="popd" function dirs-help() { - about 'directory navigation alias usage' - group 'dirs' + about 'directory navigation alias usage' + group 'dirs' - echo "Directory Navigation Alias Usage" - echo - echo "Use the power of directory stacking to move" - echo "between several locations with ease." - echo - echo "d : Show directory stack." - echo "po : Remove current location from stack." - echo "pc : Adds current location to stack." - echo "pu : Adds given location to stack." - echo "1 : Change to stack location 1." - echo "2 : Change to stack location 2." - echo "3 : Change to stack location 3." - echo "4 : Change to stack location 4." - echo "5 : Change to stack location 5." - echo "6 : Change to stack location 6." - echo "7 : Change to stack location 7." - echo "8 : Change to stack location 8." - echo "9 : Change to stack location 9." + echo "Directory Navigation Alias Usage" + echo + echo "Use the power of directory stacking to move" + echo "between several locations with ease." + echo + echo "d : Show directory stack." + echo "po : Remove current location from stack." + echo "pc : Adds current location to stack." + echo "pu : Adds given location to stack." + echo "1 : Change to stack location 1." + echo "2 : Change to stack location 2." + echo "3 : Change to stack location 3." + echo "4 : Change to stack location 4." + echo "5 : Change to stack location 5." + echo "6 : Change to stack location 6." + echo "7 : Change to stack location 7." + echo "8 : Change to stack location 8." + echo "9 : Change to stack location 9." } # Add bookmarking functionality # Usage: -if [ ! -f ~/.dirs ]; then # if doesn't exist, create it - touch ~/.dirs +: "${BASH_IT_DIRS_BKS:=${XDG_STATE_HOME:-~/.local/state}/bash_it/dirs}" +if [[ -f "${BASH_IT_DIRS_BKS?}" ]]; then + source "$BASH_IT_DIRS_BKS" +elif [[ -f ~/.dirs ]]; then + mv -vn ~/.dirs "$BASH_IT_DIRS_BKS" + source "$BASH_IT_DIRS_BKS" else - source ~/.dirs + touch "$BASH_IT_DIRS_BKS" fi -alias L='cat ~/.dirs' +alias L='cat "${BASH_IT_DIRS_BKS?}"' # Goes to destination dir, otherwise stay in the dir -G () { - about 'goes to destination dir' - param '1: directory' - example '$ G ..' - group 'dirs' +function G() { + about 'goes to destination dir' + param '1: directory' + example '$ G ..' + group 'dirs' - cd "${1:-${PWD}}" ; + cd "${1:-${PWD}}" } -S () { - about 'save a bookmark' - param '1: bookmark name' - example '$ S mybkmrk' - group 'dirs' +function S() { + about 'save a bookmark' + param '1: bookmark name' + example '$ S mybkmrk' + group 'dirs' - [[ $# -eq 1 ]] || { echo "${FUNCNAME[0]} function requires 1 argument"; return 1; } + [[ $# -eq 1 ]] || { + echo "${FUNCNAME[0]} function requires 1 argument" + return 1 + } - sed "/$@/d" ~/.dirs > ~/.dirs1; - \mv ~/.dirs1 ~/.dirs; - echo "$@"=\""${PWD}"\" >> ~/.dirs; - source ~/.dirs ; + sed "/$@/d" "$BASH_IT_DIRS_BKS" > ~/.dirs1 + \mv ~/.dirs1 "$BASH_IT_DIRS_BKS" + echo "$@"=\""${PWD}"\" >> "$BASH_IT_DIRS_BKS" + source "$BASH_IT_DIRS_BKS" } -R () { - about 'remove a bookmark' - param '1: bookmark name' - example '$ R mybkmrk' - group 'dirs' +function R() { + about 'remove a bookmark' + param '1: bookmark name' + example '$ R mybkmrk' + group 'dirs' - [[ $# -eq 1 ]] || { echo "${FUNCNAME[0]} function requires 1 argument"; return 1; } + [[ $# -eq 1 ]] || { + echo "${FUNCNAME[0]} function requires 1 argument" + return 1 + } - sed "/$@/d" ~/.dirs > ~/.dirs1; - \mv ~/.dirs1 ~/.dirs; + sed "/$@/d" "$BASH_IT_DIRS_BKS" > ~/.dirs1 + \mv ~/.dirs1 "$BASH_IT_DIRS_BKS" } -alias U='source ~/.dirs' # Update bookmark stack +alias U='source "$BASH_IT_DIRS_BKS"' # Update bookmark stack From 71b4f3c1bcb86910852e21d52e9567771962b70c Mon Sep 17 00:00:00 2001 From: John D Pell Date: Fri, 31 Dec 2021 00:13:01 -0800 Subject: [PATCH 2/3] plugin/dirs: `shfmt` && `shellcheck` --- clean_files.txt | 1 + plugins/available/dirs.plugin.bash | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index 8c8b3fed..52d77262 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -88,6 +88,7 @@ plugins/available/basher.plugin.bash plugins/available/blesh.plugin.bash plugins/available/cmd-returned-notify.plugin.bash plugins/available/direnv.plugin.bash +plugins/available/dirs.plugin.bash plugins/available/docker-machine.plugin.bash plugins/available/git.plugin.bash plugins/available/go.plugin.bash diff --git a/plugins/available/dirs.plugin.bash b/plugins/available/dirs.plugin.bash index 7cbd6684..91b7cabe 100644 --- a/plugins/available/dirs.plugin.bash +++ b/plugins/available/dirs.plugin.bash @@ -1,3 +1,4 @@ +# shellcheck shell=bash # Directory stack navigation: # # Add to stack with: pu /path/to/directory @@ -60,9 +61,11 @@ function dirs-help() { : "${BASH_IT_DIRS_BKS:=${XDG_STATE_HOME:-~/.local/state}/bash_it/dirs}" if [[ -f "${BASH_IT_DIRS_BKS?}" ]]; then + # shellcheck disable=SC1090 source "$BASH_IT_DIRS_BKS" elif [[ -f ~/.dirs ]]; then mv -vn ~/.dirs "$BASH_IT_DIRS_BKS" + # shellcheck disable=SC1090 source "$BASH_IT_DIRS_BKS" else touch "$BASH_IT_DIRS_BKS" @@ -77,7 +80,7 @@ function G() { example '$ G ..' group 'dirs' - cd "${1:-${PWD}}" + cd "${1:-${PWD}}" || return } function S() { @@ -91,9 +94,10 @@ function S() { return 1 } - sed "/$@/d" "$BASH_IT_DIRS_BKS" > ~/.dirs1 - \mv ~/.dirs1 "$BASH_IT_DIRS_BKS" - echo "$@"=\""${PWD}"\" >> "$BASH_IT_DIRS_BKS" + sed "/$1/d" "$BASH_IT_DIRS_BKS" > ~/.dirs1 + command mv ~/.dirs1 "$BASH_IT_DIRS_BKS" + echo "$1"=\""${PWD}"\" >> "$BASH_IT_DIRS_BKS" + # shellcheck disable=SC1090 source "$BASH_IT_DIRS_BKS" } @@ -108,7 +112,7 @@ function R() { return 1 } - sed "/$@/d" "$BASH_IT_DIRS_BKS" > ~/.dirs1 + sed "/$1/d" "$BASH_IT_DIRS_BKS" > ~/.dirs1 \mv ~/.dirs1 "$BASH_IT_DIRS_BKS" } From 8b308df9391f9d495362f29738cbd94df63adfd7 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 30 Dec 2021 23:04:02 -0800 Subject: [PATCH 3/3] plugin/dirs: use `$BASH_IT_DIRS_BKS.new` instead of `~/.dirs1` --- plugins/available/dirs.plugin.bash | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/plugins/available/dirs.plugin.bash b/plugins/available/dirs.plugin.bash index 91b7cabe..f61680ca 100644 --- a/plugins/available/dirs.plugin.bash +++ b/plugins/available/dirs.plugin.bash @@ -62,13 +62,13 @@ function dirs-help() { : "${BASH_IT_DIRS_BKS:=${XDG_STATE_HOME:-~/.local/state}/bash_it/dirs}" if [[ -f "${BASH_IT_DIRS_BKS?}" ]]; then # shellcheck disable=SC1090 - source "$BASH_IT_DIRS_BKS" + source "${BASH_IT_DIRS_BKS?}" elif [[ -f ~/.dirs ]]; then - mv -vn ~/.dirs "$BASH_IT_DIRS_BKS" + mv -vn ~/.dirs "${BASH_IT_DIRS_BKS?}" # shellcheck disable=SC1090 - source "$BASH_IT_DIRS_BKS" + source "${BASH_IT_DIRS_BKS?}" else - touch "$BASH_IT_DIRS_BKS" + touch "${BASH_IT_DIRS_BKS?}" fi alias L='cat "${BASH_IT_DIRS_BKS?}"' @@ -94,11 +94,11 @@ function S() { return 1 } - sed "/$1/d" "$BASH_IT_DIRS_BKS" > ~/.dirs1 - command mv ~/.dirs1 "$BASH_IT_DIRS_BKS" - echo "$1"=\""${PWD}"\" >> "$BASH_IT_DIRS_BKS" + sed "/$1/d" "${BASH_IT_DIRS_BKS?}" > "${BASH_IT_DIRS_BKS?}.new" + command mv "${BASH_IT_DIRS_BKS?}.new" "${BASH_IT_DIRS_BKS?}" + echo "$1"=\""${PWD}"\" >> "${BASH_IT_DIRS_BKS?}" # shellcheck disable=SC1090 - source "$BASH_IT_DIRS_BKS" + source "${BASH_IT_DIRS_BKS?}" } function R() { @@ -112,8 +112,8 @@ function R() { return 1 } - sed "/$1/d" "$BASH_IT_DIRS_BKS" > ~/.dirs1 - \mv ~/.dirs1 "$BASH_IT_DIRS_BKS" + sed "/$1/d" "${BASH_IT_DIRS_BKS?}" > "${BASH_IT_DIRS_BKS?}.new" + command mv "${BASH_IT_DIRS_BKS?}.new" "${BASH_IT_DIRS_BKS?}" } -alias U='source "$BASH_IT_DIRS_BKS"' # Update bookmark stack +alias U='source "${BASH_IT_DIRS_BKS?}"' # Update bookmark stack