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 { type Config struct {
Session string `yaml:"session"` Session string `yaml:"session"`
Env map[string]string `yaml:"env"` Env map[string]string `yaml:"env"`
Root string `yaml:"root"` Root string `yaml:"root"`
BeforeStart []string `yaml:"before_start"` BeforeStart []string `yaml:"before_start"`
Stop []string `yaml:"stop"` Stop []string `yaml:"stop"`
Windows []Window `yaml:"windows"` Windows []Window `yaml:"windows"`
RebalanceWindowsThreshold int `yaml:"rebalance_panes_after"`
} }
func addDefaultEnvs(c *Config, path string) { func addDefaultEnvs(c *Config, path string) {

@ -10,11 +10,6 @@ import (
const defaultWindowName = "smug_def" 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 { func ExpandPath(path string) string {
if strings.HasPrefix(path, "~/") { if strings.HasPrefix(path, "~/") {
userHome, err := os.UserHomeDir() userHome, err := os.UserHomeDir()
@ -120,12 +115,6 @@ func (smug Smug) Start(config *Config, options *Options, context Context) error
sessionExists := smug.tmux.SessionExists(sessionName) sessionExists := smug.tmux.SessionExists(sessionName)
sessionRoot := ExpandPath(config.Root) sessionRoot := ExpandPath(config.Root)
rebalancePanesThreshold := config.RebalanceWindowsThreshold
if rebalancePanesThreshold == 0 {
rebalancePanesThreshold = defaultRebalancePanesThreshold
}
windows := options.Windows windows := options.Windows
attach := options.Attach 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) paneRoot := ExpandPath(p.Root)
if paneRoot == "" || !filepath.IsAbs(p.Root) { if paneRoot == "" || !filepath.IsAbs(p.Root) {
paneRoot = filepath.Join(windowRoot, 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 return err
} }
for _, c := range p.Commands { if i%2 == 0 {
err = smug.tmux.SendKeys(window+"."+newPane, c) _, err = smug.tmux.SelectLayout(window, Tiled)
if err != nil { if err != nil {
return err return err
} }
} }
if pIndex+1 >= rebalancePanesThreshold { for _, c := range p.Commands {
_, err = smug.tmux.SelectLayout(window, Tiled) err = smug.tmux.SendKeys(window+"."+newPane, c)
if err != nil { if err != nil {
return err return err
} }
} }
} }

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

Loading…
Cancel
Save