Skip to content

Commit

Permalink
Don't require a ~/.cdcrc file
Browse files Browse the repository at this point in the history
  • Loading branch information
evanthegrayt committed Jan 21, 2025
1 parent 6123e8a commit 15051b7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 103 deletions.
67 changes: 30 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,72 +87,66 @@ source $INSTALLATION_PATH/cdc.sh # in either ~/.zshrc or ~/.bashrc
```

## Set-up
The following settings require variables to be set from a file called
`~/.cdcrc`. Note that the `~/.cdcrc` file is just a shell script that sets
values, so you can use `bash` conditionals if you'd like to use the same config
file on multiple systems. You can view an example of this in [my config
file](https://github.com/evanthegrayt/dotfiles/blob/master/dotfiles/cdcrc). Just
remember, these files get sourced into your interactive shell on startup, so
only use it to set the following values.
The following settings require variables to be set from a startup file, such as
`~/.zshrc` or `~/.bashrc`. You can view an example of this in [my config
file](https://github.com/evanthegrayt/dotfiles/blob/master/dotfiles/shellrc#L27).
Just remember, these files get sourced into your interactive shell on startup,
so only use it to set the following values.

### Telling cdc where to look
To use this plugin, you need to set `CDC_DIRS` in `~/.cdcrc`. It should be an
array with absolute paths to the directories to search.
To use this plugin, you need to `export CDC_DIRS` in a startup file. It should
be a string with absolute paths to the directories to search, separated by
colons (similar to `$PATH`).

```sh
# Set this in ~/.cdcrc
CDC_DIRS=($HOME/dir_with_repos $HOME/workspace/another_dir_with_repos)
# Set this in ~/.zshrc or similar
export CDC_DIRS=$HOME/dir_with_repos:$HOME/workspace/another_dir_with_repos
```

Note that the order of the elements in the array matters. The plugin will `cd`
Note that the order of the paths in the string matters. The plugin will `cd`
to the first match it finds, so if you have the same repository -- or two
repositories with the same name -- in two places, the first location in the
array will take precedence. There is currently an issue to better handle this
string will take precedence. There is currently an issue to better handle this
"feature". Not sure how I want to go about it yet. Suggestions are very much
welcome [on the issue](https://github.com/evanthegrayt/cdc/issues/6).

### Ignoring certain directories
If you have directories within `CDC_DIRS` that you want the plugin to ignore,
you can set `CDC_IGNORE` to an array containing those directories. These
you can `export CDC_IGNORE` to a string containing those directories. These
elements should only be the directory base-name, **not** the absolute path.
"Ignoring" a directory will prevent it from being "seen" by `cdc`.

```sh
# Assuming you never want to `cdc notes_directory`:
CDC_IGNORE=(notes_directory)
export CDC_IGNORE=notes_directory:training
```

### Only recognize actual repositories
You can set `CDC_REPOS_ONLY` in `~/.cdcrc` to make `cdc` only recognize
You can `export CDC_REPOS_ONLY` in a startup file to make `cdc` only recognize
repositories as directories. This is **disabled by default**. You can also set
an array of files and directories that mark what you consider a repository. Note
that markers that are directories must end with a `/`, while files must not.
a string of files and directories that mark what you consider a repository.
Note that markers that are directories must end with a `/`, while files must
not.

```sh
# Enable "repos-only" mode. Note, the default is false.
CDC_REPOS_ONLY=true
export CDC_REPOS_ONLY=true
# Set repository markers with the following. Note, the following is already the
# default, but this is how you can overwrite it in ~/.cdcrc.
CDC_REPO_MARKERS=(.git/ .git Rakefile Makefile .hg/ .bzr/ .svn/)
```
If you want to add markers to the existing array without overwriting it, you
can use `+=` when assigning it.

```sh
CDC_REPO_MARKERS+=(.example_directory_marker/ .example_file_marker)
# default, but this is how you can overwrite it in ~/.zshrc or similar.
export CDC_REPO_MARKERS=.git/:.git:Rakefile:Makefile:.hg/:.bzr/:.svn/
```

Note that this setting can be overridden with the `-r` and `-R` options. See
[options](#options) below.

### Automatically pushing to the history stack
By default, every `cdc` call will push the directory onto the history stack. You
can disable this feature by setting `CDC_AUTO_PUSH` to `false` in your
`~/.cdcrc`.
can disable this feature by setting `CDC_AUTO_PUSH` to `false` in a startup
file.

```sh
# Disable auto-pushing to history stack.
CDC_AUTO_PUSH=false
export CDC_AUTO_PUSH=false
```

You can then manually push directories onto the stack with `-u`. If you have
Expand All @@ -161,14 +155,14 @@ it to the stack with the `-U` option. See [options](#options) below.

### Colored Output
You can enable/disable colored terminal output, and even change the colors, by
adding the following lines to your `~/.cdcrc`.
adding the following lines to a startup file.

```sh
CDC_COLOR=false # Default: true. Setting to false disables colors
export CDC_COLOR=false # Default: true. Setting to false disables colors
# The following lines would make the colored output bold.
CDC_SUCCESS_COLOR='\033[1;92m' # Bold green. Default: '\033[0;32m' (green)
CDC_WARNING_COLOR='\033[1;93m' # Bold yellow. Default: '\033[0;33m' (yellow)
CDC_ERROR_COLOR='\033[1;91m' # Bold red. Default: '\033[0;31m' (red)
export CDC_SUCCESS_COLOR='\033[1;92m' # Bold green. Default: '\033[0;32m' (green)
export CDC_WARNING_COLOR='\033[1;93m' # Bold yellow. Default: '\033[0;33m' (yellow)
export CDC_ERROR_COLOR='\033[1;91m' # Bold red. Default: '\033[0;31m' (red)
```

## Usage
Expand All @@ -191,7 +185,7 @@ print a message to `stderr`.
### Options
The plugin comes with a few available options. Some are for dealing with the
directory history stack, similar to `pushd`, `popd`, and `dirs`. Others are for
overriding variables set in `~/.cdcrc`. There's also a debug mode.
overriding variables set in a startup file. There's also a debug mode.

|Flag|What it does|
|:------|:-----------|
Expand All @@ -209,7 +203,6 @@ overriding variables set in `~/.cdcrc`. There's also a debug mode.
|-U|Do not push the directory onto the stack.|
|-r|Only `cd` to repositories.|
|-R|`cd` to the directory even if it's not a repository.|
|-s|Re-source the config file (`~/.cdcrc`)|
|-D|Debug mode. Enables warnings for when things aren't working as expected.|
|-w|Print the directory location instead of changing to it. Like `which`.|
|-h|Print help.|
Expand Down
1 change: 0 additions & 1 deletion cdc.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ _cdc() {
-r"[Only cdc to repositories.]" \
-R"[cd to any directory, even it is not a repository.]" \
-a"[cd to the directory even if it is ignored.]" \
-s"[Re-source the config file ('~/.cdcrc')]" \
-w"[Print directory location instead of changing to it]" \
1::"[Directory to cd]:($(_cdc_repo_list))"
}
Expand Down
106 changes: 41 additions & 65 deletions cdc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
# @param string $cd_dir
# @return void
cdc() {

if [[ ! -f ~/.cdcrc ]]; then
_cdc_print 'error' 'You must create a config file called ~/.cdcrc' true
return 1
fi

##
# Set local vars to avoid environment pollution.
local dir
Expand All @@ -38,6 +32,8 @@ cdc() {
local print_help=false
local source_config_file=false
local use_color=${CDC_COLOR:-true}
local cdc_dirs=($( printf "%s\n" "${CDC_DIRS//:/ }" ))
local cdc_ignore=($( printf "%s\n" "${CDC_IGNORE//:/ }" ))

##
# The default for auto-push is true. The user can set `CDC_AUTO_PUSH=false`
Expand All @@ -58,17 +54,9 @@ cdc() {
local subdir="${1#*/}"
fi

##
# Check for the existence of required variables that should be set in
# ~/.cdcrc or a startup file. If not found, exit with non-zero return code.
if (( ${#CDC_DIRS[@]} == 0 )); then
_cdc_error 'You must set CDC_DIRS in a ~/.cdcrc file. See README.md.'
return 1
fi

##
# Case options if present. Suppress errors because we'll supply our own.
while getopts 'acCdDhilLnprRstuUw' opt 2>/dev/null; do
while getopts 'acCdDhilLnprRtuUw' opt 2>/dev/null; do
case $opt in

##
Expand Down Expand Up @@ -119,10 +107,6 @@ cdc() {
# -R: Force cdc to NOT only cd to repositories.
R) repos_only=false ;;

##
# -s: Re-source ~/.cdcrc
s) source_config_file=true ;;

##
# -u: Push the directory onto the history stack.
u) pushdir=true ;;
Expand Down Expand Up @@ -177,9 +161,9 @@ cdc() {
if [[ $debug == true ]]; then
echo "========================= ENV ==========================="
printf "CDC_DIRS += ${CDC_SUCCESS_COLOR}%s$CDC_RESET\n"\
"${CDC_DIRS[@]}"
"${cdc_dirs[@]}"
printf "CDC_IGNORE += ${CDC_ERROR_COLOR}%s$CDC_RESET\n"\
"${CDC_IGNORE[@]}"
"${cdc_ignore[@]}"
echo
printf "CDC_AUTO_PUSH = %s\n" \
$( _cdc_print 'boolean' $CDC_AUTO_PUSH )
Expand All @@ -197,28 +181,12 @@ cdc() {
echo "======================= RUNTIME ========================="
fi

if [[ $source_config_file == true ]]; then
##
# Reset all settings to their default values.
CDC_DIRS=()
CDC_IGNORE=()
CDC_REPOS_ONLY=false
CDC_REPO_MARKERS=(.git/ .git Rakefile Makefile .hg/ .bzr/ .svn/)
CDC_COLOR=true
CDC_AUTO_PUSH=true
CDC_ERROR_COLOR='\e[0;31m'
CDC_SUCCESS_COLOR='\e[0;32m'
CDC_WARNING_COLOR='\e[0;33m'

##
# Source the config file.
source $HOME/.cdcrc
if [[ $debug == true ]]; then
_cdc_print 'success' 'Re-sourced config file (~/.cdcrc)' true
fi
if (( $# == 0 )); then
return 0
fi
##
# Check for the existence of required variables that should be set in
# ~/.cdcrc or a startup file. If not found, exit with non-zero return code.
if (( ${#cdc_dirs[@]} == 0 )); then
_cdc_print 'error' 'You must set CDC_DIRS in a config file' $debug
return 1
fi

if [[ $print_help == true ]]; then
Expand All @@ -228,6 +196,10 @@ cdc() {
printf "${CDC_RESET}\n"
printf " ${CDC_WARNING_COLOR}-a${CDC_RESET}"
echo ' | `cd` to the directory even if it is ignored.'
printf " ${CDC_WARNING_COLOR}-c${CDC_RESET}"
echo ' | Enable colored output'
printf " ${CDC_WARNING_COLOR}-C${CDC_RESET}"
echo ' | Disable colored output'
printf " ${CDC_WARNING_COLOR}-l${CDC_RESET}"
echo ' | List all directories that are cdc-able.'
printf " ${CDC_WARNING_COLOR}-L${CDC_RESET}"
Expand All @@ -250,8 +222,6 @@ cdc() {
echo ' | 'Only cdc to repositories.
printf " ${CDC_WARNING_COLOR}-R${CDC_RESET}"
echo ' | cd to any directory, even it is not a repository.'
printf " ${CDC_WARNING_COLOR}-s${CDC_RESET}"
echo ' | Re-source the config file (~/.cdcrc).'
printf " ${CDC_WARNING_COLOR}-D${CDC_RESET}"
echo ' | Debug mode for when unexpected things are happening.'
printf " ${CDC_WARNING_COLOR}-w${CDC_RESET}"
Expand All @@ -266,7 +236,7 @@ cdc() {
if [[ $debug == true ]]; then
_cdc_print 'success' 'Listing searched directories.' $debug
fi
printf "%s\n" "${CDC_DIRS[@]}" | column
printf "%s\n" "${cdc_dirs[@]}" | column
should_return=true
fi

Expand Down Expand Up @@ -327,7 +297,7 @@ cdc() {
if [[ $cdc_list_ignored == true ]]; then
##
# If the ignore array is empty, return.
if (( ${#CDC_IGNORE[@]} == 0 )); then
if (( ${#cdc_ignore[@]} == 0 )); then
if [[ $debug == true ]]; then
_cdc_print 'warn' 'No directories are being ignored.' $debug
fi
Expand All @@ -336,7 +306,7 @@ cdc() {
_cdc_print 'success' 'Listing ignored directories.' $debug
fi

printf "%s\n" "${CDC_IGNORE[@]}" | column
printf "%s\n" "${cdc_ignore[@]}" | column
fi

should_return=true
Expand Down Expand Up @@ -429,11 +399,11 @@ cdc() {
fi

##
# Loop through every element in $CDC_DIRS.
for dir in ${CDC_DIRS[@]}; do
# Loop through every element in $cdc_dirs.
for dir in ${cdc_dirs[@]}; do

##
# If a directory is in the $CDC_DIRS array, but the directory doesn't
# If a directory is in the $cdc_dirs array, but the directory doesn't
# exist, print a message to stderr and move on to the next directory in
# the array.
if ! [[ -d $dir ]]; then
Expand Down Expand Up @@ -527,16 +497,17 @@ cdc() {
_cdc_is_excluded_dir() {
local element
local string="$1"
local cdc_ignore=($( printf "%s\n" "${CDC_IGNORE//:/ }" ))

##
# If $CDC_IGNORE isn't defined or is empty, return "false".
if [[ -z $CDC_IGNORE ]] || (( ${#CDC_IGNORE[@]} == 0 )); then
# If $cdc_ignore isn't defined or is empty, return "false".
if [[ -z $cdc_ignore ]] || (( ${#cdc_ignore[@]} == 0 )); then
return 1
fi

##
# Loop through each element of $CDC_IGNORE array.
for element in "${CDC_IGNORE[@]}"; do
for element in "${cdc_ignore[@]}"; do

##
# If the element matches the passed string, return "true" to indicate
Expand All @@ -552,7 +523,7 @@ _cdc_is_excluded_dir() {
}

##
# Lists repositories found in $CDC_DIRS that aren't excluded.
# Lists repositories found in $cdc_dirs that aren't excluded.
#
# @param string $string
# @return array
Expand All @@ -562,10 +533,11 @@ _cdc_repo_list() {
local fulldir
local directories=()
local debug=${1:-false}
local cdc_dirs=($( printf "%s\n" "${CDC_DIRS//:/ }" ))

##
# Loop through all elements of $CDC_DIRS array.
for dir in "${CDC_DIRS[@]}"; do
# Loop through all elements of $cdc_dirs array.
for dir in "${cdc_dirs[@]}"; do

##
# If the element isn't a directory that exists, move on.
Expand Down Expand Up @@ -617,10 +589,18 @@ _cdc_is_repo_dir() {
local id
local marker
local dir="$1"
local repo_markers

if [[ -n $CDC_REPO_MARKERS ]]; then
repo_markers=($( printf "%s\n" "${CDC_REPO_MARKERS//:/ }" ))
else
repo_markers=(.git/ .git Rakefile Makefile .hg/ .bzr/ .svn/)
fi


##
# Spin through all known repository markers.
for marker in ${CDC_REPO_MARKERS[@]}; do
for marker in ${repo_markers[@]}; do

##
# Repo identifier is the passed directory plus the known marker.
Expand Down Expand Up @@ -683,18 +663,14 @@ _cdc_print() {
esac
}

##
# The default files and directories that mark the root of a repository.
# This is set before `~/.cdcrc` is sourced so the user can overwrite OR
# append to it from their config file.
CDC_REPO_MARKERS=(.git/ .git Rakefile Makefile .hg/ .bzr/ .svn/)

##
# Source the user's config file.
if [[ -f $HOME/.cdcrc ]]; then
echo "Using ~/.cdcrc is deprecated and will be removed in a future version." >&2
echo "Please set values in a config file instead." >&2
source $HOME/.cdcrc
fi

##
# Set the array that will remember the history.
# Set the array that will remember the history. Needs to persist.
CDC_HISTORY=()

0 comments on commit 15051b7

Please sign in to comment.