Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to add warp points to other directories #121

Merged
merged 33 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ rm -f ~/.zcompdump; compinit
If you want to make use of the `fzf`-powered browse feature to fuzzy search through all your warp points, set up a keybind in your `.zshrc`:

```zsh
bindkey '^G' wd_browse
bindkey ${FZF_WD_BINDKEY:-'^B'} fuzzy_wd_widget
```

## Usage
Expand All @@ -158,6 +158,19 @@ If a warp point with the same name exists, use `wd add foo --force` to overwrite
**Note:** a warp point cannot contain colons, or consist of only spaces and dots.
The first will conflict in how `wd` stores the warp points, and the second will conflict with other features, as below.

* Add warp point to any directory with default name:

```zsh
wd addcd /foo/ bar
```

* Add warp point to any directory with a custom name:

```zsh
wd addcd /foo/
```


You can omit point name to automatically use the current directory's name instead.

* From any directory, warp to `foo` with:
Expand Down
4 changes: 4 additions & 0 deletions _wd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function _wd() {

commands=(
'add:Adds the current working directory to your warp points'
'addcd:Adds a directory to your warp points'
'add!:Overwrites existing warp point'
'export:Export warp points as static named directories'
'rm:Removes the given warp point'
Expand Down Expand Up @@ -63,6 +64,9 @@ function _wd() {
add)
_message 'Write the name of your warp point' && ret=0
;;
addcd)
_message 'Write the name of your path' && ret=0
;;
show)
_describe -t points "Warp points" warp_points && ret=0
;;
Expand Down
6 changes: 6 additions & 0 deletions wd.1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Add a new warp point which will warp to the current working directory with curre
.IP "add, -a, --add <name>"
Add a new warp point which will warp to the current working directory with \fIname\fR as identifier.
.
.IP "addcd, -c, --addcd <path>"
Add a new warp point which will warp to the current working directory with current directory name as identifier.
.
.IP "addcd, -c, --addcd <path> <point>"
Add a new warp point which will warp to the current working directory with \fIname\fR as identifier.
.
.IP "rm, -r, --remove"
Remove any warp point with current directory name as identifier.
.
Expand Down
9 changes: 5 additions & 4 deletions wd.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

eval "wd() { source '${0:A:h}/wd.sh' }"
wd > /dev/null
# Register the function as a Zsh widget
zle -N wd_browse
# Bind the widget to a key combination
bindkey '^G' wd_browse
zle -N wd_browse_widget
zle -N wd_restore_buffer
autoload -Uz add-zle-hook-widget
add-zle-hook-widget line-init wd_restore_buffer
bindkey ${FZF_WD_BINDKEY:-'^B'} wd_browse_widget
114 changes: 100 additions & 14 deletions wd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ wd_print_msg()
then
local color="${1:-$WD_BLUE}" # Default to blue if no color is provided
local msg="$2"

if [[ -z "$msg" ]]; then
print "${WD_RED}*${WD_NOC} Could not print message. Sorry!"
else
Expand All @@ -74,18 +74,20 @@ wd_print_usage()
Usage: wd [command] [point]

Commands:
<point> Warps to the directory specified by the warp point
<point> <path> Warps to the directory specified by the warp point with path appended
add <point> Adds the current working directory to your warp points
add Adds the current working directory to your warp points with current directory's name
rm <point> Removes the given warp point
rm Removes the given warp point with current directory's name
show <point> Print path to given warp point
show Print warp points to current directory
list Print all stored warp points
ls <point> Show files from given warp point (ls)
path <point> Show the path to given warp point (pwd)
clean Remove points warping to nonexistent directories (will prompt unless --force is used)
<point> Warps to the directory specified by the warp point
<point> <path> Warps to the directory specified by the warp point with path appended
add <point> Adds the current working directory to your warp points
add Adds the current working directory to your warp points with current directory's name
addcd <path> Adds a path to your warp points with the directory's name
addcd <path> <point> Adds a path to your warp points with a custom name
rm <point> Removes the given warp point
rm Removes the given warp point with current directory's name
show <point> Print path to given warp point
show Print warp points to current directory
list Print all stored warp points
ls <point> Show files from given warp point (ls)
path <point> Show the path to given warp point (pwd)
clean Remove points warping to nonexistent directories (will prompt unless --force is used)

-v | --version Print version
-d | --debug Exit after execution with exit codes (for testing)
Expand Down Expand Up @@ -203,6 +205,67 @@ wd_add()
fi
}

wd_addcd()
{
local folder=$1
local point=$2
p1r473 marked this conversation as resolved.
Show resolved Hide resolved
local force=$3
cmdnames=(add rm show list ls path clean help)

# Check if the path is actually provided, if not, exit with an error
if [[ -z "$folder" ]]
then
wd_exit_fail "You must specify a path"
return
fi

# Check if the directory exists
if [[ ! -d "$folder" ]]
then
wd_exit_fail "The directory does not exist"
return
fi

if [[ $point == "" ]]
then
point=$(basename $folder)
fi

if [[ $point =~ "^[\.]+$" ]]
then
wd_exit_fail "Warp point cannot be just dots"
elif [[ $point =~ "[[:space:]]+" ]]
then
wd_exit_fail "Warp point should not contain whitespace"
elif [[ $point =~ : ]] || [[ $point =~ / ]]
then
wd_exit_fail "Warp point contains illegal character (:/)"
elif (($cmdnames[(Ie)$point]))
then
wd_exit_fail "Warp point name cannot be a wd command (see wd -h for a full list)"
elif [[ ${points[$point]} == "" ]] || [ ! -z "$force" ]
then
wd_remove "$point" > /dev/null
printf "%q:%s\n" "${point}" "${folder/#$HOME/~}" >> "$WD_CONFIG"
if (whence sort >/dev/null); then
local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX")
# use 'cat' below to ensure we respect $WD_CONFIG as a symlink
command sort -o "${config_tmp}" "$WD_CONFIG" && command cat "${config_tmp}" >| "$WD_CONFIG" && command rm "${config_tmp}"
fi

wd_export_static_named_directories

wd_print_msg "$WD_GREEN" "Warp point '$point' added for path '$folder'"

# override exit code in case wd_remove did not remove any points
# TODO: we should handle this kind of logic better
WD_EXIT_CODE=0
else
wd_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite."
fi
}


wd_remove()
{
local point_list=$1
Expand Down Expand Up @@ -243,6 +306,24 @@ wd_browse() {
fi
}

wd_browse_widget() {
if [[ -e $WD_CONFIG ]]; then
wd_browse
saved_buffer=$BUFFER
saved_cursor=$CURSOR
BUFFER=
zle redisplay
zle accept-line
fi
}

wd_restore_buffer() {
BUFFER=$saved_buffer
CURSOR=$saved_cursor
saved_buffer=
saved_cursor=1
}

wd_list_all()
{
wd_print_msg "$WD_BLUE" "All warp points:"
Expand Down Expand Up @@ -371,7 +452,7 @@ wd_export_static_named_directories() {
fi
}

local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc}
WD_CONFIG=${WD_CONFIG:-$HOME/.warprc}
local WD_QUIET=0
local WD_EXIT_CODE=0
local WD_DEBUG=0
Expand Down Expand Up @@ -455,6 +536,10 @@ else
wd_browse
break
;;
"-c"|"--addcd"|"addcd")
wd_addcd "$2" "$3" "$wd_force_mode"
alpha-tango-kilo marked this conversation as resolved.
Show resolved Hide resolved
break
;;
"-e"|"export")
wd_export_static_named_directories
break
Expand Down Expand Up @@ -510,6 +595,7 @@ fi
unset wd_extglob_is_set
unset wd_warp
unset wd_add
unset wd_addcd
unset wd_remove
unset wd_show
unset wd_list_all
Expand Down
Loading