diff --git a/tmux/pane.go b/tmux/pane.go new file mode 100644 index 0000000..62cda58 --- /dev/null +++ b/tmux/pane.go @@ -0,0 +1,7 @@ +package tmux + +type Pane struct { + Root string `yaml:"root,omitempty"` + Type string `yaml:"type,omitempty"` + Commands []string `yaml:"commands,omitempty"` +} diff --git a/tmux/tmux.go b/tmux/tmux.go index c11fe4e..a532bf0 100644 --- a/tmux/tmux.go +++ b/tmux/tmux.go @@ -9,6 +9,11 @@ import ( "syscall" ) +const ( + VerticalSplit = "vertical" + HorizontalSplit = "horizontal" +) + type Tmux struct { BinPath string Debug bool @@ -51,6 +56,24 @@ func (t *Tmux) Attach(name string) error { return nil } +func (t *Tmux) SendKeys(target string, command string) error { + _, err := t.Exec("send-keys", "-t", target, command, "Enter") + return err +} + +func (t *Tmux) SplitWindow(target string, split string, root string) error { + args := []string{"split-window"} + switch split { + case VerticalSplit: + args = append(args, "-v") + case HorizontalSplit: + args = append(args, "-h") + } + args = append(args, "-t", target, "-c", root) + _, err := t.Exec(args...) + return err +} + func (t *Tmux) ListSessions() ([]string, error) { sessions := []string{} result, err := t.Exec("ls", "-F", "#{session_name}") diff --git a/tmux/window.go b/tmux/window.go index c669ed5..92eae4f 100644 --- a/tmux/window.go +++ b/tmux/window.go @@ -9,16 +9,29 @@ type Window struct { Root string `yaml:"root,omitempty"` Commands []string `yaml:"commands,omitempty"` ID string `yaml:"-"` + Panes []Pane `yaml:"panes,omitempty"` } func (w *Window) Create(tmux *Tmux) { tmux.Run("new-window", "-t", w.ID, "-n", w.Name, "-c", w.Root) + + for i, pane := range w.Panes { + if i > 0 { + err := tmux.SplitWindow(w.ID, pane.Type, w.Root) + if err != nil { + panic(err) + } + } + + if len(pane.Commands) > 0 { + tmux.SendKeys(w.ID, strings.Join(pane.Commands, ";")) + } + } } func (w *Window) SendCommands(tmux *Tmux) { if len(w.Commands) > 0 { - tmux.Run("send-keys", "-t", w.ID, strings.Join(w.Commands, ";")) - tmux.Run("send-keys", "-t", w.ID, "Enter") + tmux.SendKeys(w.ID, strings.Join(w.Commands, ";")) } }