From f313e5b6fb39b22641c07a43a5c9ddd073227874 Mon Sep 17 00:00:00 2001 From: Marwan Sulaiman Date: Sat, 29 May 2021 10:57:44 -0400 Subject: [PATCH] Export pane --- scaffold/scaffold.go | 53 ++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/scaffold/scaffold.go b/scaffold/scaffold.go index daf395b..13e40a2 100644 --- a/scaffold/scaffold.go +++ b/scaffold/scaffold.go @@ -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 ( @@ -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) @@ -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 } @@ -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) } @@ -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