dundalek / closh

Bash-like shell based on Clojure
Eclipse Public License 1.0
1.61k stars 65 forks source link

Add directory stack to cd #145

Open tramzee opened 5 years ago

tramzee commented 5 years ago

I like to use cd - to flip back and forth between two directories.

This implements a very rudimentary directory stack that allows an easy way to return to a previous directory. It also adds a dh command that allows one to view the stack.

dundalek commented 5 years ago

Thanks for the contribution :+1:

I need to do more research to see how the directory stack works in other shells, but from the top of my mind they usually also have popd and pushd built-ins. I will get to a deeper review hopefully during the weekend.

dundalek commented 5 years ago

When reading more about directory stack in other shells I also learned that cd sets the OLDPWD environment variable, which is currently notimplemented. So in case of switching to last directory we could just swap PWD and OLDPWD to enable the functionality. Supporting cd - seems useful to me.

I have a question, do you just use cd to jump to last directory or do you use the functionality to jump to a directory deeper in the stack? I try not to blindly copy every feature and personally have never used the directory stack.

Comments to the proposed implementation:

Conlusion

I see two options: 1) To support flipping back and forth between two directories with cd - we can add OLDPWD and flip it with PWD. 2) To add proper implementation of the directory stack would require more work, and I am not sure if it is that useful.

@tramzee Would 1) be enough to cover your usage?


Sources:

tramzee commented 5 years ago

@dundalek I use the directory stack deeper than just one. However, I would say that 90% of my usage is cd -. Having said that I can certainly live without any stack whatsoever. Completion makes changing directories easy enough without excessive amounts of typing.

tramzee commented 5 years ago

Also, I would be willing to tale a cut at doing the full directory stack implementation (pushd, popd, and dirs).

dundalek commented 5 years ago

@tramzee Cool, that makes sense. I am also interested in having the full dir stack implementation, so that would be great.

FYI I refactored the built-ins a bit so that they are now defined with defcmd which is cleaner than being hardcoded. Also it should be now possible to implement more customizations in user space in ~/.closhrc. For example I think it could be an interesting experiment to try to implement the whole dir stack functionality via defcmds in ~/.closhrc.