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

feat: tmux session attach mode #901

Merged
merged 10 commits into from
Sep 17, 2024

Conversation

wetfloo
Copy link
Contributor

@wetfloo wetfloo commented Sep 5, 2024

What does this PR do

This will allow the user to choose between just creating a session when in tmux, or switching the client to the newly created session. It's useful for my workflow revolving around tmux, where having forced a separate session with no quick way to attach to it created friction

Standards checklist

  • The PR title is descriptive.
  • I have read CONTRIBUTING.md
  • Optional: I have tested the code myself

For new steps

  • Optional: Topgrade skips this step where needed
  • Optional: The --dry-run option works with this step
  • Optional: The --yes option works with this step if it is supported by
    the underlying command

If you developed a feature or a bug fix for someone else and you do not have the
means to test it, please tag this person here.

@SteveLauC
Copy link
Member

Hi, sorry for the late reply!

Would you mind telling me the difference between tmux attach-session and tmux switch-session, I tried it locally, seems like after running the command, I just "attached" to the newly-created topgrade session🤔

@wetfloo
Copy link
Contributor Author

wetfloo commented Sep 16, 2024

Thanks for the reply! Basically, here's how it works
if the mode is create_and_switch_client, no matter if we're in- or outside of tmux, it will always attach to the new session
if the mode is create, it will only attach to the new session when outside of tmux, just like before (that's why it's picked as a default)

Here's a video that, hopefully, demonstrates that.

out.mp4

@SteveLauC
Copy link
Member

If the mode is create_and_switch_client, no matter if we're in- or outside of tmux, it will always attach to the new session

I get it, thanks for the explanation and the demo video:)

Copy link
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

General impl looks good, left some comments:)

# the newly crated tmux session, creating the session
# and only attaching to it if not inside tmux
# (default: create, allowed values: "create", "create_and_switch_client")
# tmux_session_attach_mode = "create"
Copy link
Member

Choose a reason for hiding this comment

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

I am thinking about the naming, what about we call them:

  1. AttachIfNotInASession
  2. AlwasyAttach

I think switch_client is too technical, thoughts on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed some names, also dropping attach infix from tmux params, to not repeat it

src/config.rs Outdated
Comment on lines 728 to 730
/// The preferred way to run the new tmux session
#[arg(long = "tmux-session-attach-mode")]
tmux_session_attach_mode: Option<TmuxSessionAttachMode>,
Copy link
Member

Choose a reason for hiding this comment

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

Is it necessary to add this CLI option, I think this feature is more like a static feature, one won't need to frequently change it 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

src/config.rs Outdated
@@ -967,6 +986,19 @@ impl Config {
.unwrap_or(false)
}

/// The preferred way to run the new tmux session.
pub fn tmux_session_attach_mode(&self) -> TmuxSessionAttachMode {
Copy link
Member

Choose a reason for hiding this comment

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

We should make this function private as well if we want developers to use .tmux_config() function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 158 to 169
// Only attach to the newly-created session if we're not currently in a tmux session.
TmuxSessionAttachMode::Create if is_inside_tmux => {
println!("Topgrade launched in a new tmux session");
return Ok(());
}
TmuxSessionAttachMode::CreateAndSwitchClient if is_inside_tmux => {
tmux.build().args(["switch-client", "-t", &session]).exec()
}
TmuxSessionAttachMode::Create | TmuxSessionAttachMode::CreateAndSwitchClient => {
tmux.build().args(["attach-session", "-t", &session]).exec()
}
};
Copy link
Member

Choose a reason for hiding this comment

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

What about writing it in this way, would it be a little bit clearer?

TmuxSessionAttachModule::Create => {
    if is_inside_tmux {
        println!("Topgrade launched in a new tmux session");
        return Ok(());
    } else {
        tmux.build().args(["attach-session", "-t", &session]).exec()
    }
}
TmuxSessionAttachModule::CreateAndSwitchClient => {
     if is_inside_tmux {
        tmux.build().args(["switch-client", "-t", &session]).exec()
     } else {
        tmux.build().args(["attach-session", "-t", &session]).exec()
     }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can do it if it's easier, just felt bad to repeat attach-session call twice

Copy link
Member

Choose a reason for hiding this comment

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

just felt bad to repeat attach-session call twice

Yeah, that's indeed one drawback. Though I prefer readability, which will make maintenance easier:)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for implementing the feature!

@SteveLauC SteveLauC merged commit 21751aa into topgrade-rs:main Sep 17, 2024
11 checks passed
@wetfloo wetfloo deleted the feat/tmux_attach_mode branch September 18, 2024 02:38
@lucaspar lucaspar mentioned this pull request Oct 6, 2024
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants