From 3399f02a6e01324f5bb881f6b049c9e8d94733ee Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 11 Feb 2022 21:44:18 +0400 Subject: [PATCH] Attach windows to current session with different name (#78) --- smug.go | 29 +++++++++++++++++++++++------ smug_test.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/smug.go b/smug.go index 8e347dc..3e6e6fc 100644 --- a/smug.go +++ b/smug.go @@ -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) } diff --git a/smug_test.go b/smug_test.go index f2fbb4d..f67f1c5 100644 --- a/smug_test.go +++ b/smug_test.go @@ -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"}, }, }