Skip to content

Commit 67fc483

Browse files
thewillieparis
authored andcommitted
added variable to allow configuration of mousetrap message duration (#809)
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.
1 parent 5755ecf commit 67fc483

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

cobra.go

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strconv"
2424
"strings"
2525
"text/template"
26+
"time"
2627
"unicode"
2728
)
2829

@@ -56,6 +57,12 @@ var MousetrapHelpText string = `This is a command line tool.
5657
You need to open cmd.exe and run it from there.
5758
`
5859

60+
// MousetrapDisplayDuration controls how long the MousetrapHelpText message is displayed on Windows
61+
// if the CLI is started from explorer.exe. Set to 0 to wait for the return key to be pressed.
62+
// To disable the mousetrap, just set MousetrapHelpText to blank string ("").
63+
// Works only on Microsoft Windows.
64+
var MousetrapDisplayDuration time.Duration = 5 * time.Second
65+
5966
// AddTemplateFunc adds a template function that's available to Usage and Help
6067
// template generation.
6168
func AddTemplateFunc(name string, tmplFunc interface{}) {

command_win.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package cobra
44

55
import (
6+
"fmt"
67
"os"
78
"time"
89

@@ -14,7 +15,12 @@ var preExecHookFn = preExecHook
1415
func preExecHook(c *Command) {
1516
if MousetrapHelpText != "" && mousetrap.StartedByExplorer() {
1617
c.Print(MousetrapHelpText)
17-
time.Sleep(5 * time.Second)
18+
if MousetrapDisplayDuration > 0 {
19+
time.Sleep(MousetrapDisplayDuration)
20+
} else {
21+
c.Println("Press return to continue...")
22+
fmt.Scanln()
23+
}
1824
os.Exit(1)
1925
}
2026
}

0 commit comments

Comments
 (0)