Create a default window, and then kill it, instead of using (#23)

* Create a default window, and then kill it, instead of using
master
Ivan 3 years ago committed by GitHub
parent 0880b52738
commit 40106e141d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,7 +8,6 @@ type Context struct {
func CreateContext() Context { func CreateContext() Context {
_, tmux := os.LookupEnv("TMUX") _, tmux := os.LookupEnv("TMUX")
os.Environ()
insideTmuxSession := os.Getenv("TERM") == "screen" || tmux insideTmuxSession := os.Getenv("TERM") == "screen" || tmux
return Context{insideTmuxSession} return Context{insideTmuxSession}
} }

@ -42,7 +42,7 @@ func main() {
} }
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Cannot parse command line otions: %q", err.Error()) fmt.Fprintf(os.Stderr, "Cannot parse command line options: %q", err.Error())
os.Exit(1) os.Exit(1)
} }
@ -80,7 +80,6 @@ func main() {
commander := DefaultCommander{logger} commander := DefaultCommander{logger}
tmux := Tmux{commander} tmux := Tmux{commander}
smug := Smug{tmux, commander} smug := Smug{tmux, commander}
context := CreateContext() context := CreateContext()
switch options.Command { switch options.Command {

@ -4,9 +4,12 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
) )
const defaultWindowName = "smug_def"
func ExpandPath(path string) string { func ExpandPath(path string) string {
if strings.HasPrefix(path, "~/") { if strings.HasPrefix(path, "~/") {
userHome, err := os.UserHomeDir() userHome, err := os.UserHomeDir()
@ -48,11 +51,11 @@ func (smug Smug) execShellCommands(commands []string, path string) error {
return nil return nil
} }
func (smug Smug) switchOrAttach(sessionName string, attach bool, insideTmuxSession bool) error { func (smug Smug) switchOrAttach(target string, attach bool, insideTmuxSession bool) error {
if insideTmuxSession && attach { if insideTmuxSession && attach {
return smug.tmux.SwitchClient(sessionName) return smug.tmux.SwitchClient(target)
} else if !insideTmuxSession { } else if !insideTmuxSession {
return smug.tmux.Attach(sessionName, os.Stdin, os.Stdout, os.Stderr) return smug.tmux.Attach(target, os.Stdin, os.Stdout, os.Stderr)
} }
return nil return nil
} }
@ -94,14 +97,7 @@ func (smug Smug) Start(config Config, options Options, context Context) error {
return err return err
} }
var defaultWindowName string _, err = smug.tmux.NewSession(config.Session, sessionRoot, defaultWindowName)
if len(windows) > 0 {
defaultWindowName = windows[0]
} else if len(config.Windows) > 0 {
defaultWindowName = config.Windows[0].Name
}
_, err = smug.tmux.NewSession(strings.Replace(sessionName, ":", "", 1), sessionRoot, defaultWindowName)
if err != nil { if err != nil {
return err return err
} }
@ -109,7 +105,7 @@ func (smug Smug) Start(config Config, options Options, context Context) error {
return smug.switchOrAttach(sessionName, attach, context.InsideTmuxSession) return smug.switchOrAttach(sessionName, attach, context.InsideTmuxSession)
} }
for wIndex, w := range config.Windows { for _, w := range config.Windows {
if (len(windows) == 0 && w.Manual) || (len(windows) > 0 && !Contains(windows, w.Name)) { if (len(windows) == 0 && w.Manual) || (len(windows) > 0 && !Contains(windows, w.Name)) {
continue continue
} }
@ -120,11 +116,9 @@ func (smug Smug) Start(config Config, options Options, context Context) error {
} }
window := sessionName + w.Name window := sessionName + w.Name
if (!sessionExists && wIndex > 0 && len(windows) == 0) || (sessionExists && len(windows) > 0) { _, err := smug.tmux.NewWindow(sessionName, w.Name, windowRoot)
_, err := smug.tmux.NewWindow(sessionName, w.Name, windowRoot) if err != nil {
if err != nil { return err
return err
}
} }
for _, c := range w.Commands { for _, c := range w.Commands {
@ -134,31 +128,41 @@ func (smug Smug) Start(config Config, options Options, context Context) error {
} }
} }
for _, p := range w.Panes { layout := w.Layout
if layout == "" {
layout = EvenHorizontal
}
_, err = smug.tmux.SelectLayout(sessionName+w.Name, layout)
if err != nil {
return err
}
for pIndex, p := range w.Panes {
paneRoot := ExpandPath(p.Root) paneRoot := ExpandPath(p.Root)
if paneRoot == "" || !filepath.IsAbs(p.Root) { if paneRoot == "" || !filepath.IsAbs(p.Root) {
paneRoot = filepath.Join(windowRoot, p.Root) paneRoot = filepath.Join(windowRoot, p.Root)
} }
_, err := smug.tmux.SplitWindow(window, p.Type, paneRoot, p.Commands) _, err := smug.tmux.SplitWindow(window, p.Type, paneRoot)
if err != nil { if err != nil {
return err return err
} }
}
layout := w.Layout
if layout == "" {
layout = EvenHorizontal
}
_, err := smug.tmux.SelectLayout(sessionName+w.Name, layout) for _, c := range p.Commands {
if err != nil { err = smug.tmux.SendKeys(window+"."+strconv.Itoa(pIndex+1), c)
return err if err != nil {
return err
}
}
} }
} }
if len(windows) == 0 { smug.tmux.KillWindow(sessionName + defaultWindowName)
return smug.switchOrAttach(sessionName, attach, context.InsideTmuxSession) smug.tmux.RenumberWindows(sessionName)
if len(windows) == 0 && len(config.Windows) > 0 {
return smug.switchOrAttach(sessionName+config.Windows[0].Name, attach, context.InsideTmuxSession)
} }
return nil return nil

@ -20,6 +20,11 @@ var testTable = []struct {
Session: "ses", Session: "ses",
Root: "root", Root: "root",
BeforeStart: []string{"command1", "command2"}, BeforeStart: []string{"command1", "command2"},
Windows: []Window{
{
Name: "win1",
},
},
}, },
Options{ Options{
Windows: []string{}, Windows: []string{},
@ -29,8 +34,12 @@ var testTable = []struct {
"tmux has-session -t ses:", "tmux has-session -t ses:",
"/bin/sh -c command1", "/bin/sh -c command1",
"/bin/sh -c command2", "/bin/sh -c command2",
"tmux new -Pd -s ses -n -c root", "tmux new -Pd -s ses -n smug_def -c root",
"tmux attach -d -t ses:", "tmux neww -Pd -t ses: -n win1 -c root",
"tmux select-layout -t ses:win1 even-horizontal",
"tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:",
"tmux attach -d -t ses:win1",
}, },
[]string{ []string{
"tmux kill-session -t ses", "tmux kill-session -t ses",
@ -67,10 +76,13 @@ var testTable = []struct {
Context{}, Context{},
[]string{ []string{
"tmux has-session -t ses:", "tmux has-session -t ses:",
"tmux new -Pd -s ses -n win1 -c root", "tmux new -Pd -s ses -n smug_def -c root",
"tmux split-window -Pd -t ses:win1 -c root -h", "tmux neww -Pd -t ses: -n win1 -c root",
"tmux select-layout -t ses:win1 main-horizontal", "tmux select-layout -t ses:win1 main-horizontal",
"tmux attach -d -t ses:", "tmux split-window -Pd -t ses:win1 -c root -h",
"tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:",
"tmux attach -d -t ses:win1",
}, },
[]string{ []string{
"/bin/sh -c stop1", "/bin/sh -c stop1",
@ -100,8 +112,11 @@ var testTable = []struct {
Context{}, Context{},
[]string{ []string{
"tmux has-session -t ses:", "tmux has-session -t ses:",
"tmux new -Pd -s ses -n win2 -c root", "tmux new -Pd -s ses -n smug_def -c root",
"tmux neww -Pd -t ses: -n win2 -c root",
"tmux select-layout -t ses:win2 even-horizontal", "tmux select-layout -t ses:win2 even-horizontal",
"tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:",
}, },
[]string{ []string{
"tmux kill-window -t ses:win2", "tmux kill-window -t ses:win2",
@ -131,7 +146,8 @@ var testTable = []struct {
Context{}, Context{},
[]string{ []string{
"tmux has-session -t ses:", "tmux has-session -t ses:",
"tmux new -Pd -s ses -n win1 -c root", "tmux new -Pd -s ses -n smug_def -c root",
"tmux neww -Pd -t ses: -n win1 -c root",
"tmux send-keys -t ses:win1 command1 Enter", "tmux send-keys -t ses:win1 command1 Enter",
"tmux send-keys -t ses:win1 command2 Enter", "tmux send-keys -t ses:win1 command2 Enter",
"tmux select-layout -t ses:win1 even-horizontal", "tmux select-layout -t ses:win1 even-horizontal",
@ -139,19 +155,62 @@ var testTable = []struct {
"tmux send-keys -t ses:win2 command3 Enter", "tmux send-keys -t ses:win2 command3 Enter",
"tmux send-keys -t ses:win2 command4 Enter", "tmux send-keys -t ses:win2 command4 Enter",
"tmux select-layout -t ses:win2 even-horizontal", "tmux select-layout -t ses:win2 even-horizontal",
"tmux attach -d -t ses:", "tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:",
"tmux attach -d -t ses:win1",
},
[]string{
"tmux kill-session -t ses",
},
"xyz",
},
{
Config{
Session: "ses",
Root: "root",
Windows: []Window{
{
Name: "win1",
Manual: false,
Root: "./win1",
Panes: []Pane{
{
Root: "pane1",
Type: "vertical",
Commands: []string{
"command1",
},
},
},
},
},
},
Options{},
Context{},
[]string{
"tmux has-session -t ses:",
"tmux new -Pd -s ses -n smug_def -c root",
"tmux neww -Pd -t ses: -n win1 -c root/win1",
"tmux select-layout -t ses:win1 even-horizontal",
"tmux split-window -Pd -t ses:win1 -c root/win1/pane1 -v",
"tmux send-keys -t ses:win1.1 command1 Enter",
"tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:",
"tmux attach -d -t ses:win1",
}, },
[]string{ []string{
"tmux kill-session -t ses", "tmux kill-session -t ses",
}, },
"xyz", "xyz",
}, },
{ {
Config{ Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
BeforeStart: []string{"command1", "command2"}, BeforeStart: []string{"command1", "command2"},
Windows: []Window{
{Name: "win1"},
},
}, },
Options{}, Options{},
Context{}, Context{},
@ -168,13 +227,22 @@ var testTable = []struct {
Config{ Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
Windows: []Window{
{
Name: "win1",
},
},
}, },
Options{Attach: true}, Options{Attach: true},
Context{InsideTmuxSession: true}, Context{InsideTmuxSession: true},
[]string{ []string{
"tmux has-session -t ses:", "tmux has-session -t ses:",
"tmux new -Pd -s ses -n -c root", "tmux new -Pd -s ses -n smug_def -c root",
"tmux switch-client -t ses:", "tmux neww -Pd -t ses: -n win1 -c root",
"tmux select-layout -t ses:win1 even-horizontal",
"tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:",
"tmux switch-client -t ses:win1",
}, },
[]string{ []string{
"tmux kill-session -t ses", "tmux kill-session -t ses",
@ -190,7 +258,9 @@ var testTable = []struct {
Context{InsideTmuxSession: true}, Context{InsideTmuxSession: true},
[]string{ []string{
"tmux has-session -t ses:", "tmux has-session -t ses:",
"tmux new -Pd -s ses -n -c root", "tmux new -Pd -s ses -n smug_def -c root",
"tmux kill-window -t ses:smug_def",
"tmux move-window -r -s ses: -t ses:",
}, },
[]string{ []string{
"tmux kill-session -t ses", "tmux kill-session -t ses",
@ -201,6 +271,9 @@ var testTable = []struct {
Config{ Config{
Session: "ses", Session: "ses",
Root: "root", Root: "root",
Windows: []Window{
{Name: "win1"},
},
}, },
Options{Attach: true}, Options{Attach: true},
Context{InsideTmuxSession: true}, Context{InsideTmuxSession: true},

@ -47,8 +47,7 @@ func (tmux Tmux) NewWindow(target string, name string, root string) (string, err
func (tmux Tmux) SendKeys(target string, command string) error { func (tmux Tmux) SendKeys(target string, command string) error {
cmd := exec.Command("tmux", "send-keys", "-t", target, command, "Enter") cmd := exec.Command("tmux", "send-keys", "-t", target, command, "Enter")
_, err := tmux.commander.Exec(cmd) return tmux.commander.ExecSilently(cmd)
return err
} }
func (tmux Tmux) Attach(target string, stdin *os.File, stdout *os.File, stderr *os.File) error { func (tmux Tmux) Attach(target string, stdin *os.File, stdout *os.File, stderr *os.File) error {
@ -62,12 +61,12 @@ func (tmux Tmux) Attach(target string, stdin *os.File, stdout *os.File, stderr *
} }
func (tmux Tmux) RenumberWindows(target string) error { func (tmux Tmux) RenumberWindows(target string) error {
cmd := exec.Command("tmux", "move-window", "-r") cmd := exec.Command("tmux", "move-window", "-r", "-s", target, "-t", target)
_, err := tmux.commander.Exec(cmd) _, err := tmux.commander.Exec(cmd)
return err return err
} }
func (tmux Tmux) SplitWindow(target string, splitType string, root string, commands []string) (string, error) { func (tmux Tmux) SplitWindow(target string, splitType string, root string) (string, error) {
args := []string{"split-window", "-Pd", "-t", target, "-c", root} args := []string{"split-window", "-Pd", "-t", target, "-c", root}
switch splitType { switch splitType {
@ -84,13 +83,6 @@ func (tmux Tmux) SplitWindow(target string, splitType string, root string, comma
return "", err return "", err
} }
for _, c := range commands {
err = tmux.SendKeys(pane, c)
if err != nil {
return "", err
}
}
return pane, nil return pane, nil
} }

Loading…
Cancel
Save