Errors, documentation.

This commit is contained in:
Nuno Cruces 2022-07-28 00:11:12 +01:00
parent 7ac67006b5
commit 60a828af0a
4 changed files with 17 additions and 6 deletions

View file

@ -5,6 +5,7 @@ package zenutil
import ( import (
"bytes" "bytes"
"context" "context"
"fmt"
"io" "io"
"os" "os"
"os/exec" "os/exec"
@ -77,11 +78,14 @@ func (d *progressDialog) wait(extra *string, out *bytes.Buffer) {
case eerr.ExitCode() == -1 && atomic.LoadInt32(&d.closed) != 0: case eerr.ExitCode() == -1 && atomic.LoadInt32(&d.closed) != 0:
err = nil err = nil
case eerr.ExitCode() == 1: case eerr.ExitCode() == 1:
if extra != nil && *extra+"\n" == string(out.Bytes()) { out := bytes.TrimSuffix(out.Bytes(), []byte{'\n'})
if extra != nil && *extra == string(out) {
err = ErrExtraButton err = ErrExtraButton
} else { } else {
err = ErrCanceled err = ErrCanceled
} }
default:
err = fmt.Errorf("%w: %s", eerr, eerr.Stderr)
} }
} }
d.err = err d.err = err

View file

@ -13,6 +13,9 @@ func list(text string, items []string, opts options) (string, error) {
if opts.extraButton != nil { if opts.extraButton != nil {
return "", fmt.Errorf("%w: extra button", ErrUnsupported) return "", fmt.Errorf("%w: extra button", ErrUnsupported)
} }
if len(opts.defaultItems) > 1 {
return "", fmt.Errorf("%w: multiple default items", ErrUnsupported)
}
var data zenutil.List var data zenutil.List
data.Items = items data.Items = items

2
pwd.go
View file

@ -10,7 +10,7 @@ func Password(options ...Option) (usr string, pwd string, err error) {
return password(applyOptions(options)) return password(applyOptions(options))
} }
// Username returns an Option to display the username (Unix only). // Username returns an Option to display the username.
func Username() Option { func Username() Option {
return funcOption(func(o *options) { o.username = true }) return funcOption(func(o *options) { o.username = true })
} }

View file

@ -4,6 +4,7 @@ package zenity
import ( import (
"bytes" "bytes"
"fmt"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
@ -66,11 +67,14 @@ func appendWindowIcon(args []string, opts options) []string {
func strResult(opts options, out []byte, err error) (string, error) { func strResult(opts options, out []byte, err error) (string, error) {
out = bytes.TrimSuffix(out, []byte{'\n'}) out = bytes.TrimSuffix(out, []byte{'\n'})
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if eerr, ok := err.(*exec.ExitError); ok {
if opts.extraButton != nil && *opts.extraButton == string(out) { if eerr.ExitCode() == 1 {
return "", ErrExtraButton if opts.extraButton != nil && *opts.extraButton == string(out) {
return "", ErrExtraButton
}
return "", ErrCanceled
} }
return "", ErrCanceled return "", fmt.Errorf("%w: %s", eerr, eerr.Stderr)
} }
if err != nil { if err != nil {
return "", err return "", err