Attach windows to current session with different name (#78)

master
Ivan 2 years ago committed by GitHub
parent 5a2fca4627
commit 3399f02a6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,7 @@
package main
import (
"errors"
"os"
"os/exec"
"path/filepath"
@ -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
@ -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)
}

@ -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",
@ -225,6 +225,7 @@ 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",
@ -232,7 +233,30 @@ var testTable = map[string]struct {
[]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"},
},
}

Loading…
Cancel
Save