2020-01-04 22:21:39 -05:00
|
|
|
package zenity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
2020-01-05 07:37:45 -05:00
|
|
|
|
2020-01-19 06:57:05 -05:00
|
|
|
"github.com/ncruces/zenity/internal/zenutil"
|
2020-01-04 22:21:39 -05:00
|
|
|
)
|
|
|
|
|
2020-01-23 06:44:28 -05:00
|
|
|
func message(kind messageKind, text string, options []Option) (bool, error) {
|
2020-01-04 22:21:39 -05:00
|
|
|
opts := optsParse(options)
|
2020-01-19 06:57:05 -05:00
|
|
|
data := zenutil.Msg{Text: text}
|
2020-01-23 06:44:28 -05:00
|
|
|
dialog := kind == questionKind || opts.icon != 0
|
2020-01-04 22:21:39 -05:00
|
|
|
|
2020-01-06 07:01:51 -05:00
|
|
|
if dialog {
|
2020-01-09 20:46:53 -05:00
|
|
|
data.Operation = "displayDialog"
|
|
|
|
data.Title = opts.title
|
2020-01-04 22:21:39 -05:00
|
|
|
|
|
|
|
switch opts.icon {
|
|
|
|
case ErrorIcon:
|
|
|
|
data.Icon = "stop"
|
|
|
|
case WarningIcon:
|
|
|
|
data.Icon = "caution"
|
2020-01-12 18:09:11 -05:00
|
|
|
case InfoIcon, QuestionIcon:
|
|
|
|
data.Icon = "note"
|
2020-01-04 22:21:39 -05:00
|
|
|
}
|
|
|
|
} else {
|
2020-01-09 20:46:53 -05:00
|
|
|
data.Operation = "displayAlert"
|
|
|
|
if opts.title != "" {
|
|
|
|
data.Message = text
|
|
|
|
data.Text = opts.title
|
|
|
|
}
|
|
|
|
|
2020-01-23 06:44:28 -05:00
|
|
|
switch kind {
|
|
|
|
case infoKind:
|
2020-01-04 22:21:39 -05:00
|
|
|
data.As = "informational"
|
2020-01-23 06:44:28 -05:00
|
|
|
case warningKind:
|
2020-01-04 22:21:39 -05:00
|
|
|
data.As = "warning"
|
2020-01-23 06:44:28 -05:00
|
|
|
case errorKind:
|
2020-01-12 13:55:10 -05:00
|
|
|
data.As = "critical"
|
2020-01-04 22:21:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 06:44:28 -05:00
|
|
|
if kind != questionKind {
|
2020-01-06 07:01:51 -05:00
|
|
|
if dialog {
|
2020-01-04 22:21:39 -05:00
|
|
|
opts.ok = "OK"
|
|
|
|
}
|
2020-01-06 07:01:51 -05:00
|
|
|
opts.cancel = ""
|
2020-01-04 22:21:39 -05:00
|
|
|
}
|
2020-01-06 07:01:51 -05:00
|
|
|
if opts.ok != "" || opts.cancel != "" || opts.extra != "" {
|
2020-01-04 22:21:39 -05:00
|
|
|
if opts.ok == "" {
|
|
|
|
opts.ok = "OK"
|
|
|
|
}
|
|
|
|
if opts.cancel == "" {
|
|
|
|
opts.cancel = "Cancel"
|
|
|
|
}
|
2020-01-23 06:44:28 -05:00
|
|
|
if kind == questionKind {
|
2020-01-04 22:21:39 -05:00
|
|
|
if opts.extra == "" {
|
|
|
|
data.Buttons = []string{opts.cancel, opts.ok}
|
|
|
|
data.Default = 2
|
|
|
|
data.Cancel = 1
|
|
|
|
} else {
|
|
|
|
data.Buttons = []string{opts.extra, opts.cancel, opts.ok}
|
|
|
|
data.Default = 3
|
|
|
|
data.Cancel = 2
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if opts.extra == "" {
|
|
|
|
data.Buttons = []string{opts.ok}
|
|
|
|
data.Default = 1
|
|
|
|
} else {
|
|
|
|
data.Buttons = []string{opts.extra, opts.ok}
|
|
|
|
data.Default = 2
|
|
|
|
}
|
|
|
|
}
|
2020-01-06 19:57:00 -05:00
|
|
|
data.Extra = opts.extra
|
2020-01-04 22:21:39 -05:00
|
|
|
}
|
|
|
|
if opts.defcancel {
|
|
|
|
if data.Cancel != 0 {
|
|
|
|
data.Default = data.Cancel
|
|
|
|
}
|
2020-01-06 07:01:51 -05:00
|
|
|
if dialog && data.Buttons == nil {
|
2020-01-04 22:21:39 -05:00
|
|
|
data.Default = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:57:05 -05:00
|
|
|
out, err := zenutil.Run("msg", data)
|
2020-01-04 22:21:39 -05:00
|
|
|
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-01-06 07:01:51 -05:00
|
|
|
if len(out) > 0 && string(out[:len(out)-1]) == opts.extra {
|
|
|
|
return false, ErrExtraButton
|
|
|
|
}
|
2020-01-04 22:21:39 -05:00
|
|
|
return true, err
|
|
|
|
}
|