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

Introduce cd command #971

Merged
merged 1 commit into from
Jul 3, 2024
Merged

Introduce cd command #971

merged 1 commit into from
Jul 3, 2024

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Jun 16, 2024

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.

@st0012 st0012 added the enhancement New feature or request label Jun 16, 2024
@st0012 st0012 self-assigned this Jun 16, 2024
lib/irb/command/cd.rb Outdated Show resolved Hide resolved
@st0012 st0012 force-pushed the introduce-cd-command branch 2 times, most recently from 0d273f3 to 4ac5305 Compare June 16, 2024 18:45
@kmcphillips
Copy link

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.

HELP

def execute(arg)
case arg

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the cd - case? As in, go to the previous but also push the current back on the stack.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the rest of the PR that's built on top of existing features, this requires adding new internal API and will not work nicely with other workspace commands like pushws...etc. I implemented it anyway but I'll let other maintainers decide if it's worth the extra complexity.

when ".."
irb_context.pop_workspace
when "-"
irb_context.replace_workspace(irb_context.previous_workspace)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To support this, we need a way to remember popped workspaces too. And because commands are not memoized, we can't use ivars here. So I needed to add a new previous_workspace attribute to Context just for this feature. Additionally, it will not work with other commands like pushws or chws without adding further complexities.

While I understand its use cases, I personally don't use it. So either keeping or dropping it is fine for me. Thoughts? @ruby/irb-reline

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth adding, or maybe we can add it later. Not particular about keeping or dropping it at the first step.

Since cd has a stack that represents directory-like model, I think cd - should restore the whole stack, not just the current workspace.

cd 1  # 1
cd 2  # 1/2
cd 3  # 1/2/3
cd .. # 1/2
cd -  # 1/2/3
cd    # main
cd -  # 1/2/3
cd .. # 1/2

And we need to consider irb_context.previous_workspace==nil case.

$ irb
irb(main):001> cd -

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I'll update my implementation 👍

@brandondrew
Copy link

brandondrew commented Jun 20, 2024

- "..", 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
Copy link
Contributor

alpaca-tc commented Jun 27, 2024

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
Copy link
Member Author

st0012 commented Jul 2, 2024

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.

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
```
@st0012
Copy link
Member Author

st0012 commented Jul 3, 2024

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 🙂

@st0012 st0012 requested a review from tompng July 3, 2024 16:32
@tompng tompng merged commit 4a0e0e8 into master Jul 3, 2024
59 checks passed
@tompng tompng deleted the introduce-cd-command branch July 3, 2024 17:17
matzbot pushed a commit to ruby/ruby that referenced this pull request Jul 3, 2024
(ruby/irb#971)

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
```

ruby/irb@4a0e0e89b7
alessandro-fazzi added a commit to alessandro-fazzi/irb that referenced this pull request Aug 1, 2024
Document `cd object` and `cd ..` commands introduced in 1.14.0 by ruby#971
alessandro-fazzi added a commit to alessandro-fazzi/irb that referenced this pull request Aug 2, 2024
Document `cd object` and `cd ..` commands introduced in 1.14.0 by ruby#971
alessandro-fazzi added a commit to alessandro-fazzi/irb that referenced this pull request Aug 2, 2024
Document `cd object` and `cd ..` commands introduced in 1.14.0 by ruby#971
alessandro-fazzi added a commit to alessandro-fazzi/irb that referenced this pull request Aug 2, 2024
Document `cd object` and `cd ..` commands introduced in 1.14.0 by ruby#971
alessandro-fazzi added a commit to alessandro-fazzi/irb that referenced this pull request Aug 2, 2024
Document `cd object` and `cd ..` commands introduced in 1.14.0 by ruby#971
st0012 pushed a commit that referenced this pull request Aug 4, 2024
Document `cd object` and `cd ..` commands introduced in 1.14.0 by #971
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

6 participants