From f334702cb2cb283363a68ce4b77a49c0729b42f9 Mon Sep 17 00:00:00 2001 From: Willi Eggeling Date: Mon, 14 Jan 2019 12:12:07 +0100 Subject: [PATCH] added variable to allow configuration of mousetrap message duration new variable MousetrapDisplayDuration allows to modify the default display duration of 5s, or to completely disable the timeout and wait for the user to press the return key. --- cobra.go | 7 +++++++ command_win.go | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/cobra.go b/cobra.go index 7010fd15b..6505c070b 100644 --- a/cobra.go +++ b/cobra.go @@ -23,6 +23,7 @@ import ( "strconv" "strings" "text/template" + "time" "unicode" ) @@ -56,6 +57,12 @@ var MousetrapHelpText string = `This is a command line tool. You need to open cmd.exe and run it from there. ` +// MousetrapDisplayDuration controls how long the MousetrapHelpText message is displayed on Windows +// if the CLI is started from explorer.exe. Set to 0 to wait for the return key to be pressed. +// To disable the mousetrap, just set MousetrapHelpText to blank string (""). +// Works only on Microsoft Windows. +var MousetrapDisplayDuration time.Duration = 5 * time.Second + // AddTemplateFunc adds a template function that's available to Usage and Help // template generation. func AddTemplateFunc(name string, tmplFunc interface{}) { diff --git a/command_win.go b/command_win.go index edec728e4..8768b1736 100644 --- a/command_win.go +++ b/command_win.go @@ -3,6 +3,7 @@ package cobra import ( + "fmt" "os" "time" @@ -14,7 +15,12 @@ var preExecHookFn = preExecHook func preExecHook(c *Command) { if MousetrapHelpText != "" && mousetrap.StartedByExplorer() { c.Print(MousetrapHelpText) - time.Sleep(5 * time.Second) + if MousetrapDisplayDuration > 0 { + time.Sleep(MousetrapDisplayDuration) + } else { + c.Println("Press return to continue...") + fmt.Scanln() + } os.Exit(1) } }