Skip to content

Commit

Permalink
Add option that allows to rebalance panes (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaaaan authored Apr 19, 2021
1 parent e81ece1 commit 6a55cc9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 6 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ type Window struct {
}

type Config struct {
Session string `yaml:"session"`
Root string `yaml:"root"`
BeforeStart []string `yaml:"before_start"`
Stop []string `yaml:"stop"`
Windows []Window `yaml:"windows"`
Session string `yaml:"session"`
Root string `yaml:"root"`
BeforeStart []string `yaml:"before_start"`
Stop []string `yaml:"stop"`
Windows []Window `yaml:"windows"`
RebalanceWindowsThreshold int `yaml:"rebalance_panes_after"`
}

func EditConfig(path string) error {
Expand Down
21 changes: 20 additions & 1 deletion smug.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (

const defaultWindowName = "smug_def"

// Very wisely picked default value,
// after which panes will be rebalanced for each `split-window`
// Helps with "no space for new pane" error
const defaultRebalancePanesThreshold = 5

func ExpandPath(path string) string {
if strings.HasPrefix(path, "~/") {
userHome, err := os.UserHomeDir()
Expand Down Expand Up @@ -90,6 +95,11 @@ func (smug Smug) Start(config Config, options Options, context Context) error {
windows := options.Windows
attach := options.Attach

rebalancePanesThreshold := config.RebalanceWindowsThreshold
if rebalancePanesThreshold == 0 {
rebalancePanesThreshold = defaultRebalancePanesThreshold
}

if !sessionExists {
err := smug.execShellCommands(config.BeforeStart, sessionRoot)
if err != nil {
Expand Down Expand Up @@ -126,13 +136,14 @@ func (smug Smug) Start(config Config, options Options, context Context) error {
}
}

for _, p := range w.Panes {
for pIndex, p := range w.Panes {
paneRoot := ExpandPath(p.Root)
if paneRoot == "" || !filepath.IsAbs(p.Root) {
paneRoot = filepath.Join(windowRoot, p.Root)
}

newPane, err := smug.tmux.SplitWindow(window, p.Type, paneRoot)

if err != nil {
return err
}
Expand All @@ -143,6 +154,14 @@ func (smug Smug) Start(config Config, options Options, context Context) error {
return err
}
}

if pIndex+1 >= rebalancePanesThreshold {
_, err = smug.tmux.SelectLayout(window, Tiled)
if err != nil {
return err
}

}
}

layout := w.Layout
Expand Down

0 comments on commit 6a55cc9

Please sign in to comment.