refactor: Rebalance panes after each 2nd split

master
Ivan 2 years ago committed by GitHub
parent 66dc299a82
commit 35425aa081
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,13 +27,12 @@ type Window struct {
}
type Config struct {
Session string `yaml:"session"`
Env map[string]string `yaml:"env"`
Root string `yaml:"root"`
BeforeStart []string `yaml:"before_start"`
Stop []string `yaml:"stop"`
Windows []Window `yaml:"windows"`
RebalanceWindowsThreshold int `yaml:"rebalance_panes_after"`
Session string `yaml:"session"`
Env map[string]string `yaml:"env"`
Root string `yaml:"root"`
BeforeStart []string `yaml:"before_start"`
Stop []string `yaml:"stop"`
Windows []Window `yaml:"windows"`
}
func addDefaultEnvs(c *Config, path string) {

@ -10,11 +10,6 @@ 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()
@ -120,12 +115,6 @@ func (smug Smug) Start(config *Config, options *Options, context Context) error
sessionExists := smug.tmux.SessionExists(sessionName)
sessionRoot := ExpandPath(config.Root)
rebalancePanesThreshold := config.RebalanceWindowsThreshold
if rebalancePanesThreshold == 0 {
rebalancePanesThreshold = defaultRebalancePanesThreshold
}
windows := options.Windows
attach := options.Attach
@ -170,7 +159,7 @@ func (smug Smug) Start(config *Config, options *Options, context Context) error
}
}
for pIndex, p := range w.Panes {
for i, p := range w.Panes {
paneRoot := ExpandPath(p.Root)
if paneRoot == "" || !filepath.IsAbs(p.Root) {
paneRoot = filepath.Join(windowRoot, p.Root)
@ -182,19 +171,19 @@ func (smug Smug) Start(config *Config, options *Options, context Context) error
return err
}
for _, c := range p.Commands {
err = smug.tmux.SendKeys(window+"."+newPane, c)
if i%2 == 0 {
_, err = smug.tmux.SelectLayout(window, Tiled)
if err != nil {
return err
}
}
if pIndex+1 >= rebalancePanesThreshold {
_, err = smug.tmux.SelectLayout(window, Tiled)
for _, c := range p.Commands {
err = smug.tmux.SendKeys(window+"."+newPane, c)
if err != nil {
return err
}
}
}

@ -111,6 +111,7 @@ var testTable = map[string]struct {
"tmux new -Pd -s ses -n smug_def -c root",
"tmux neww -Pd -t ses: -c root -F #{window_id} -n win1",
"tmux split-window -Pd -h -t win1 -c root -F #{pane_id}",
"tmux select-layout -t win1 tiled",
"tmux send-keys -t win1.1 command1 Enter",
"tmux select-layout -t win1 main-horizontal",
"tmux kill-window -t ses:smug_def",

Loading…
Cancel
Save