go-prompt/_example/sleep/main.go

41 lines
624 B
Go
Raw Normal View History

2017-07-23 15:35:13 +00:00
package main
import (
"fmt"
"time"
2017-08-07 09:48:33 +00:00
"context"
"os/exec"
2017-07-23 15:35:13 +00:00
2017-08-07 15:19:51 +00:00
"github.com/c-bata/go-prompt"
2017-07-23 15:35:13 +00:00
)
2017-08-07 09:48:33 +00:00
func executor(t string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
2017-07-23 15:35:13 +00:00
defer cancel()
if t == "sleep 5s" {
cmd := exec.CommandContext(ctx, "sleep", "5")
cmd.Run()
} else if t == "sleep 20s" {
cmd := exec.CommandContext(ctx, "sleep", "20")
cmd.Run()
}
2017-08-06 06:31:44 +00:00
return
2017-07-23 15:35:13 +00:00
}
2017-08-04 11:30:50 +00:00
func completer(t string) []prompt.Suggest {
return []prompt.Suggest{
2017-07-23 15:35:13 +00:00
{Text: "sleep 5s"},
{Text: "sleep 20s"},
}
}
func main() {
2017-08-07 15:19:51 +00:00
p := prompt.New(
2017-07-23 15:35:13 +00:00
executor,
completer,
)
defer fmt.Println("\nGoodbye!")
2017-08-07 15:19:51 +00:00
p.Run()
2017-07-23 15:35:13 +00:00
}