Improve error message.

This commit is contained in:
Nuno Cruces 2023-04-12 11:22:30 +01:00
parent a175ffa19d
commit f0dd8acdeb
4 changed files with 29 additions and 17 deletions

17
file.go
View file

@ -325,12 +325,15 @@ func isUniformTypeIdentifier(pattern string) bool {
return true
}
func splitDirAndName(path string) (dir, name string) {
if path != "" {
fi, err := os.Stat(path)
if err == nil && fi.IsDir() {
return path, ""
}
func splitDirAndName(path string) (dir, name string, err error) {
if path == "" {
return "", "", nil
}
return filepath.Split(path)
fi, err := os.Stat(path)
if err == nil && fi.IsDir() {
return path, "", nil
}
dir, name = filepath.Split(path)
_, err = os.Stat(dir)
return dir, name, err
}

View file

@ -2,11 +2,14 @@ package zenity
import "github.com/ncruces/zenity/internal/zenutil"
func selectFile(opts options) (string, error) {
func selectFile(opts options) (name string, err error) {
var data zenutil.File
data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden
data.Options.Location, _ = splitDirAndName(opts.filename)
data.Options.Location, _, err = splitDirAndName(opts.filename)
if err != nil {
return "", err
}
if opts.attach != nil {
data.Application = opts.attach
}
@ -25,13 +28,16 @@ func selectFile(opts options) (string, error) {
return strResult(opts, out, err)
}
func selectFileMultiple(opts options) ([]string, error) {
func selectFileMultiple(opts options) (list []string, err error) {
var data zenutil.File
data.Separator = zenutil.Separator
data.Options.Multiple = true
data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden
data.Options.Location, _ = splitDirAndName(opts.filename)
data.Options.Multiple = true
data.Separator = zenutil.Separator
data.Options.Location, _, err = splitDirAndName(opts.filename)
if err != nil {
return nil, err
}
if opts.attach != nil {
data.Application = opts.attach
}
@ -50,11 +56,14 @@ func selectFileMultiple(opts options) ([]string, error) {
return lstResult(opts, out, err)
}
func selectFileSave(opts options) (string, error) {
func selectFileSave(opts options) (name string, err error) {
var data zenutil.File
data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden
data.Options.Location, data.Options.Name = splitDirAndName(opts.filename)
data.Options.Location, data.Options.Name, err = splitDirAndName(opts.filename)
if err != nil {
return "", err
}
if opts.attach != nil {
data.Application = opts.attach
}

View file

@ -28,7 +28,7 @@ func Test_splitDirAndName(t *testing.T) {
}
for i, tt := range tests {
gotDir, gotName := splitDirAndName(tt.path)
gotDir, gotName, _ := splitDirAndName(tt.path)
if gotDir != tt.wantDir {
t.Errorf("splitDirAndName[%d].dir = %q; want %q", i, gotDir, tt.wantDir)
}

View file

@ -321,7 +321,7 @@ func browseForFolderCallback(wnd win.HWND, msg uint32, lparam, data uintptr) uin
}
func initDirNameExt(filename string, name []uint16) (dir *uint16, ext *uint16) {
d, n := splitDirAndName(filename)
d, n, _ := splitDirAndName(filename)
e := filepath.Ext(n)
if n != "" {
copy(name, syscall.StringToUTF16(n))