zenity/msg_unix.go

99 lines
2.4 KiB
Go
Raw Permalink Normal View History

2020-01-08 13:12:29 -05:00
// +build !windows,!darwin
2020-01-04 22:21:39 -05:00
package zenity
2020-01-09 06:17:39 -05:00
import (
"os/exec"
"github.com/ncruces/zenity/internal/zen"
)
2020-01-04 22:21:39 -05:00
// Display question dialog.
2020-01-10 07:00:38 -05:00
//
// Returns true on OK, false on Cancel, or ErrExtraButton.
2020-01-10 07:00:38 -05:00
//
// Valid options: Title, Icon, OKLabel, CancelLabel, ExtraButton, NoWrap,
// Ellipsize, DefaultCancel.
func Question(text string, options ...Option) (bool, error) {
return message("--question", text, options)
2020-01-04 22:21:39 -05:00
}
2020-01-10 07:00:38 -05:00
// Display info dialog.
//
// Returns true on OK, false on dismiss, or ErrExtraButton.
//
// Valid options: Title, Icon, OKLabel, ExtraButton, NoWrap, Ellipsize.
2020-01-04 22:21:39 -05:00
func Info(text string, options ...Option) (bool, error) {
return message("--info", text, options)
}
// Display warning dialog.
2020-01-10 07:00:38 -05:00
//
// Returns true on OK, false on dismiss, or ErrExtraButton.
2020-01-10 07:00:38 -05:00
//
// Valid options: Title, Icon, OKLabel, ExtraButton, NoWrap, Ellipsize.
func Warning(text string, options ...Option) (bool, error) {
return message("--warning", text, options)
2020-01-04 22:21:39 -05:00
}
// Display error dialog.
2020-01-10 07:00:38 -05:00
//
// Returns true on OK, false on dismiss, or ErrExtraButton.
//
// Valid options: Title, Icon, OKLabel, ExtraButton, NoWrap, Ellipsize.
func Error(text string, options ...Option) (bool, error) {
return message("--error", text, options)
2020-01-04 22:21:39 -05:00
}
func message(arg, text string, options []Option) (bool, error) {
opts := optsParse(options)
2020-01-09 06:17:39 -05:00
args := []string{arg}
if text != "" {
args = append(args, "--text", text, "--no-markup")
}
2020-01-04 22:21:39 -05:00
if opts.title != "" {
args = append(args, "--title", opts.title)
}
if opts.ok != "" {
args = append(args, "--ok-label", opts.ok)
}
if opts.cancel != "" {
args = append(args, "--cancel-label", opts.cancel)
}
if opts.extra != "" {
args = append(args, "--extra-button", opts.extra)
}
if opts.nowrap {
args = append(args, "--no-wrap")
}
if opts.ellipsize {
args = append(args, "--ellipsize")
}
if opts.defcancel {
args = append(args, "--default-cancel")
}
switch opts.icon {
case ErrorIcon:
args = append(args, "--icon-name=dialog-error")
2020-01-12 18:09:11 -05:00
case WarningIcon:
args = append(args, "--icon-name=dialog-warning")
2020-01-04 22:21:39 -05:00
case InfoIcon:
args = append(args, "--icon-name=dialog-information")
case QuestionIcon:
args = append(args, "--icon-name=dialog-question")
}
2020-01-09 06:17:39 -05:00
out, err := zen.Run(args)
2020-01-06 07:35:45 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
if len(out) > 0 && string(out[:len(out)-1]) == opts.extra {
return false, ErrExtraButton
}
2020-01-04 22:21:39 -05:00
return false, nil
}
if err != nil {
return false, err
}
return true, err
}