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 Sep 16, 2024
1 parent 6123e8a commit 1464aeb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 73 deletions.
51 changes: 21 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,19 @@ 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
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/cdcrc). 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 an array with absolute paths to the directories to search.

```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`
Expand All @@ -113,46 +111,40 @@ 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 an array 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)
```

### 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.

```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 +153,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 +183,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 +201,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
53 changes: 11 additions & 42 deletions cdc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
# @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 Down Expand Up @@ -58,17 +53,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 +106,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 @@ -197,28 +180,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 +195,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 +221,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 Down

0 comments on commit 1464aeb

Please sign in to comment.