Skip to content

Commit

Permalink
Export pane
Browse files Browse the repository at this point in the history
  • Loading branch information
marwan-at-work committed May 29, 2021
1 parent 8617afb commit f313e5b
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions scaffold/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// window with tabs, pains, env vars, and other
// configurations to get your multi-terminal program
// up fast.
//
// This Package is EXPERIMENTAL and its APIs are likely
// to change before becoming stable.
package scaffold

import (
Expand All @@ -20,6 +23,12 @@ type WindowSpec struct {
// Run takes a window spec and creates a new iTerm2 session and uses it
// to create a new window with the given specs.
func Run(appName string, w WindowSpec) error {
if w.Title == "" {
return fmt.Errorf("window must have a title")
}
if len(w.Tabs) == 0 {
return fmt.Errorf("window must have at least 1 tab")
}
app, err := iterm2.NewApp(appName)
if err != nil {
return fmt.Errorf("iterm2.NewApp: %w", err)
Expand Down Expand Up @@ -55,6 +64,18 @@ func Run(appName string, w WindowSpec) error {
if err != nil {
return fmt.Errorf("createTab: %w", err)
}
windowTabs, err := window.ListTabs()
if err != nil {
return err
}
ss, err := windowTabs[0].ListSessions()
if err != nil {
return fmt.Errorf("first tab sessions: %w", err)
}
err = ss[0].Activate(true, true)
if err != nil {
return fmt.Errorf("session.Activate: %w", err)
}
return nil
}

Expand All @@ -68,31 +89,35 @@ func createTab(tab iterm2.Tab, ts TabSpec) error {
return fmt.Errorf("tab.ListSessions: %w", err)
}
sesh := sessions[0]
err = sesh.SendText(fmt.Sprintf("cd %v\n", ts.Dir))
if err != nil {
return fmt.Errorf("error changing directory: %w", err)
}
for _, e := range ts.Env.GetEnv() {
err = sesh.SendText(fmt.Sprintf("export %s\n", e))
if ts.Dir != "" {
err = sesh.SendText(fmt.Sprintf("cd %v\n", ts.Dir))
if err != nil {
return fmt.Errorf("error exporting env: %w", err)
return fmt.Errorf("error changing directory: %w", err)
}
}
if ts.Env != nil {
for _, e := range ts.Env.GetEnv() {
err = sesh.SendText(fmt.Sprintf("export %s\n", e))
if err != nil {
return fmt.Errorf("error exporting env: %w", err)
}
}
}
if ts.OnCreate != nil {
err = sesh.SendText(ts.OnCreate(sesh))
err = ts.OnCreate(sesh)
if err != nil {
return fmt.Errorf("ts.OnCreate: %w", err)
return err
}
}
if ts.pane != nil {
if ts.Pane != nil {
pane, err := sesh.SplitPane(iterm2.SplitPaneOptions{
Vertical: true,
})
if err != nil {
return fmt.Errorf("sesh.SplitPane: %w", err)
}
if ts.pane.OnCreate != nil {
err = ts.pane.OnCreate(pane)
if ts.Pane.OnCreate != nil {
err = ts.Pane.OnCreate(pane)
if err != nil {
return fmt.Errorf("pane.OnCreate: %w", err)
}
Expand Down Expand Up @@ -127,8 +152,8 @@ type TabSpec struct {
Title string
Dir string
Env EnvGetter
OnCreate func(s iterm2.Session) string
pane *PaneSpec
OnCreate func(s iterm2.Session) error
Pane *PaneSpec
}

// PaneSpec specifies a vertical right pane within a tab
Expand Down

0 comments on commit f313e5b

Please sign in to comment.