diff --git a/smug.go b/smug.go index 2026926..4b683b9 100644 --- a/smug.go +++ b/smug.go @@ -132,7 +132,12 @@ func (smug Smug) Start(config Config, windows []string) error { } if len(windows) == 0 { - err = smug.tmux.KillWindow(ses + "0") + windows, err := smug.tmux.ListWindows(ses) + if err != nil { + return err + } + + err = smug.tmux.KillWindow(ses + windows[0]) if err != nil { return err } diff --git a/smug_test.go b/smug_test.go index d8c65cb..696869f 100644 --- a/smug_test.go +++ b/smug_test.go @@ -24,7 +24,8 @@ var testTable = []struct { "/bin/sh -c command1", "/bin/sh -c command2", "tmux new -Pd -s ses", - "tmux kill-window -t ses:0", + "tmux list-windows -t ses: -F #{window_index}", + "tmux kill-window -t ses:ses:", "tmux move-window -r", "tmux attach -t ses:0", }, @@ -65,7 +66,8 @@ var testTable = []struct { "tmux neww -Pd -t ses: -n win1 -c root", "tmux split-window -Pd -t ses: -c root -h", "tmux select-layout -t ses:win1 main-horizontal", - "tmux kill-window -t ses:0", + "tmux list-windows -t ses: -F #{window_index}", + "tmux kill-window -t ses:ses:", "tmux move-window -r", "tmux attach -t ses:0", }, diff --git a/tmux.go b/tmux.go index b4e997b..f283c66 100644 --- a/tmux.go +++ b/tmux.go @@ -3,6 +3,7 @@ package main import ( "os" "os/exec" + "strings" ) const ( @@ -115,3 +116,14 @@ func (tmux Tmux) StopSession(target string) (string, error) { cmd := exec.Command("tmux", "kill-session", "-t", target) return tmux.commander.Exec(cmd) } + +func (tmux Tmux) ListWindows(target string) ([]string, error) { + cmd := exec.Command("tmux", "list-windows", "-t", target, "-F", "#{window_index}") + + output, err := tmux.commander.Exec(cmd) + if err != nil { + return []string{}, err + } + + return strings.Split(output, "\n"), nil +}