-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
util.go
128 lines (116 loc) · 2.93 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package zenity
import (
"bytes"
"encoding/xml"
"fmt"
"os/exec"
"strconv"
"strings"
"github.com/ncruces/zenity/internal/zenutil"
)
func quoteAccelerators(text string) string {
return strings.ReplaceAll(text, "&", "&&")
}
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()
}
func appendGeneral(args []string, opts options) []string {
if opts.title != nil {
args = append(args, "--title", *opts.title)
}
if id, ok := opts.attach.(int); ok {
args = append(args, "--attach", strconv.Itoa(id))
}
if opts.modal {
args = append(args, "--modal")
}
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)
}
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 appendWindowIcon(args []string, opts options) []string {
switch opts.windowIcon {
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")
}
if i, ok := opts.windowIcon.(string); ok {
args = append(args, "--window-icon", i)
}
return args
}
func strResult(opts options, out []byte, err error) (string, error) {
out = bytes.TrimSuffix(out, []byte{'\n'})
if eerr, ok := err.(*exec.ExitError); ok {
if eerr.ExitCode() == 1 {
if opts.extraButton != nil && *opts.extraButton == string(out) {
return "", ErrExtraButton
}
return "", ErrCanceled
}
return "", fmt.Errorf("%w: %s", eerr, eerr.Stderr)
}
if err != nil {
return "", err
}
return string(out), nil
}
func lstResult(opts options, out []byte, err error) ([]string, error) {
str, err := strResult(opts, out, err)
if err != nil {
return nil, err
}
if len(out) == 0 {
return []string{}, nil
}
return strings.Split(str, zenutil.Separator), nil
}
func pwdResult(sep string, opts options, out []byte, err error) (string, string, error) {
str, err := strResult(opts, out, err)
if opts.username {
usr, pwd, _ := strings.Cut(str, sep)
return usr, pwd, err
}
return "", str, err
}