Skip to content

Commit

Permalink
Add support for layout in windows (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaaaan authored Dec 21, 2020
1 parent f85d2fc commit 880c448
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ windows:
- name: code
root: blog # a relative path to root
manual: true # you can start this window only manually, using the -w arg
layout: main-vertical
commands:
- docker-compose start
panes:
Expand All @@ -91,6 +92,7 @@ windows:

- name: infrastructure
root: ~/Developer/blog/my-microservices
layout: tiled
panes:
- type: horizontal
root: .
Expand All @@ -114,6 +116,7 @@ stop:

windows:
- name: code
layout: main-horizontal
commands:
- vim app/dependencies.php
panes:
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type Window struct {
BeforeStart []string `yaml:"before_start"`
Panes []Pane `yaml:"panes"`
Commands []string `yaml:"commands"`
Manual bool
Layout string `yaml:"layout"`
Manual bool `yaml:"manual"`
}

type Config struct {
Expand Down
10 changes: 10 additions & 0 deletions smug.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ func (smug Smug) Start(config Config, windows []string) error {
return err
}
}

layout := w.Layout
if layout == "" {
layout = EvenHorizontal
}

_, err = smug.tmux.SelectLayout(ses+w.Name, layout)
if err != nil {
return err
}
}

if len(windows) == 0 {
Expand Down
10 changes: 10 additions & 0 deletions smug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,17 @@ var testTable = []struct {
{
Name: "win1",
Manual: false,
Layout: "main-horizontal",
Panes: []Pane{
Pane{
Type: "horizontal",
},
},
},
{
Name: "win2",
Manual: true,
Layout: "tiled",
},
},
Stop: []string{
Expand All @@ -56,6 +63,8 @@ var testTable = []struct {
"tmux has-session -t ses",
"tmux new -Pd -s ses",
"tmux neww -Pd -t ses: -n win1 -c root",
"tmux split-window -Pd -t ses: -c root -h",
"tmux select-layout -t ses:win1 main-horizontal",
"tmux kill-window -t ses:0",
"tmux move-window -r",
"tmux attach -t ses:0",
Expand Down Expand Up @@ -86,6 +95,7 @@ var testTable = []struct {
"tmux has-session -t ses",
"tmux new -Pd -s ses",
"tmux neww -Pd -t ses: -n win2 -c root",
"tmux select-layout -t ses:win2 even-horizontal",
},
[]string{
"tmux kill-window -t ses:win2",
Expand Down
13 changes: 13 additions & 0 deletions tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const (
HSplit = "horizontal"
)

const (
EvenHorizontal = "even-horizontal"
EvenVertical = "even-vertical"
MainHorizontal = "main-horizontal"
MainVertical = "main-vertical"
Tiled = "tiled"
)

type Tmux struct {
commander Commander
}
Expand Down Expand Up @@ -98,6 +106,11 @@ func (tmux Tmux) SplitWindow(target string, splitType string, root string, comma
return pane, nil
}

func (tmux Tmux) SelectLayout(target string, layoutType string) (string, error) {
cmd := exec.Command("tmux", "select-layout", "-t", target, layoutType)
return tmux.commander.Exec(cmd)
}

func (tmux Tmux) StopSession(target string) (string, error) {
cmd := exec.Command("tmux", "kill-session", "-t", target)
return tmux.commander.Exec(cmd)
Expand Down

0 comments on commit 880c448

Please sign in to comment.