ruby / irb

interactive Ruby
BSD 2-Clause "Simplified" License
393 stars 119 forks source link

Introduce cd command #971

Closed st0012 closed 4 months ago

st0012 commented 5 months ago

It's essentially a combination of pushws and popws commands that are easier to use.

Help message:

Usage: cd ([target]|..)

IRB uses a stack of workspaces to keep track of context(s), with `pushws` and `popws` commands to manipulate the stack.
The `cd` command is an attempt to simplify the operation and will be subject to change.

When given:
- an object, cd will use that object as the new context by pushing it onto the workspace stack.
- "..", cd will leave the current context by popping the top workspace off the stack.
- no arguments, cd will move to the top workspace on the stack by popping off all workspaces.

Examples:

  cd Foo
  cd Foo.new
  cd @ivar
  cd ..
  cd

Please note that it's not a replica of Pry's cd command as it doesn't support the / argument, nor complicated navigation syntax.

kmcphillips commented 5 months ago

I love this. Anytime I try to teach someone they can use binding.irb without a gem depencency to pry, I have to say "It's like Pry but..." and this removes one of the things on that list.

brandondrew commented 5 months ago
- "..", cd will leave the current context, moving back to the previous context.

I believe this is incorrect, based on both the assumption that this works similarly to cd .. in a shell, and based on the code shown above (in a previous comment), which shows it 'popping' contexts (which normally means moving up a level, unless the 'popping' metaphor is also very different in irb.) I've copied that code here:

        when ".."
          irb_context.pop_workspace

Popping does not necessarily take you to the previous workspace. You could be where you are because of using cd -, in which case the next level up might not be the previous workspace.

alpaca-tc commented 5 months ago

Thanks for the great PR 💛 I hope this PR will be merged because I also used the cd command in my .irbrc.

Currently, this PR doesn't support cd ../.. like pry. I would like this feature. Do you have any plans to add it in the future?

st0012 commented 5 months ago

Popping does not necessarily take you to the previous workspace. You could be where you are because of using cd -, in which case the next level up might not be the previous workspace.

It'll be the previous workspace on the same workspace stack. In this context, previous means the previous value that's been added to the stack (array). I know it's not perfectly accurate, but I can't think of better description for it and I'm open to better ideas 🙂

I would like this feature. Do you have any plans to add it in the future?

Unfortunately I don't plan to introduce nested syntax atm, such as cd @x/@y like Pry does. In the similar sense, I won't introduce multi-level exits yet.

st0012 commented 4 months ago

I couldn't 100% replicate Prys' cd - behaviour without making huge changes, which is against my goal to build a nicer interface based on the current implementation. Even if I were allowed to cut a few corners and deliver a simpler version of it, I wouldn't know where to cut and what to keep as I personally am not a user 😅 So I decided to drop it from the current PR and ship what we have first. If you really want to see cd -, please open an issue and describe the desire behaviour in details. Thanks 🙂