Skip to content

Commit

Permalink
Attach windows to current session with different name (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaaaan authored Feb 11, 2022
1 parent 5a2fca4 commit 3399f02
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
29 changes: 23 additions & 6 deletions smug.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -100,19 +101,35 @@ func (smug Smug) Stop(config Config, options Options, context Context) error {
}

func (smug Smug) Start(config Config, options Options, context Context) error {
sessionName := config.Session + ":"
var sessionName string
var err error

createWindowsInsideCurrSession := options.InsideCurrentSession
if createWindowsInsideCurrSession && !context.InsideTmuxSession {
return errors.New("cannot use -i flag outside of a tmux session")
}

sessionName = config.Session
if createWindowsInsideCurrSession {
sessionName, err = smug.tmux.SessionName()
if err != nil {
return err
}
}
sessionName = sessionName + ":"

sessionExists := smug.tmux.SessionExists(sessionName)
sessionRoot := ExpandPath(config.Root)

windows := options.Windows
attach := options.Attach

rebalancePanesThreshold := config.RebalanceWindowsThreshold
if rebalancePanesThreshold == 0 {
rebalancePanesThreshold = defaultRebalancePanesThreshold
}

if !sessionExists {
windows := options.Windows
attach := options.Attach

if !sessionExists && !createWindowsInsideCurrSession {
err := smug.execShellCommands(config.BeforeStart, sessionRoot)
if err != nil {
return err
Expand All @@ -127,7 +144,7 @@ func (smug Smug) Start(config Config, options Options, context Context) error {
if err != nil {
return err
}
} else if len(windows) == 0 && !options.InsideCurrentSession {
} else if len(windows) == 0 && !createWindowsInsideCurrSession {
return smug.switchOrAttach(sessionName, attach, context.InsideTmuxSession)
}

Expand Down
28 changes: 26 additions & 2 deletions smug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ var testTable = map[string]struct {
},
[]string{""},
},
"test create new windows in current session": {
"test create new windows in current session with same name": {
Config{
Session: "ses",
Root: "root",
Expand All @@ -225,14 +225,38 @@ var testTable = map[string]struct {
},
Context{InsideTmuxSession: true},
[]string{
"tmux display-message -p #S",
"tmux has-session -t ses:",
"tmux neww -Pd -t ses: -c root -F #{window_id} -n win1",
"tmux select-layout -t even-horizontal",
},
[]string{
"tmux kill-session -t ses",
},
[]string{""},
[]string{"ses", ""},
},
"test create new windows in current session with different name": {
Config{
Session: "ses",
Root: "root",
Windows: []Window{
{Name: "win1"},
},
},
Options{
InsideCurrentSession: true,
},
Context{InsideTmuxSession: true},
[]string{
"tmux display-message -p #S",
"tmux has-session -t ses:",
"tmux neww -Pd -t ses: -c root -F #{window_id} -n win1",
"tmux select-layout -t win1 even-horizontal",
},
[]string{
"tmux kill-session -t ses",
},
[]string{"ses", "win1"},
},
}

Expand Down

0 comments on commit 3399f02

Please sign in to comment.