zenity/util_unix.go

82 lines
1.8 KiB
Go
Raw Permalink Normal View History

2021-05-05 19:38:49 -04:00
// +build !windows
2021-04-08 11:43:14 -04:00
package zenity
import (
2021-05-22 06:29:23 -04:00
"bytes"
2021-04-08 11:43:14 -04:00
"os/exec"
"strconv"
"strings"
"github.com/ncruces/zenity/internal/zenutil"
)
func appendTitle(args []string, opts options) []string {
if opts.title != nil {
args = append(args, "--title", *opts.title)
}
return args
}
func appendButtons(args []string, opts options) []string {
if opts.okLabel != nil {
args = append(args, "--ok-label", *opts.okLabel)
}
if opts.cancelLabel != nil {
args = append(args, "--cancel-label", *opts.cancelLabel)
}
if opts.extraButton != nil {
args = append(args, "--extra-button", *opts.extraButton)
}
return args
}
func appendWidthHeight(args []string, opts options) []string {
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))
}
return args
}
func appendIcon(args []string, opts options) []string {
switch opts.icon {
case ErrorIcon:
args = append(args, "--window-icon=error")
case WarningIcon:
args = append(args, "--window-icon=warning")
case InfoIcon:
args = append(args, "--window-icon=info")
case QuestionIcon:
args = append(args, "--window-icon=question")
}
return args
}
2021-04-29 11:05:28 -04:00
func strResult(opts options, out []byte, err error) (string, error) {
2021-05-22 06:29:23 -04:00
out = bytes.TrimSuffix(out, []byte{'\n'})
2021-04-08 11:43:14 -04:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
2021-05-22 06:29:23 -04:00
if opts.extraButton != nil && *opts.extraButton == string(out) {
2021-04-29 11:05:28 -04:00
return "", ErrExtraButton
2021-04-08 11:43:14 -04:00
}
2021-04-29 11:05:28 -04:00
return "", ErrCanceled
2021-04-08 11:43:14 -04:00
}
if err != nil {
2021-04-29 11:05:28 -04:00
return "", err
2021-04-08 11:43:14 -04:00
}
2021-04-29 11:05:28 -04:00
return string(out), nil
2021-04-08 11:43:14 -04:00
}
func lstResult(opts options, out []byte, err error) ([]string, error) {
2021-04-29 11:05:28 -04:00
str, err := strResult(opts, out, err)
2021-07-08 13:56:28 -04:00
if err != nil {
return nil, err
}
if len(out) == 0 {
return []string{}, nil
2021-04-08 11:43:14 -04:00
}
2021-07-08 13:56:28 -04:00
return strings.Split(str, zenutil.Separator), nil
2021-04-08 11:43:14 -04:00
}