Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
# Changes the way topgrade interacts with
# the tmux session, creating the session
# and only attaching to it if not inside tmux
# (default: "attach_if_not_in_session", allowed values: "attach_if_not_in_session", "attach_always")
# (default: "attach_if_not_in_session", allowed values:
# - "attach_if_not_in_session" create and attach in a new unique session, only if isn't in another one
# - "attach_always" create and attach in a new unique session, even if in another one
# - "reattach_if_not_in_session" reuse or create a new session, and attach to if isn't in another one
# - "reattach_always" reuse or create a new session, and attach to, even if in another one)
Comment on lines +65 to +68
Copy link
Member

Choose a reason for hiding this comment

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

only if isn't in another one

Either grammatically incorrect or extremely hard to read. Please reword these comments

# tmux_session_mode = "attach_if_not_in_session"

# Cleanup temporary or old files (default: false)
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ pub struct Misc {
pub enum TmuxSessionMode {
AttachIfNotInSession,
AttachAlways,
ReattachIfNotInSession,
ReattachAlways,
Comment on lines 355 to +358
Copy link
Member

Choose a reason for hiding this comment

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

Instead of expanding this enum, could you make a new tmux_reattach: bool (or a different name if you like)? This would also remove the duplication that is currently in the match block.

}

pub struct TmuxConfig {
Expand Down
37 changes: 36 additions & 1 deletion src/steps/tmux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)?;
Copy link
Member

Choose a reason for hiding this comment

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

Please remove commented-out code

let session: String;
Copy link
Member

Choose a reason for hiding this comment

The 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)?;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
session = tmux.new_unique_session(session_name, window_name, &command)?;
let session = tmux.new_unique_session(session_name, window_name, &command)?;

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"));
Expand All @@ -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 {
Expand Down