zenity/msg_unix.go

80 lines
2.0 KiB
Go
Raw 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 (
2021-03-05 21:29:00 -05:00
"bytes"
2020-01-09 06:17:39 -05:00
"os/exec"
2021-02-12 09:24:13 -05:00
"strconv"
2020-01-09 06:17:39 -05:00
2020-01-19 06:57:05 -05:00
"github.com/ncruces/zenity/internal/zenutil"
2020-01-09 06:17:39 -05:00
)
2020-01-04 22:21:39 -05:00
2021-03-04 07:42:30 -05:00
func message(kind messageKind, text string, opts options) (bool, error) {
2021-03-05 08:56:16 -05:00
args := []string{"--text", text, "--no-markup"}
2020-01-23 06:44:28 -05:00
switch kind {
case questionKind:
args = append(args, "--question")
case infoKind:
args = append(args, "--info")
case warningKind:
args = append(args, "--warning")
case errorKind:
args = append(args, "--error")
}
2021-03-04 07:35:27 -05:00
if opts.title != nil {
args = append(args, "--title", *opts.title)
2020-01-04 22:21:39 -05:00
}
2021-02-12 09:24:13 -05:00
if opts.width > 0 {
args = append(args, "--width", strconv.FormatUint(uint64(opts.width), 10))
}
if opts.height > 0 {
args = append(args, "--height", strconv.FormatUint(uint64(opts.height), 10))
}
2021-03-04 07:35:27 -05:00
if opts.okLabel != nil {
args = append(args, "--ok-label", *opts.okLabel)
2020-01-04 22:21:39 -05:00
}
2021-03-04 07:35:27 -05:00
if opts.cancelLabel != nil {
args = append(args, "--cancel-label", *opts.cancelLabel)
2020-01-04 22:21:39 -05:00
}
2021-03-04 07:35:27 -05:00
if opts.extraButton != nil {
args = append(args, "--extra-button", *opts.extraButton)
2020-01-04 22:21:39 -05:00
}
2020-01-24 07:52:45 -05:00
if opts.noWrap {
2020-01-04 22:21:39 -05:00
args = append(args, "--no-wrap")
}
if opts.ellipsize {
args = append(args, "--ellipsize")
}
2020-01-24 07:52:45 -05:00
if opts.defaultCancel {
2020-01-04 22:21:39 -05:00
args = append(args, "--default-cancel")
}
switch opts.icon {
case ErrorIcon:
2020-01-26 11:31:23 -05:00
args = append(args, "--window-icon=error", "--icon-name=dialog-error")
2020-01-12 18:09:11 -05:00
case WarningIcon:
2020-01-26 11:31:23 -05:00
args = append(args, "--window-icon=warning", "--icon-name=dialog-warning")
2020-01-04 22:21:39 -05:00
case InfoIcon:
2020-01-26 11:31:23 -05:00
args = append(args, "--window-icon=info", "--icon-name=dialog-information")
2020-01-04 22:21:39 -05:00
case QuestionIcon:
2020-01-26 11:31:23 -05:00
args = append(args, "--window-icon=question", "--icon-name=dialog-question")
2021-03-05 10:14:30 -05:00
case PasswordIcon:
args = append(args, "--icon-name=dialog-password")
case NoIcon:
args = append(args, "--icon-name=")
2020-01-04 22:21:39 -05:00
}
2020-01-28 07:46:43 -05:00
out, err := zenutil.Run(opts.ctx, args)
2021-03-04 07:35:27 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
2021-03-05 21:29:00 -05:00
if opts.extraButton != nil &&
*opts.extraButton == string(bytes.TrimSuffix(out, []byte{'\n'})) {
2021-03-04 21:01:59 -05:00
return false, ErrExtraButton
}
2020-01-04 22:21:39 -05:00
return false, nil
}
if err != nil {
return false, err
}
return true, err
}