From fff047910c3879e7037209be921e0179c2340f50 Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Thu, 4 Nov 2021 11:55:30 +0100 Subject: [PATCH] Support $XDG_CONFIG_HOME Backwards compatible change that supports storing the bookmarks in $XDG_CONFIG_HOME/cd-bookmarks. * If the user as set the previously existing $CD_BOOKMARK_FILE, this is always overriding. * If the user has created the directory/file $XDG_CONFIG_HOME/cd-bookmarks/(bookmarks), use this * Else fall back to old default $HOME/.cdbookmark Fixes #3 --- README.md | 5 ++++- cd-bookmark | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 36f948c..a66974f 100644 --- a/README.md +++ b/README.md @@ -80,8 +80,11 @@ In the 3rd form, list current bookmark. * `-h` display this help and exit ## Variables +The location of the bookmarks list file depends on the envionment's configuration. The file is searched in the following order: -Bookmark list are stored in `~/.cdbookmark` file. This file name can be changed by `CD_BOOKMARK_FILE` variable. +1. `$CD_BOOKMARK_FILE` - if it is set. This has highest precedence. Thus you can set this to override to custom location. +1. `$XDG_CONFIG_HOME/cd-bookmarks/bookmarks` - if the directory `cd-bookmarks/` exist. Note that `$XDG_CONFIG_HOME` defaults to `$HOME/.config` +1. `$HOME/.cdbookmark` - fall back to old default $HOME/.cdbookmark ## Examples diff --git a/cd-bookmark b/cd-bookmark index e0dd738..6e62223 100644 --- a/cd-bookmark +++ b/cd-bookmark @@ -17,8 +17,18 @@ # or: cd-bookmark [-l] # +function _cdbookmark_get_config_file() { + if ! [ -z ${CD_BOOKMARK_FILE+x} ]; then + echo "$CD_BOOKMARK_FILE" + elif [ -d ${XDG_CONFIG_HOME:-$HOME/.config}/cd-bookmark ]; then + echo "${XDG_CONFIG_HOME:-$HOME/.config}/cd-bookmark/bookmarks" + else + echo "${HOME}/.cdbookmark" + fi +} + typeset -r SCRIPT_NAME="cd-bookmark" -typeset -r BOOKMARK_FILE="${CD_BOOKMARK_FILE:-${HOME}/.cdbookmark}" +typeset -r BOOKMARK_FILE="$(_cdbookmark_get_config_file)" function _cdbookmark_print_usage() { cat << EOF