Skip to content

Commit

Permalink
Only use cd for moving to directories (will support any patterns the …
Browse files Browse the repository at this point in the history
…current shell supports)
  • Loading branch information
alexandrebodin committed Jul 11, 2018
1 parent 2853734 commit 7cb2dc9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 62 deletions.
2 changes: 1 addition & 1 deletion __examples__/simple.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name="simple-session"
dir="~/"
dir="~/dev"
select-window=""
select-pane=1
clear-panes=true
Expand Down
11 changes: 10 additions & 1 deletion __examples__/single-window.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@ dir="~/Desktop"

[[windows]]
name="window-one"
dir="~/"
dir="~/dev"

[[windows]]
dir="~/dev/bloom"

[[windows.panes]]
[[windows.panes]]
dir="~/dev/bloom/dashboard"

[[windows]]
29 changes: 0 additions & 29 deletions builder/dir.go

This file was deleted.

2 changes: 1 addition & 1 deletion builder/pane.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newPane(win *window, config config.Pane, index int) *pane {
}

if config.Dir != "" {
pane.Dir = lookupDir(config.Dir)
pane.Dir = config.Dir
} else {
pane.Dir = win.Dir
}
Expand Down
35 changes: 17 additions & 18 deletions builder/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ type Session struct {
func NewSession(config config.Session, options *tmux.Options) *Session {
sess := &Session{
Name: config.Name,
Dir: lookupDir(config.Dir),
Dir: config.Dir,
ClearPanes: config.ClearPanes,
WindowScripts: config.WindowScripts,
SelectWindow: config.SelectWindow,
SelectPane: config.SelectPane,
TmuxOptions: options,
}

for _, winConfig := range config.Windows {
window := newWindow(sess, winConfig)
for idx, winConfig := range config.Windows {
window := newWindow(sess, winConfig, idx)
sess.Windows = append(sess.Windows, window)
}

Expand All @@ -51,23 +51,17 @@ func (sess *Session) Start() error {
return err
}

args := []string{"new-session", "-d", "-s", sess.Name, "-x", width, "-y", height}
if len(sess.Windows) == 0 {
_, err = tmux.Exec("new-session", "-d", "-s", sess.Name, "-c", sess.Dir, "-x", width, "-y", height)
_, err = tmux.Exec()
} else {

firstWindow := sess.Windows[0]
_, err = tmux.Exec("new-session", "-d", "-s", sess.Name, "-c", sess.Dir, "-n", firstWindow.Name, "-x", width, "-y", height)
if err != nil {
return fmt.Errorf("starting session: %v", err)
}
args = append(args, "-n", firstWindow.Name)
}

if firstWindow.Dir != sess.Dir {
cdCmd := fmt.Sprintf("cd %s", firstWindow.Dir)
err := tmux.SendKeys(firstWindow.Target, cdCmd)
if err != nil {
return fmt.Errorf("moving window to dir %s: %v", firstWindow.Dir, err)
}
}
_, err = tmux.Exec(args...)
if err != nil {
return fmt.Errorf("starting session: %v", err)
}

if len(sess.Windows) > 1 {
Expand All @@ -80,14 +74,19 @@ func (sess *Session) Start() error {
}

for _, win := range sess.Windows {
err := tmux.SendKeys(win.Target, fmt.Sprintf("cd %s", win.Dir))
if err != nil {
return fmt.Errorf("moving window to dir %s: %v", win.Dir, err)
}

for _, script := range sess.WindowScripts {
err := tmux.SendKeys(win.Target, script)
if err != nil {
return err
return fmt.Errorf("running window scripts: %v", err)
}
}

err := win.init()
err = win.init()
if err != nil {
return fmt.Errorf("initializing window: %v", err)
}
Expand Down
33 changes: 21 additions & 12 deletions builder/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package builder

import (
"fmt"
"strconv"
"strings"

"github.com/alexandrebodin/tmuxctl/config"
"github.com/alexandrebodin/tmuxctl/tmux"
)

type window struct {
Idx int
Sess *Session
Name string
Dir string
Expand All @@ -20,19 +22,25 @@ type window struct {
Target string
}

func newWindow(sess *Session, config config.Window) *window {
func newWindow(sess *Session, config config.Window, idx int) *window {
win := &window{
Idx: idx,
Sess: sess,
Name: config.Name,
Layout: config.Layout,
Sync: config.Sync,
Scripts: config.Scripts,
PaneScripts: config.PaneScripts,
Target: sess.Name + ":" + config.Name,
}

if config.Name == "" {
win.Target = sess.Name + ":" + strconv.Itoa(idx+sess.TmuxOptions.BaseIndex)
} else {
win.Target = sess.Name + ":" + config.Name
}

if config.Dir != "" {
win.Dir = lookupDir(config.Dir)
win.Dir = config.Dir
} else {
win.Dir = sess.Dir
}
Expand Down Expand Up @@ -131,16 +139,8 @@ func (w *window) renderPane() error {
return nil
}

firstPane := w.Panes[0]
if firstPane.Dir != "" && firstPane.Dir != w.Dir { // we need to move the pane
err := tmux.SendKeys(firstPane.Target, "cd "+firstPane.Dir)
if err != nil {
return err
}
}

for _, pane := range w.Panes[1:] {
args := []string{"split-window", "-t", w.Target, "-c", pane.Dir}
args := []string{"split-window", "-t", w.Target}

if pane.Split != "" {
args = append(args, strings.Split(pane.Split, " ")...)
Expand All @@ -151,6 +151,15 @@ func (w *window) renderPane() error {
}
}

for _, pane := range w.Panes {
if pane.Dir != w.Dir {
err := tmux.SendKeys(pane.Target, "cd "+pane.Dir)
if err != nil {
return err
}
}
}

return nil
}

Expand Down

0 comments on commit 7cb2dc9

Please sign in to comment.