Skip to content

Commit

Permalink
CDPATH support
Browse files Browse the repository at this point in the history
  • Loading branch information
osteensco committed Oct 11, 2024
1 parent de5dc19 commit fec0ac6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@

<h3>CD to a directory super fast</h4>

Typing out a long directory path can be a bit painful. Most tools that solve this problem give you less to type but still involve additional steps afterwards. Instead of tracking frequently visited directories, using a fuzzy search, or even a ML model to improve the cd experience, fastTravelCLI allows you to directly save a destination as a key-value pair, then you can fast travel to that location by just using the key you set.
Typing out a long directory path can be a bit painful. Most tools that solve this problem give you less to type but still involve additional steps afterwards.
Instead of tracking frequently visited directories, using a fuzzy search, or even a ML model to improve the cd experience, fastTravelCLI allows you to directly save a destination as a key-value pair, then you can fast travel to that location by just using the key you set.
fastTravelCLI is a true CD command replacement with additional features. Check out the issues for even more features on the way.
<br></br>
fastTravelCLI takes an experience like this:

Zoxide is a great solution but the implementation is complex leading to many issues that cause it to be less than ideal for many use cases.
Here are a few examples of issues that fastTravelCLI solves gracefully or avoids entirely.
https://github.com/ajeetdsouza/zoxide/issues/620 - CDPATH support
https://github.com/ajeetdsouza/zoxide/issues/876 - Differentiating similarly named directories
https://github.com/ajeetdsouza/zoxide/issues/839 - Session history stack navigation
https://github.com/ajeetdsouza/zoxide/issues/863 - Navigation local to project

<br></br>
If you use fzf to find a deeply nested directory, fastTravelCLI takes an experience like this:
```
cd $(find * -type d | fzf)
```
Expand Down Expand Up @@ -50,14 +61,6 @@ ft wknotes
<br></br>


<!-- Zoxide is a great solution but the implementation is complex leading to many issues that cause it to be less than ideal for many use cases. -->
<!-- Here are a few examples of issues that fastTravelCLI solves gracefully or avoids entirely. -->
<!-- https://github.com/ajeetdsouza/zoxide/issues/620 - CDPATH support -->
<!-- https://github.com/ajeetdsouza/zoxide/issues/876 - Differentiating similarly named directories -->
<!-- https://github.com/ajeetdsouza/zoxide/issues/839 - Session history stack navigation -->
<!-- https://github.com/ajeetdsouza/zoxide/issues/863 - Navigation local to project -->

<br></br>

<h1>Usage</h1>
<br></br>
Expand All @@ -83,10 +86,10 @@ ft [key]/some/subdir
ft relative/dir
ft ..
ft -
ft ./dir
# or
ft dir/

# ft supports relative paths in the working directory and CDPATH

ft dir

# ft allows you to visit previously visited directories

Expand Down
27 changes: 27 additions & 0 deletions ft/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -60,6 +61,7 @@ func PassCmd(args []string) ([]string, error) {
return args[1:], nil
}

// changeDirectory can handle key lookup, relative paths, directories in CDPATH, and key evaluation.
func changeDirectory(data *CmdArgs) error {
if len(data.allPaths) == 0 {
fmt.Print(NoLocationsSetMsg)
Expand Down Expand Up @@ -103,8 +105,33 @@ func changeDirectory(data *CmdArgs) error {
} else {

key = provided_string
// handles key lookup
p, ok := data.allPaths[key]
if !ok {

// handles releative directory in CWD
dir, err := os.Stat(key)
if err == nil {
if dir.IsDir() {
fmt.Println(key)
return nil
}
}

// handles CDPATH
cdpath := os.Getenv("CDPATH")
if cdpath != "" {
cdpaths := strings.Split(cdpath, ":")
for _, path := range cdpaths {
cdPathResult := filepath.Join(path, key)
dir, err := os.Stat(cdPathResult)
if err == nil && dir.IsDir() {
fmt.Println(cdPathResult)
return nil
}
}
}

fmt.Printf(UnrecognizedKeyMsg, key, key, key)
return nil
}
Expand Down
29 changes: 29 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ func TestMainFunc(t *testing.T) {
t.Fatalf("Failed to navigate to temp directory")
}

// CDPATH setup
homeDir, err := os.UserHomeDir()
if err != nil {
t.Fatalf("Failed to retrieve home directory: %v", err)
}
cdpathdir, err := os.MkdirTemp(homeDir, "cdpathdir")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}

defer os.RemoveAll(cdpathdir)

cdpathtest, err := os.MkdirTemp(cdpathdir, "cdpathtest")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}

cdpathtestkey := filepath.Base(cdpathtest)

cdpath := os.Getenv("CDPATH")
err = os.Setenv("CDPATH", fmt.Sprint(cdpathdir, ":", cdpath))

// tests
tests := []struct {
name string
args []string
Expand Down Expand Up @@ -102,6 +125,12 @@ func TestMainFunc(t *testing.T) {
expected: "[\n",
wantErr: false,
},
{
name: "7. Check cd command with CDPATH.",
args: []string{"ft", cdpathtestkey},
expected: fmt.Sprintf("%v\n", cdpathtest),
wantErr: false,
},
// {
// []string{"ft", "rn", "key", "key2"},
// "key renamed to key2",
Expand Down

0 comments on commit fec0ac6

Please sign in to comment.