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 all 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
51 changes: 51 additions & 0 deletions test/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,57 @@ test_valid_identifiers()
"$pipestatus"
}

test_wd_addcd()
{
# Create a base directory for testing
create_test_wp

# Test with basic directory
wd -q addcd "$WD_TEST_DIR"
assertTrue "should successfully add default wp for directory" \
"$(wp_exists "$(basename "$WD_TEST_DIR")")"

# Test with a specific warp point name
wd -q addcd "$WD_TEST_DIR" "$WD_TEST_WP_2"
assertTrue "should successfully add specified wp for directory" \
"$(wp_exists "$WD_TEST_WP_2")"

# Test with force option
wd -q addcd "$WD_TEST_DIR" -f
assertTrue "should successfully force-add wp for directory" \
"$(wp_exists "$(basename "$WD_TEST_DIR")")"

# Test with a specific warp point name and force option
wd -q addcd "$WD_TEST_DIR" "$WD_TEST_WP_2" -f
assertTrue "should successfully force-add specified wp for directory" \
"$(wp_exists "$WD_TEST_WP_2")"

# Test with absolute path
local abs_path="$PWD/$WD_TEST_DIR"
wd -q addcd "$abs_path"
assertTrue "should successfully add default wp for absolute directory path" \
"$(wp_exists "$(basename "$abs_path")")"

# Test with relative path including navigation
mkdir -p "$WD_TEST_DIR/nested/extra"
local rel_path="../$(basename "$PWD")/$WD_TEST_DIR"
cd "$WD_TEST_DIR/nested/extra"
wd -q addcd "$rel_path"
assertTrue "should successfully add wp for relative path with navigation" \
"$(wp_exists "$(basename "$rel_path")")"
cd - > /dev/null

# Test with nested directory paths
local nested_path="$WD_TEST_DIR/nested/extra"
wd -q addcd "$nested_path"
assertTrue "should successfully add wp for nested directory path" \
"$(wp_exists "$(basename "$nested_path")")"

# Cleanup
destroy_test_wp
}


test_removal()
{
wd -q add foo
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
58 changes: 44 additions & 14 deletions wd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# @github.com/mfaerevaag/wd

# version
readonly WD_VERSION=0.6.1
readonly WD_VERSION=0.5.2

# colors
readonly WD_BLUE="\033[96m"
Expand Down 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,28 @@ wd_add()
fi
}

wd_addcd() {
local folder=$1
local point=$2
p1r473 marked this conversation as resolved.
Show resolved Hide resolved
local force=$3
local currentdir=$PWD
alpha-tango-kilo marked this conversation as resolved.
Show resolved Hide resolved

if [[ -z "$folder" ]]; then
wd_exit_fail "You must specify a path"
return
fi

if [[ ! -d "$folder" ]]; then
wd_exit_fail "The directory does not exist"
return
fi

cd "$folder" || return
wd_add "$point" "$force"
cd "$currentdir" || return
}


wd_remove()
{
local point_list=$1
Expand Down Expand Up @@ -260,6 +284,7 @@ wd_restore_buffer() {
saved_buffer=
saved_cursor=1
}

wd_list_all()
{
wd_print_msg "$WD_BLUE" "All warp points:"
Expand Down Expand Up @@ -472,6 +497,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 @@ -527,6 +556,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