messagebox.go 870 B

123456789101112131415161718192021222324252627282930313233
  1. //+build !windows
  2. package lorca
  3. import (
  4. "fmt"
  5. "os/exec"
  6. "runtime"
  7. "strings"
  8. "syscall"
  9. )
  10. func messageBox(title, text string) bool {
  11. if runtime.GOOS == "linux" {
  12. err := exec.Command("zenity", "--question", "--title", title, "--text", text).Run()
  13. if err != nil {
  14. if exitError, ok := err.(*exec.ExitError); ok {
  15. return exitError.Sys().(syscall.WaitStatus).ExitStatus() == 0
  16. }
  17. }
  18. } else if runtime.GOOS == "darwin" {
  19. script := `set T to button returned of ` +
  20. `(display dialog "%s" with title "%s" buttons {"No", "Yes"} default button "Yes")`
  21. out, err := exec.Command("osascript", "-e", fmt.Sprintf(script, text, title)).Output()
  22. if err != nil {
  23. if exitError, ok := err.(*exec.ExitError); ok {
  24. return exitError.Sys().(syscall.WaitStatus).ExitStatus() == 0
  25. }
  26. }
  27. return strings.TrimSpace(string(out)) == "Yes"
  28. }
  29. return false
  30. }