From 66863de571628561da27b2e03ee5f310142f78d4 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Thu, 15 Dec 2022 11:26:08 +0000 Subject: [PATCH] Fix panic. --- cmd/zenity/main.go | 9 ++++----- file_test.go | 8 ++++---- file_windows.go | 13 +++++++------ zenity_test.go | 4 ++-- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index 994dd99..725cd41 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -724,14 +724,13 @@ func addColumn(s string) error { func addFileFilter(s string) error { var filter zenity.FileFilter - if split := strings.SplitN(s, "|", 2); len(split) > 1 { - filter.Name = strings.TrimSpace(split[0]) - s = split[1] + if head, tail, cut := strings.Cut(s, "|"); cut { + filter.Name = head + s = tail } - filter.Patterns = strings.Split(strings.TrimSpace(s), " ") + filter.Patterns = strings.Split(strings.Trim(s, " "), " ") fileFilters = append(fileFilters, filter) - return nil } diff --git a/file_test.go b/file_test.go index 8b70656..b1c7802 100644 --- a/file_test.go +++ b/file_test.go @@ -20,7 +20,7 @@ func ExampleSelectFile() { zenity.SelectFile( zenity.Filename(defaultPath), zenity.FileFilters{ - {"Go files", []string{"*.go"}, true}, + {"Go files", []string{"*.go"}, false}, {"Web files", []string{"*.html", "*.js", "*.css"}, true}, {"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true}, }) @@ -30,7 +30,7 @@ func ExampleSelectFileMultiple() { zenity.SelectFileMultiple( zenity.Filename(defaultPath), zenity.FileFilters{ - {"Go files", []string{"*.go"}, true}, + {"Go files", []string{"*.go"}, false}, {"Web files", []string{"*.html", "*.js", "*.css"}, true}, {"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true}, }) @@ -41,7 +41,7 @@ func ExampleSelectFileSave() { zenity.ConfirmOverwrite(), zenity.Filename(defaultName), zenity.FileFilters{ - {"Go files", []string{"*.go"}, true}, + {"Go files", []string{"*.go"}, false}, {"Web files", []string{"*.html", "*.js", "*.css"}, true}, {"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true}, }) @@ -232,7 +232,7 @@ func TestSelectFileSave_script(t *testing.T) { str, err := zenity.SelectFileSave( zenity.ConfirmOverwrite(), zenity.Filename("Χρτο.go"), - zenity.FileFilter{"Go files", []string{"*.go"}, true}, + zenity.FileFilter{"Go files", []string{"*.go"}, false}, ) if skip, err := skip(err); skip { t.Skip("skipping:", err) diff --git a/file_windows.go b/file_windows.go index 4fce08d..b9c33c4 100644 --- a/file_windows.go +++ b/file_windows.go @@ -28,7 +28,7 @@ func selectFile(opts options) (string, error) { args.Flags |= win.OFN_FORCESHOWHIDDEN } if opts.fileFilters != nil { - args.Filter = &initFilters(opts.fileFilters)[0] + args.Filter = initFilters(opts.fileFilters) } var res [32768]uint16 @@ -71,7 +71,7 @@ func selectFileMultiple(opts options) ([]string, error) { args.Flags |= win.OFN_FORCESHOWHIDDEN } if opts.fileFilters != nil { - args.Filter = &initFilters(opts.fileFilters)[0] + args.Filter = initFilters(opts.fileFilters) } var res [32768 + 1024*256]uint16 @@ -145,7 +145,7 @@ func selectFileSave(opts options) (string, error) { args.Flags |= win.OFN_FORCESHOWHIDDEN } if opts.fileFilters != nil { - args.Filter = &initFilters(opts.fileFilters)[0] + args.Filter = initFilters(opts.fileFilters) } var res [32768]uint16 @@ -335,12 +335,12 @@ func initDirNameExt(filename string, name []uint16) (dir *uint16, ext *uint16) { return } -func initFilters(filters FileFilters) []uint16 { +func initFilters(filters FileFilters) *uint16 { filters.simplify() filters.name() var res []uint16 for _, f := range filters { - if f.Name == "" || len(f.Patterns) == 0 { + if len(f.Patterns) == 0 { continue } res = append(res, utf16.Encode([]rune(f.Name))...) @@ -353,6 +353,7 @@ func initFilters(filters FileFilters) []uint16 { } if res != nil { res = append(res, 0) + return &res[0] } - return res + return nil } diff --git a/zenity_test.go b/zenity_test.go index de905d1..5c3b20b 100644 --- a/zenity_test.go +++ b/zenity_test.go @@ -56,8 +56,8 @@ func Test_applyOptions(t *testing.T) { {name: "ConfirmCreate", args: ConfirmCreate(), want: options{confirmCreate: true}}, {name: "ShowHidden", args: ShowHidden(), want: options{showHidden: true}}, {name: "Filename", args: Filename("file.go"), want: options{filename: "file.go"}}, - {name: "FileFilter", args: FileFilter{"Go files", []string{"*.go"}, true}, want: options{ - fileFilters: FileFilters{{"Go files", []string{"*.go"}, true}}, + {name: "FileFilter", args: FileFilter{"All files", []string{"*.*"}, true}, want: options{ + fileFilters: FileFilters{{"All files", []string{"*.*"}, true}}, }}, {name: "FileFilters", args: FileFilters{{"Go files", []string{"*.go"}, true}}, want: options{ fileFilters: FileFilters{{"Go files", []string{"*.go"}, true}},