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

13
file.go
View file

@ -325,12 +325,15 @@ func isUniformTypeIdentifier(pattern string) bool {
return true return true
} }
func splitDirAndName(path string) (dir, name string) { func splitDirAndName(path string) (dir, name string, err error) {
if path != "" { if path == "" {
return "", "", nil
}
fi, err := os.Stat(path) fi, err := os.Stat(path)
if err == nil && fi.IsDir() { if err == nil && fi.IsDir() {
return path, "" return path, "", nil
} }
} dir, name = filepath.Split(path)
return 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" 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 var data zenutil.File
data.Options.Prompt = opts.title data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden 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 { if opts.attach != nil {
data.Application = opts.attach data.Application = opts.attach
} }
@ -25,13 +28,16 @@ func selectFile(opts options) (string, error) {
return strResult(opts, out, err) return strResult(opts, out, err)
} }
func selectFileMultiple(opts options) ([]string, error) { func selectFileMultiple(opts options) (list []string, err error) {
var data zenutil.File var data zenutil.File
data.Separator = zenutil.Separator
data.Options.Multiple = true
data.Options.Prompt = opts.title data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden data.Options.Invisibles = opts.showHidden
data.Options.Location, _ = splitDirAndName(opts.filename) data.Options.Location, _, err = splitDirAndName(opts.filename)
data.Options.Multiple = true if err != nil {
data.Separator = zenutil.Separator return nil, err
}
if opts.attach != nil { if opts.attach != nil {
data.Application = opts.attach data.Application = opts.attach
} }
@ -50,11 +56,14 @@ func selectFileMultiple(opts options) ([]string, error) {
return lstResult(opts, out, err) return lstResult(opts, out, err)
} }
func selectFileSave(opts options) (string, error) { func selectFileSave(opts options) (name string, err error) {
var data zenutil.File var data zenutil.File
data.Options.Prompt = opts.title data.Options.Prompt = opts.title
data.Options.Invisibles = opts.showHidden 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 { if opts.attach != nil {
data.Application = opts.attach data.Application = opts.attach
} }

View file

@ -28,7 +28,7 @@ func Test_splitDirAndName(t *testing.T) {
} }
for i, tt := range tests { for i, tt := range tests {
gotDir, gotName := splitDirAndName(tt.path) gotDir, gotName, _ := splitDirAndName(tt.path)
if gotDir != tt.wantDir { if gotDir != tt.wantDir {
t.Errorf("splitDirAndName[%d].dir = %q; want %q", i, 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) { func initDirNameExt(filename string, name []uint16) (dir *uint16, ext *uint16) {
d, n := splitDirAndName(filename) d, n, _ := splitDirAndName(filename)
e := filepath.Ext(n) e := filepath.Ext(n)
if n != "" { if n != "" {
copy(name, syscall.StringToUTF16(n)) copy(name, syscall.StringToUTF16(n))