Skip to content

Commit

Permalink
#8 Always kill first window from list-windows command (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaaaan authored Dec 22, 2020
1 parent 880c448 commit cfbd86c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 6 additions & 1 deletion smug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions smug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down Expand Up @@ -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",
},
Expand Down
12 changes: 12 additions & 0 deletions tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"os"
"os/exec"
"strings"
)

const (
Expand Down Expand Up @@ -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
}

0 comments on commit cfbd86c

Please sign in to comment.