add support for panesthesia
All checks were successful
continuous-integration/drone/push Build is passing

Close #12
This commit is contained in:
James Walker 2024-06-24 12:56:38 -04:00
parent b2a281d224
commit 468788594f
Signed by: walkah
SSH Key Fingerprint: SHA256:f7Gn4jO4BFHZxWfKTTzEAfWz+cLW51IyGFl9MjDyZGI
3 changed files with 45 additions and 2 deletions

7
tmux/pane.go Normal file
View File

@ -0,0 +1,7 @@
package tmux
type Pane struct {
Root string `yaml:"root,omitempty"`
Type string `yaml:"type,omitempty"`
Commands []string `yaml:"commands,omitempty"`
}

View File

@ -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}")

View File

@ -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, ";"))
}
}