Skip to content

Commit

Permalink
Merge pull request #2 from jasonlyle88/develop
Browse files Browse the repository at this point in the history
Version 1.00.00
  • Loading branch information
jasonlyle88 authored May 10, 2024
2 parents e779b3e + 413474e commit e520850
Show file tree
Hide file tree
Showing 8 changed files with 594 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
scratchpad/**
*.code-workspace

!.gitkeep
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,70 @@
# bd
Easily travel up your current path without using cd ../../..

## Acknowledgements

This is a reimplementation of [zsh-bd](https://github.com/Tarrasch/zsh-bd). It exapands on this project by adding more robust zsh completion, better performance, and following plugin best practices.

## Usage

Usage is simple, `bd` accepts a single argument. The argument specifies which directory to which you want to travel up your directory tree. It can either be the name of a parent directory or the number of levels up you want to go up. Completion is also included and setup to work with your configured [`matcher-list`](https://zsh-manual.netlify.app/completion-widgets?highlight=matcher-list#195-completion-matching-control).

Below is an example:

```shell
# Setup the example
pwd
/tmp/bd_example
❯ mkdir -p a/b/c/d/1/2/3/4
❯ llt
drwxr-xr-x - jlyle wheel 2024-05-10 14:43 .
drwxr-xr-x - jlyle wheel 2024-05-10 14:43 └── a
drwxr-xr-x - jlyle wheel 2024-05-10 14:43 └── b
drwxr-xr-x - jlyle wheel 2024-05-10 14:43 └── c
drwxr-xr-x - jlyle wheel 2024-05-10 14:43 └── d
drwxr-xr-x - jlyle wheel 2024-05-10 14:43 └── 1
drwxr-xr-x - jlyle wheel 2024-05-10 14:43 └── 2
drwxr-xr-x - jlyle wheel 2024-05-10 14:43 └── 3
drwxr-xr-x - jlyle wheel 2024-05-10 14:43 └── 4
cd /tmp/bd_example/a/b/c/d/1/2/3/4
# Go up to a named directory
❯ bd bd_example
pwd
/tmp/bd_example
cd /tmp/bd_example/a/b/c/d/1/2/3/4
# Go up 6 directories
❯ bd 6
pwd
/tmp/bd_example/a/b
cd /tmp/bd_example/a/b/c/d/1/2/3/4
# Go up only 1 directory because 3 is the name of a directory
❯ bd 3
pwd
/tmp/bd_example/a/b/c/d/1/2/3
```

## Requirements
- ZSH Shell

## Installation

### Manual installation
```shell
git clone 'https://github.com/jasonlyle88/bd' "${XDG_CONFIG_HOME:-${HOME}}/bd"
echo 'source "${XDG_CONFIG_HOME:-${HOME}}/bd/bd.plugin.zsh"' >> "${ZDOTDIR:-${HOME}}/.zshrc"
source "${XDG_CONFIG_HOME:-${HOME}}/bd/bd.plugin.zsh"
```

### Installation with package managers

#### [Antidote](https://getantidote.github.io/)
Add `jasonlyle88/bd` to your plugins file (default is `~/.zsh_plugins.txt`)

#### [Oh-My-Zsh](https://ohmyz.sh/)
```shell
git clone 'https://github.com/jasonlyle88/bd' "${ZSH_CUSTOM:-${HOME}/.oh-my-zsh/custom}/plugins/bd"
omz plugin enable bd
```

#### Others
This should be compatible with other ZSH frameworks/package managers, but I have not tested them. If you have tested this plugin with another package manager, feel free to create a merge request and add the instructions here!
22 changes: 22 additions & 0 deletions bd.plugin.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Standardized $0 Handling
# https://wiki.zshell.dev/community/zsh_plugin_standard#zero-handling
0="${ZERO:-${${0:#$ZSH_ARGZERO}:-${(%):-%N}}}"
0="${${(M)0:#/*}:-$PWD/$0}"

# Plugins Hash
# https://github.zshell.dev/docs/zsh/Zsh-Plugin-Standard.html#std-hash
typeset -gA Plugins
Plugins[BD_REPO_DIR]="${0:h}"

# Functions Directory
# https://wiki.zshell.dev/community/zsh_plugin_standard#funtions-directory
if [[ $PMSPEC != *f* ]]; then
fpath+=( "${0:h}/functions" )
fi

autoload -Uz -- \
.bd_get_pwd_parents \
_bd \
bd

compdef _bd bd
39 changes: 39 additions & 0 deletions functions/.bd_get_pwd_parents
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Standard Recommended Options
# https://github.zshell.dev/docs/zsh/Zsh-Plugin-Standard.html#std-options
emulate -L zsh
setopt -- \
extended_glob \
warn_create_global \
typeset_silent \
no_short_loops \
rc_quotes \
no_auto_pushd

# Standard Recommended Varaibles
# https://github.zshell.dev/docs/zsh/Zsh-Plugin-Standard.html#std-variables
local MATCH REPLY
integer MBEGIN MEND
local -a match mbegin mend reply

# Seperate PWD into an array, splitting on / character
local -a parents=(${(ps:/:)"${PWD}"})
local numParents
local idx

# prepend root to the parents array
parents=('/' "${parents[@]}")

# Remove the current directory since it isn't a parent
shift -p parents

# Get the number of parents
numParents="${#parents[@]}"

# Print the array in reverse
for idx in $(seq "${numParents}" -1 0); do
printf -- '%s' "${parents[${idx}]}"

if [[ "${idx}" -gt 1 ]]; then
printf -- '\0'
fi
done
26 changes: 26 additions & 0 deletions functions/_bd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Standard Recommended Options
# https://github.zshell.dev/docs/zsh/Zsh-Plugin-Standard.html#std-options
emulate -L zsh
setopt -- \
extended_glob \
warn_create_global \
typeset_silent \
no_short_loops \
rc_quotes \
no_auto_pushd

# Standard Recommended Varaibles
# https://github.zshell.dev/docs/zsh/Zsh-Plugin-Standard.html#std-variables
local MATCH REPLY
integer MBEGIN MEND
local -a match mbegin mend reply

local localMatcherList
local -a parents

# Get the ZStyle matcher-list value
zstyle -s ':completion:*' 'matcher-list' 'localMatcherList'

parents=("${(0)"$(.bd_get_pwd_parents)"}")

compadd -V 'Parent directories' -M "${localMatcherList}" "$@" -- "${parents[@]}"
67 changes: 67 additions & 0 deletions functions/bd
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Standard Recommended Options
# https://github.zshell.dev/docs/zsh/Zsh-Plugin-Standard.html#std-options
emulate -L zsh
setopt -- \
extended_glob \
warn_create_global \
typeset_silent \
no_short_loops \
rc_quotes \
no_auto_pushd

# Standard Recommended Varaibles
# https://github.zshell.dev/docs/zsh/Zsh-Plugin-Standard.html#std-variables
local MATCH REPLY
integer MBEGIN MEND
local -a match mbegin mend reply

# Print out usage is no parameters are given
(($#<1)) && {
printf -- 'usage: %s <name-of-any-parent-directory>\n' "${0}"
printf -- ' %s <number-of-folders>\n' "${0}"

return 1
} >&2

local requestedDestination="${1}"
local -a parents
local numParents
local destination
local parent
local idx

parents=("${(0)"$(.bd_get_pwd_parents)"}")
numParents="${#parents[@]}"

# Build destination and 'cd' to it by looping over the parents array in reverse
destination='./'
for parent in "${parents[@]}"; do
destination+='../'

if [[ "${requestedDestination}" == "${parent}" ]]; then
cd "${destination}"

return $?
fi
done

# If the user provided an integer, go up as many times as asked
destination='./'
if [[ "${requestedDestination}" == <-> ]]; then
if [[ "${requestedDestination}" -gt "${numParents}" ]]; then
printf -- '%s: Error: Can not go up %s times (not enough parent directories)\n' "${0}" "${requestedDestination}"
return 1
fi

for idx in {1.."${requestedDestination}"}; do
destination+='../'
done

cd "${destination}"

return $?
fi

# If the above methods fail
printf -- '%s: Error: No parent directory named "%s"\n' "${0}" "${requestedDestination}"
return 1
Empty file added scratchpad/.gitkeep
Empty file.
Loading

0 comments on commit e520850

Please sign in to comment.