zenity/util.go

129 lines
2.9 KiB
Go
Raw Normal View History

2021-04-08 11:43:14 -04:00
package zenity
import (
2021-05-22 06:29:23 -04:00
"bytes"
2022-12-20 07:23:15 -05:00
"encoding/xml"
2022-07-27 19:11:12 -04:00
"fmt"
2021-04-08 11:43:14 -04:00
"os/exec"
"strconv"
"strings"
2024-07-10 00:06:01 -04:00
"git.bigun.dev/evan/zenity/internal/zenutil"
2021-04-08 11:43:14 -04:00
)
2022-12-07 09:35:37 -05:00
func quoteAccelerators(text string) string {
return strings.ReplaceAll(text, "&", "&&")
}
2022-12-20 07:23:15 -05:00
func quoteMnemonics(text string) string {
return strings.ReplaceAll(text, "_", "__")
}
func quoteMarkup(text string) string {
var res strings.Builder
err := xml.EscapeText(&res, []byte(text))
if err != nil {
return text
}
return res.String()
}
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")
}
2022-12-19 14:49:05 -05:00
if opts.display != "" {
args = append(args, "--display", opts.display)
}
if opts.class != "" {
args = append(args, "--class", opts.class)
}
if opts.name != "" {
args = append(args, "--name", opts.name)
}
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-08-10 17:40:04 -04:00
usr, pwd, _ := strings.Cut(str, sep)
2022-05-12 09:27:24 -04:00
return usr, pwd, err
2022-05-11 12:49:15 -04:00
}
return "", str, err
}