From 4ed7d6d48342af322cd01497500b066eead6870a Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sat, 6 Mar 2021 02:29:00 +0000 Subject: [PATCH] Trim newlines. --- cmd/zenity/main.go | 9 +++++---- color_unix.go | 2 +- entry_darwin.go | 8 +++++--- entry_unix.go | 17 ++++++++++------- file_darwin.go | 15 ++++----------- file_unix.go | 22 +++++++++------------- internal/zenutil/color.go | 4 ++-- msg_darwin.go | 5 +++-- msg_unix.go | 5 +++-- 9 files changed, 42 insertions(+), 45 deletions(-) diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index 2faabb0..b2ecb62 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "flag" "image/color" @@ -443,8 +444,8 @@ func ingestPath(path string) string { if args != nil { args = append(args, path) out, err := exec.Command(args[0], args[1:]...).Output() - if len(out) > 0 && err == nil { - path = string(out[:len(out)-1]) + if err == nil { + path = string(bytes.TrimSuffix(out, []byte{'\n'})) } } } @@ -464,8 +465,8 @@ func egestPath(path string, err error) (string, error) { var out []byte args = append(args, filepath.ToSlash(path)) out, err = exec.Command(args[0], args[1:]...).Output() - if len(out) > 0 && err == nil { - path = string(out[:len(out)-1]) + if err == nil { + path = string(bytes.TrimSuffix(out, []byte{'\n'})) } } } diff --git a/color_unix.go b/color_unix.go index 1df04e4..83614d9 100644 --- a/color_unix.go +++ b/color_unix.go @@ -23,7 +23,7 @@ func selectColor(opts options) (color.Color, error) { } out, err := zenutil.Run(opts.ctx, args) - if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { + if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { return nil, nil } if err != nil { diff --git a/entry_darwin.go b/entry_darwin.go index d296f56..c1786e3 100644 --- a/entry_darwin.go +++ b/entry_darwin.go @@ -1,6 +1,7 @@ package zenity import ( + "bytes" "os/exec" "github.com/ncruces/zenity/internal/zenutil" @@ -44,9 +45,10 @@ func entry(text string, opts options) (string, bool, error) { } out, err := zenutil.Run(opts.ctx, "dialog", data) + out = bytes.TrimSuffix(out, []byte{'\n'}) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { - if len(out) > 0 && opts.extraButton != nil && - string(out[:len(out)-1]) == *opts.extraButton { + if opts.extraButton != nil && + *opts.extraButton == string(out) { return "", false, ErrExtraButton } return "", false, nil @@ -54,7 +56,7 @@ func entry(text string, opts options) (string, bool, error) { if err != nil { return "", false, err } - return string(out[:len(out)-1]), true, nil + return string(out), true, nil } func password(opts options) (string, string, bool, error) { diff --git a/entry_unix.go b/entry_unix.go index 35c024d..d6361ed 100644 --- a/entry_unix.go +++ b/entry_unix.go @@ -3,6 +3,7 @@ package zenity import ( + "bytes" "os/exec" "strconv" "strings" @@ -45,9 +46,10 @@ func entry(text string, opts options) (string, bool, error) { } out, err := zenutil.Run(opts.ctx, args) + out = bytes.TrimSuffix(out, []byte{'\n'}) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { - if len(out) > 0 && opts.extraButton != nil && - string(out[:len(out)-1]) == *opts.extraButton { + if opts.extraButton != nil && + *opts.extraButton == string(out) { return "", false, ErrExtraButton } return "", false, nil @@ -55,7 +57,7 @@ func entry(text string, opts options) (string, bool, error) { if err != nil { return "", false, err } - return string(out[:len(out)-1]), true, nil + return string(out), true, nil } func password(opts options) (string, string, bool, error) { @@ -77,9 +79,10 @@ func password(opts options) (string, string, bool, error) { } out, err := zenutil.Run(opts.ctx, args) + out = bytes.TrimSuffix(out, []byte{'\n'}) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { - if len(out) > 0 && opts.extraButton != nil && - string(out[:len(out)-1]) == *opts.extraButton { + if opts.extraButton != nil && + *opts.extraButton == string(out) { return "", "", false, ErrExtraButton } return "", "", false, nil @@ -88,9 +91,9 @@ func password(opts options) (string, string, bool, error) { return "", "", false, err } if opts.username { - if split := strings.SplitN(string(out[:len(out)-1]), "|", 2); len(split) == 2 { + if split := strings.SplitN(string(out), "|", 2); len(split) == 2 { return split[0], split[1], true, nil } } - return "", string(out[:len(out)-1]), true, nil + return "", string(out), true, nil } diff --git a/file_darwin.go b/file_darwin.go index 7e1b92c..275d234 100644 --- a/file_darwin.go +++ b/file_darwin.go @@ -1,6 +1,7 @@ package zenity import ( + "bytes" "os/exec" "strings" @@ -27,10 +28,7 @@ func selectFile(opts options) (string, error) { if err != nil { return "", err } - if len(out) > 0 { - out = out[:len(out)-1] - } - return string(out), nil + return string(bytes.TrimSuffix(out, []byte{'\n'})), nil } func selectFileMutiple(opts options) ([]string, error) { @@ -55,9 +53,7 @@ func selectFileMutiple(opts options) ([]string, error) { if err != nil { return nil, err } - if len(out) > 0 { - out = out[:len(out)-1] - } + out = bytes.TrimSuffix(out, []byte{'\n'}) if len(out) == 0 { return nil, nil } @@ -83,10 +79,7 @@ func selectFileSave(opts options) (string, error) { if err != nil { return "", err } - if len(out) > 0 { - out = out[:len(out)-1] - } - return string(out), nil + return string(bytes.TrimSuffix(out, []byte{'\n'})), nil } func initFilters(filters []FileFilter) []string { diff --git a/file_unix.go b/file_unix.go index 0f81127..a9be018 100644 --- a/file_unix.go +++ b/file_unix.go @@ -3,6 +3,7 @@ package zenity import ( + "bytes" "os/exec" "strings" @@ -23,16 +24,13 @@ func selectFile(opts options) (string, error) { args = append(args, initFilters(opts.fileFilters)...) out, err := zenutil.Run(opts.ctx, args) - if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { + if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { return "", nil } if err != nil { return "", err } - if len(out) > 0 { - out = out[:len(out)-1] - } - return string(out), nil + return string(bytes.TrimSuffix(out, []byte{'\n'})), nil } func selectFileMutiple(opts options) ([]string, error) { @@ -49,14 +47,15 @@ func selectFileMutiple(opts options) ([]string, error) { args = append(args, initFilters(opts.fileFilters)...) out, err := zenutil.Run(opts.ctx, args) - if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { + if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { return nil, nil } if err != nil { return nil, err } - if len(out) > 0 { - out = out[:len(out)-1] + out = bytes.TrimSuffix(out, []byte{'\n'}) + if len(out) == 0 { + return nil, nil } return strings.Split(string(out), zenutil.Separator), nil } @@ -78,16 +77,13 @@ func selectFileSave(opts options) (string, error) { args = append(args, initFilters(opts.fileFilters)...) out, err := zenutil.Run(opts.ctx, args) - if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { + if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { return "", nil } if err != nil { return "", err } - if len(out) > 0 { - out = out[:len(out)-1] - } - return string(out), nil + return string(bytes.TrimSuffix(out, []byte{'\n'})), nil } func initFilters(filters []FileFilter) []string { diff --git a/internal/zenutil/color.go b/internal/zenutil/color.go index 366de9f..f2a0b2e 100644 --- a/internal/zenutil/color.go +++ b/internal/zenutil/color.go @@ -29,12 +29,12 @@ func ParseColor(s string) color.Color { if len(s) >= 10 { c := color.NRGBA{A: 0xff} - if _, err := fmt.Sscanf(s, "rgb(%d,%d,%d)\n", &c.R, &c.G, &c.B); err == nil { + if _, err := fmt.Sscanf(s, "rgb(%d,%d,%d)", &c.R, &c.G, &c.B); err == nil { return c } var a float32 - if _, err := fmt.Sscanf(s, "rgba(%d,%d,%d,%f)\n", &c.R, &c.G, &c.B, &a); err == nil { + if _, err := fmt.Sscanf(s, "rgba(%d,%d,%d,%f)", &c.R, &c.G, &c.B, &a); err == nil { c.A = uint8(255*a + 0.5) return c } diff --git a/msg_darwin.go b/msg_darwin.go index 47da5e6..3c40d0a 100644 --- a/msg_darwin.go +++ b/msg_darwin.go @@ -1,6 +1,7 @@ package zenity import ( + "bytes" "os/exec" "github.com/ncruces/zenity/internal/zenutil" @@ -101,8 +102,8 @@ func message(kind messageKind, text string, opts options) (bool, error) { out, err := zenutil.Run(opts.ctx, "dialog", data) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { - if len(out) > 0 && opts.extraButton != nil && - string(out[:len(out)-1]) == *opts.extraButton { + if opts.extraButton != nil && + *opts.extraButton == string(bytes.TrimSuffix(out, []byte{'\n'})) { return false, ErrExtraButton } return false, nil diff --git a/msg_unix.go b/msg_unix.go index 2ded76c..85249d0 100644 --- a/msg_unix.go +++ b/msg_unix.go @@ -3,6 +3,7 @@ package zenity import ( + "bytes" "os/exec" "strconv" @@ -65,8 +66,8 @@ func message(kind messageKind, text string, opts options) (bool, error) { out, err := zenutil.Run(opts.ctx, args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { - if len(out) > 0 && opts.extraButton != nil && - string(out[:len(out)-1]) == *opts.extraButton { + if opts.extraButton != nil && + *opts.extraButton == string(bytes.TrimSuffix(out, []byte{'\n'})) { return false, ErrExtraButton } return false, nil