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

refactor cds and _cds for readability and completeness #82

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

aryarm
Copy link
Collaborator

@aryarm aryarm commented Apr 24, 2019

The new cds

Ever since the creation of cds, I've struggled with creating a true wrapper function: one that completely emulates the behavior of the command it is wrapping (including error output) except when it can add some cool, additional functionality. In pr #56, I introduced some new code for cds that made it nearly perfect in this regard, making sure to preserve the error output of cd almost all the time, especially when we expect the additional functionality to fail.

There were two main drawbacks to that code, as I discussed in comment 26858406:

  1. It's incredibly ugly and completely unreadable to someone unfamiliar with the intricacies of shell/unix.
  2. It doesn't preserve the output of cd 100% of the time. Specifically, no error output would be shown if
    1. cd doesn't work,
    2. there exists a valid symlink with the specified shortcut name,
    3. and (for whatever reason) that symlink doesn't work either.

The current pr addresses problem 2 by calling cd a third time to retrieve the lost error output if it is needed. This should not considerably impact the overall speed of cds because this happens pretty rarely.
It also tries to address problem 1 by including comments in the code and refactoring a portion of the code that was particularly terse (and used lots of fancy file descriptor redirections when it didn't need to). Thus, this pr resolves #73.
The pr also improves portability in cds -, which used a number of bashisms. This fix resolves #86.

Unfortunately, I'm still not perfectly satisfied with the code. If possible, I hope a future me will find a far more elegant solution to this whole problem of perfect wrapping. At the moment, I have serious doubt that shells are currently capable of an elegant solution, since I've been searching for one ever since I started cds and although I've learned a lot, it seems everything boils down to one big problem: you can't capture the output of a command in a variable without putting the command in a subshell (unless you use non-elegant stuff like temporary files, named pipes, non-POSIX stuff, or background processes). Until this changes, we'll be stuck with the current implementation of cds.

The new _cds

As of 8/23/19, this PR also now contains a small refactor of _cds, the function responsible for adding bash tab completions to cds.
Users of _cds should see only one major change: tab completions will now work correctly when a directory with the same name as one of your shortcuts appears in the current directory. This finally resolves #75.
The implementation of _cds has changed considerably, though. As of 59462a2, _cds is now no longer dependent on realpath. In my personal opinion, realpath offers many features of path resolution and canonicalization that are only really helpful in rare situations. Portable, simple alternatives to realpath exist when basic guarantees can be made about the path that is being resolved (for example: knowing that all directories in the path will exist). For these reasons, I removed realpath from the repository in da05455. That should resolve #33 and close #83. @GiselleSerate, you can revert da05455 if you still want to keep realpath.

It will now display errors from cd if a shortcut exists with the given shortcut name but the shortcut doesn't work (for whatever reason)
@aryarm aryarm marked this pull request as ready for review April 24, 2019 05:39
@aryarm aryarm changed the title make cds completely foolproof! make cds readable and completely foolproof! Jul 14, 2019
Some shells apparently prioritize executing aliases for builtin commands before the builtin commands themselves
This can lead to infinite recursion when using cd aliased to cds.
I would like to allow the user as much flexibility as possible when
using cds. For example, it would be nice if people could override cd
before initializing cds with a cd function. But in this case, I'd like
to prioritize the overriding of cd with cds via an alias.
…t directory (and resolve #75)

Before this fix, _cds would add the shortcut dir to the completions twice
Also, use the dir in the current dir instead of the shortcut dir for all subdirectory completions
Realpath is the swiss army knife of path resolution. However, its use is probably overkill in most situations.
In this case, _cds used realpath to ensure that typing "cds <shortcut>/../[tab]" would yield completions for
the directory above the resolved shortcut and not the .short directory inside $ABSPATH.
However, we can achieve the same behavior with cd -P in this case because we don't care about symlink cycles,
parts of the path that don't exist, or any of the other extra features realpath offers.
In fact, we really only care about how cd -P will resolve the path, since cds uses cd -P anyway!
@aryarm aryarm changed the title make cds readable and completely foolproof! refactor cds and _cds for readability and bug fixes Aug 23, 2019
@aryarm aryarm changed the title refactor cds and _cds for readability and bug fixes refactor cds and _cds for readability and completeness Aug 23, 2019
@aryarm
Copy link
Collaborator Author

aryarm commented Aug 31, 2019

By the way, if we ever decide to create tests for this code, we should keep in mind this bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant