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 package main
import ( import (
"bytes"
"context" "context"
"flag" "flag"
"image/color" "image/color"
@ -443,8 +444,8 @@ func ingestPath(path string) string {
if args != nil { if args != nil {
args = append(args, path) args = append(args, path)
out, err := exec.Command(args[0], args[1:]...).Output() out, err := exec.Command(args[0], args[1:]...).Output()
if len(out) > 0 && err == nil { if err == nil {
path = string(out[:len(out)-1]) path = string(bytes.TrimSuffix(out, []byte{'\n'}))
} }
} }
} }
@ -464,8 +465,8 @@ func egestPath(path string, err error) (string, error) {
var out []byte var out []byte
args = append(args, filepath.ToSlash(path)) args = append(args, filepath.ToSlash(path))
out, err = exec.Command(args[0], args[1:]...).Output() out, err = exec.Command(args[0], args[1:]...).Output()
if len(out) > 0 && err == nil { if err == nil {
path = string(out[:len(out)-1]) 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) 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 return nil, nil
} }
if err != nil { if err != nil {

View File

@ -1,6 +1,7 @@
package zenity package zenity
import ( import (
"bytes"
"os/exec" "os/exec"
"github.com/ncruces/zenity/internal/zenutil" "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, err := zenutil.Run(opts.ctx, "dialog", data)
out = bytes.TrimSuffix(out, []byte{'\n'})
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
if len(out) > 0 && opts.extraButton != nil && if opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton { *opts.extraButton == string(out) {
return "", false, ErrExtraButton return "", false, ErrExtraButton
} }
return "", false, nil return "", false, nil
@ -54,7 +56,7 @@ func entry(text string, opts options) (string, bool, error) {
if err != nil { if err != nil {
return "", false, err return "", false, err
} }
return string(out[:len(out)-1]), true, nil return string(out), true, nil
} }
func password(opts options) (string, string, bool, error) { func password(opts options) (string, string, bool, error) {

View File

@ -3,6 +3,7 @@
package zenity package zenity
import ( import (
"bytes"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
@ -45,9 +46,10 @@ func entry(text string, opts options) (string, bool, error) {
} }
out, err := zenutil.Run(opts.ctx, args) out, err := zenutil.Run(opts.ctx, args)
out = bytes.TrimSuffix(out, []byte{'\n'})
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
if len(out) > 0 && opts.extraButton != nil && if opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton { *opts.extraButton == string(out) {
return "", false, ErrExtraButton return "", false, ErrExtraButton
} }
return "", false, nil return "", false, nil
@ -55,7 +57,7 @@ func entry(text string, opts options) (string, bool, error) {
if err != nil { if err != nil {
return "", false, err return "", false, err
} }
return string(out[:len(out)-1]), true, nil return string(out), true, nil
} }
func password(opts options) (string, string, bool, error) { 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, err := zenutil.Run(opts.ctx, args)
out = bytes.TrimSuffix(out, []byte{'\n'})
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
if len(out) > 0 && opts.extraButton != nil && if opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton { *opts.extraButton == string(out) {
return "", "", false, ErrExtraButton return "", "", false, ErrExtraButton
} }
return "", "", false, nil return "", "", false, nil
@ -88,9 +91,9 @@ func password(opts options) (string, string, bool, error) {
return "", "", false, err return "", "", false, err
} }
if opts.username { 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 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 package zenity
import ( import (
"bytes"
"os/exec" "os/exec"
"strings" "strings"
@ -27,10 +28,7 @@ func selectFile(opts options) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
if len(out) > 0 { return string(bytes.TrimSuffix(out, []byte{'\n'})), nil
out = out[:len(out)-1]
}
return string(out), nil
} }
func selectFileMutiple(opts options) ([]string, error) { func selectFileMutiple(opts options) ([]string, error) {
@ -55,9 +53,7 @@ func selectFileMutiple(opts options) ([]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(out) > 0 { out = bytes.TrimSuffix(out, []byte{'\n'})
out = out[:len(out)-1]
}
if len(out) == 0 { if len(out) == 0 {
return nil, nil return nil, nil
} }
@ -83,10 +79,7 @@ func selectFileSave(opts options) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
if len(out) > 0 { return string(bytes.TrimSuffix(out, []byte{'\n'})), nil
out = out[:len(out)-1]
}
return string(out), nil
} }
func initFilters(filters []FileFilter) []string { func initFilters(filters []FileFilter) []string {

View File

@ -3,6 +3,7 @@
package zenity package zenity
import ( import (
"bytes"
"os/exec" "os/exec"
"strings" "strings"
@ -23,16 +24,13 @@ func selectFile(opts options) (string, error) {
args = append(args, initFilters(opts.fileFilters)...) args = append(args, initFilters(opts.fileFilters)...)
out, err := zenutil.Run(opts.ctx, args) 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 return "", nil
} }
if err != nil { if err != nil {
return "", err return "", err
} }
if len(out) > 0 { return string(bytes.TrimSuffix(out, []byte{'\n'})), nil
out = out[:len(out)-1]
}
return string(out), nil
} }
func selectFileMutiple(opts options) ([]string, error) { func selectFileMutiple(opts options) ([]string, error) {
@ -49,14 +47,15 @@ func selectFileMutiple(opts options) ([]string, error) {
args = append(args, initFilters(opts.fileFilters)...) args = append(args, initFilters(opts.fileFilters)...)
out, err := zenutil.Run(opts.ctx, args) 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 return nil, nil
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(out) > 0 { out = bytes.TrimSuffix(out, []byte{'\n'})
out = out[:len(out)-1] if len(out) == 0 {
return nil, nil
} }
return strings.Split(string(out), zenutil.Separator), 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)...) args = append(args, initFilters(opts.fileFilters)...)
out, err := zenutil.Run(opts.ctx, args) 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 return "", nil
} }
if err != nil { if err != nil {
return "", err return "", err
} }
if len(out) > 0 { return string(bytes.TrimSuffix(out, []byte{'\n'})), nil
out = out[:len(out)-1]
}
return string(out), nil
} }
func initFilters(filters []FileFilter) []string { func initFilters(filters []FileFilter) []string {

View File

@ -29,12 +29,12 @@ func ParseColor(s string) color.Color {
if len(s) >= 10 { if len(s) >= 10 {
c := color.NRGBA{A: 0xff} 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 return c
} }
var a float32 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) c.A = uint8(255*a + 0.5)
return c return c
} }

View File

@ -1,6 +1,7 @@
package zenity package zenity
import ( import (
"bytes"
"os/exec" "os/exec"
"github.com/ncruces/zenity/internal/zenutil" "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) out, err := zenutil.Run(opts.ctx, "dialog", data)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
if len(out) > 0 && opts.extraButton != nil && if opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton { *opts.extraButton == string(bytes.TrimSuffix(out, []byte{'\n'})) {
return false, ErrExtraButton return false, ErrExtraButton
} }
return false, nil return false, nil

View File

@ -3,6 +3,7 @@
package zenity package zenity
import ( import (
"bytes"
"os/exec" "os/exec"
"strconv" "strconv"
@ -65,8 +66,8 @@ func message(kind messageKind, text string, opts options) (bool, error) {
out, err := zenutil.Run(opts.ctx, args) out, err := zenutil.Run(opts.ctx, args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
if len(out) > 0 && opts.extraButton != nil && if opts.extraButton != nil &&
string(out[:len(out)-1]) == *opts.extraButton { *opts.extraButton == string(bytes.TrimSuffix(out, []byte{'\n'})) {
return false, ErrExtraButton return false, ErrExtraButton
} }
return false, nil return false, nil