Trim newlines.

This commit is contained in:
Nuno Cruces 2021-03-06 02:29:00 +00:00
parent 967da07465
commit 4ed7d6d483
9 changed files with 42 additions and 45 deletions

View file

@ -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'}))
}
}
}

View file

@ -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 {

View file

@ -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) {

View file

@ -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
}

View file

@ -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 {

View file

@ -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 {

View file

@ -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
}

View file

@ -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

View file

@ -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