2022-03-24 10:37:37 -04:00
|
|
|
//go:build !windows
|
2021-04-08 11:43:14 -04:00
|
|
|
|
|
|
|
package zenity
|
|
|
|
|
|
|
|
import (
|
2021-05-22 06:29:23 -04:00
|
|
|
"bytes"
|
2022-07-27 19:11:12 -04:00
|
|
|
"fmt"
|
2021-04-08 11:43:14 -04:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ncruces/zenity/internal/zenutil"
|
|
|
|
)
|
|
|
|
|
2022-06-02 07:24:24 -04:00
|
|
|
func appendGeneral(args []string, opts options) []string {
|
2021-04-08 11:43:14 -04:00
|
|
|
if opts.title != nil {
|
|
|
|
args = append(args, "--title", *opts.title)
|
|
|
|
}
|
2022-06-08 15:33:10 -04:00
|
|
|
if id, ok := opts.attach.(int); ok {
|
|
|
|
args = append(args, "--attach", strconv.Itoa(id))
|
2022-06-02 07:24:24 -04:00
|
|
|
}
|
|
|
|
if opts.modal {
|
|
|
|
args = append(args, "--modal")
|
|
|
|
}
|
2021-04-08 11:43:14 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-18 09:48:09 -04:00
|
|
|
func appendWindowIcon(args []string, opts options) []string {
|
|
|
|
switch opts.windowIcon {
|
2021-04-08 11:43:14 -04:00
|
|
|
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")
|
|
|
|
}
|
2022-05-24 07:02:51 -04:00
|
|
|
if i, ok := opts.windowIcon.(string); ok {
|
|
|
|
args = append(args, "--window-icon", i)
|
|
|
|
}
|
2021-04-08 11:43:14 -04:00
|
|
|
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'})
|
2022-07-27 19:11:12 -04:00
|
|
|
if eerr, ok := err.(*exec.ExitError); ok {
|
|
|
|
if eerr.ExitCode() == 1 {
|
|
|
|
if opts.extraButton != nil && *opts.extraButton == string(out) {
|
|
|
|
return "", ErrExtraButton
|
|
|
|
}
|
|
|
|
return "", ErrCanceled
|
2021-04-08 11:43:14 -04:00
|
|
|
}
|
2022-07-27 19:11:12 -04:00
|
|
|
return "", fmt.Errorf("%w: %s", eerr, eerr.Stderr)
|
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
|
|
|
}
|
2022-05-11 12:49:15 -04:00
|
|
|
|
|
|
|
func pwdResult(sep string, opts options, out []byte, err error) (string, string, error) {
|
|
|
|
str, err := strResult(opts, out, err)
|
|
|
|
if opts.username {
|
2022-05-12 09:27:24 -04:00
|
|
|
usr, pwd, _ := cut(str, sep)
|
|
|
|
return usr, pwd, err
|
2022-05-11 12:49:15 -04:00
|
|
|
}
|
|
|
|
return "", str, err
|
|
|
|
}
|
2022-05-12 09:27:24 -04:00
|
|
|
|
|
|
|
// Replace with strings.Cut after 1.18.
|
|
|
|
func cut(s, sep string) (before, after string, found bool) {
|
|
|
|
if i := strings.Index(s, sep); i >= 0 {
|
|
|
|
return s[:i], s[i+len(sep):], true
|
|
|
|
}
|
|
|
|
return s, "", false
|
|
|
|
}
|