From 80205349b5b052504f3e143a126ff73b3b1bd917 Mon Sep 17 00:00:00 2001 From: spike Date: Fri, 30 Sep 2022 22:50:42 +0200 Subject: [PATCH] set a custom timeout before executing send-keys for commands In some situations send-keys might fire before the shell prompt finished initilization. It happened for me when using fish shell and *oh-my-fish* which makes the send-keys start executing bofore my shell is ready and I endup with a written command that is not executed This patch adds a custom `sendkeys_timeout` as a top level option that takes an integer in milliseconds. It will ensure a Sleep call is made before. --- config.go | 13 +++++++------ config_test.go | 2 ++ smug.go | 3 +++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/config.go b/config.go index 92ee8bc..b219369 100644 --- a/config.go +++ b/config.go @@ -27,12 +27,13 @@ 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"` + SendKeysTimeout int `yaml:"sendkeys_timeout"` + 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) { diff --git a/config_test.go b/config_test.go index c794741..b2c8f0f 100644 --- a/config_test.go +++ b/config_test.go @@ -9,6 +9,7 @@ import ( func TestParseConfig(t *testing.T) { yaml := ` session: ${session} +sendkeys_timeout: 200 windows: - layout: tiled commands: @@ -28,6 +29,7 @@ windows: expected := Config{ Session: "test", + SendKeysTimeout: 200, Env: make(map[string]string), Windows: []Window{ { diff --git a/smug.go b/smug.go index 6d2bd95..4c6acdc 100644 --- a/smug.go +++ b/smug.go @@ -6,6 +6,7 @@ import ( "os/exec" "path/filepath" "strings" + "time" ) const defaultWindowName = "smug_def" @@ -153,6 +154,7 @@ func (smug Smug) Start(config *Config, options *Options, context Context) error } for _, c := range w.Commands { + time.Sleep(time.Millisecond * time.Duration(config.SendKeysTimeout)) err := smug.tmux.SendKeys(window, c) if err != nil { return err @@ -180,6 +182,7 @@ func (smug Smug) Start(config *Config, options *Options, context Context) error } for _, c := range p.Commands { + time.Sleep(time.Millisecond * time.Duration(config.SendKeysTimeout)) err = smug.tmux.SendKeys(window+"."+newPane, c) if err != nil { return err