-
Notifications
You must be signed in to change notification settings - Fork 203
feat: add two more tmux session mode #1359
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -354,6 +354,8 @@ pub struct Misc { | |
| pub enum TmuxSessionMode { | ||
| AttachIfNotInSession, | ||
| AttachAlways, | ||
| ReattachIfNotInSession, | ||
| ReattachAlways, | ||
|
Comment on lines
355
to
+358
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of expanding this enum, could you make a new |
||
| } | ||
|
|
||
| pub struct TmuxConfig { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -117,6 +117,21 @@ impl Tmux { | |||||
| unreachable!() | ||||||
| } | ||||||
|
|
||||||
| fn new_same_session(&self, session_name: &str, window_name: &str, command: &str) -> Result<String> { | ||||||
| let session = session_name.to_owned(); | ||||||
|
|
||||||
| if !self | ||||||
| .has_session(&session) | ||||||
| .context("Error determining if a tmux session exists")? | ||||||
| { | ||||||
| self.new_session(&session, window_name, command) | ||||||
| .context("Error running Topgrade in tmux")?; | ||||||
| return Ok(session); | ||||||
| } | ||||||
|
|
||||||
| Ok(session) | ||||||
| } | ||||||
|
|
||||||
| /// Create a new window in the given tmux session, running the given command. | ||||||
| fn new_window(&self, session_name: &str, window_name: &str, command: &str) -> Result<()> { | ||||||
| self.build() | ||||||
|
|
@@ -168,11 +183,13 @@ pub fn run_in_tmux(config: TmuxConfig) -> Result<()> { | |||||
| // Find an unused session and run `topgrade` in it with the current command's arguments. | ||||||
| let session_name = "topgrade"; | ||||||
| let window_name = "topgrade"; | ||||||
| let session = tmux.new_unique_session(session_name, window_name, &command)?; | ||||||
| // let session = tmux.new_unique_session(session_name, window_name, &command)?; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove commented-out code |
||||||
| let session: String; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not move this to L192? |
||||||
|
|
||||||
| let is_inside_tmux = env::var("TMUX").is_ok(); | ||||||
| let err = match config.session_mode { | ||||||
| TmuxSessionMode::AttachIfNotInSession => { | ||||||
| session = tmux.new_unique_session(session_name, window_name, &command)?; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if is_inside_tmux { | ||||||
| // Only attach to the newly-created session if we're not currently in a tmux session. | ||||||
| println!("{}", t!("Topgrade launched in a new tmux session")); | ||||||
|
|
@@ -183,6 +200,24 @@ pub fn run_in_tmux(config: TmuxConfig) -> Result<()> { | |||||
| } | ||||||
|
|
||||||
| TmuxSessionMode::AttachAlways => { | ||||||
| session = tmux.new_unique_session(session_name, window_name, &command)?; | ||||||
| if is_inside_tmux { | ||||||
| tmux.build().args(["switch-client", "-t", &session]).exec() | ||||||
| } else { | ||||||
| tmux.build().args(["attach-session", "-t", &session]).exec() | ||||||
| } | ||||||
| } | ||||||
| TmuxSessionMode::ReattachIfNotInSession => { | ||||||
| session = tmux.new_same_session(session_name, window_name, &command)?; | ||||||
| if is_inside_tmux { | ||||||
| println!("{}", t!("Topgrade launched in a new tmux session")); | ||||||
| return Ok(()); | ||||||
| } else { | ||||||
| tmux.build().args(["attach-session", "-t", &session]).exec() | ||||||
| } | ||||||
| } | ||||||
| TmuxSessionMode::ReattachAlways => { | ||||||
| session = tmux.new_same_session(session_name, window_name, &command)?; | ||||||
| if is_inside_tmux { | ||||||
| tmux.build().args(["switch-client", "-t", &session]).exec() | ||||||
| } else { | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either grammatically incorrect or extremely hard to read. Please reword these comments