Fix panic.

This commit is contained in:
Nuno Cruces 2022-12-15 11:26:08 +00:00
parent 98d9f88c95
commit 66863de571
4 changed files with 17 additions and 17 deletions

View file

@ -724,14 +724,13 @@ func addColumn(s string) error {
func addFileFilter(s string) error { func addFileFilter(s string) error {
var filter zenity.FileFilter var filter zenity.FileFilter
if split := strings.SplitN(s, "|", 2); len(split) > 1 { if head, tail, cut := strings.Cut(s, "|"); cut {
filter.Name = strings.TrimSpace(split[0]) filter.Name = head
s = split[1] s = tail
} }
filter.Patterns = strings.Split(strings.TrimSpace(s), " ") filter.Patterns = strings.Split(strings.Trim(s, " "), " ")
fileFilters = append(fileFilters, filter) fileFilters = append(fileFilters, filter)
return nil return nil
} }

View file

@ -20,7 +20,7 @@ func ExampleSelectFile() {
zenity.SelectFile( zenity.SelectFile(
zenity.Filename(defaultPath), zenity.Filename(defaultPath),
zenity.FileFilters{ zenity.FileFilters{
{"Go files", []string{"*.go"}, true}, {"Go files", []string{"*.go"}, false},
{"Web files", []string{"*.html", "*.js", "*.css"}, true}, {"Web files", []string{"*.html", "*.js", "*.css"}, true},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true}, {"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true},
}) })
@ -30,7 +30,7 @@ func ExampleSelectFileMultiple() {
zenity.SelectFileMultiple( zenity.SelectFileMultiple(
zenity.Filename(defaultPath), zenity.Filename(defaultPath),
zenity.FileFilters{ zenity.FileFilters{
{"Go files", []string{"*.go"}, true}, {"Go files", []string{"*.go"}, false},
{"Web files", []string{"*.html", "*.js", "*.css"}, true}, {"Web files", []string{"*.html", "*.js", "*.css"}, true},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true}, {"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true},
}) })
@ -41,7 +41,7 @@ func ExampleSelectFileSave() {
zenity.ConfirmOverwrite(), zenity.ConfirmOverwrite(),
zenity.Filename(defaultName), zenity.Filename(defaultName),
zenity.FileFilters{ zenity.FileFilters{
{"Go files", []string{"*.go"}, true}, {"Go files", []string{"*.go"}, false},
{"Web files", []string{"*.html", "*.js", "*.css"}, true}, {"Web files", []string{"*.html", "*.js", "*.css"}, true},
{"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true}, {"Image files", []string{"*.png", "*.gif", "*.ico", "*.jpg", "*.webp"}, true},
}) })
@ -232,7 +232,7 @@ func TestSelectFileSave_script(t *testing.T) {
str, err := zenity.SelectFileSave( str, err := zenity.SelectFileSave(
zenity.ConfirmOverwrite(), zenity.ConfirmOverwrite(),
zenity.Filename("Χρτο.go"), zenity.Filename("Χρτο.go"),
zenity.FileFilter{"Go files", []string{"*.go"}, true}, zenity.FileFilter{"Go files", []string{"*.go"}, false},
) )
if skip, err := skip(err); skip { if skip, err := skip(err); skip {
t.Skip("skipping:", err) t.Skip("skipping:", err)

View file

@ -28,7 +28,7 @@ func selectFile(opts options) (string, error) {
args.Flags |= win.OFN_FORCESHOWHIDDEN args.Flags |= win.OFN_FORCESHOWHIDDEN
} }
if opts.fileFilters != nil { if opts.fileFilters != nil {
args.Filter = &initFilters(opts.fileFilters)[0] args.Filter = initFilters(opts.fileFilters)
} }
var res [32768]uint16 var res [32768]uint16
@ -71,7 +71,7 @@ func selectFileMultiple(opts options) ([]string, error) {
args.Flags |= win.OFN_FORCESHOWHIDDEN args.Flags |= win.OFN_FORCESHOWHIDDEN
} }
if opts.fileFilters != nil { if opts.fileFilters != nil {
args.Filter = &initFilters(opts.fileFilters)[0] args.Filter = initFilters(opts.fileFilters)
} }
var res [32768 + 1024*256]uint16 var res [32768 + 1024*256]uint16
@ -145,7 +145,7 @@ func selectFileSave(opts options) (string, error) {
args.Flags |= win.OFN_FORCESHOWHIDDEN args.Flags |= win.OFN_FORCESHOWHIDDEN
} }
if opts.fileFilters != nil { if opts.fileFilters != nil {
args.Filter = &initFilters(opts.fileFilters)[0] args.Filter = initFilters(opts.fileFilters)
} }
var res [32768]uint16 var res [32768]uint16
@ -335,12 +335,12 @@ func initDirNameExt(filename string, name []uint16) (dir *uint16, ext *uint16) {
return return
} }
func initFilters(filters FileFilters) []uint16 { func initFilters(filters FileFilters) *uint16 {
filters.simplify() filters.simplify()
filters.name() filters.name()
var res []uint16 var res []uint16
for _, f := range filters { for _, f := range filters {
if f.Name == "" || len(f.Patterns) == 0 { if len(f.Patterns) == 0 {
continue continue
} }
res = append(res, utf16.Encode([]rune(f.Name))...) res = append(res, utf16.Encode([]rune(f.Name))...)
@ -353,6 +353,7 @@ func initFilters(filters FileFilters) []uint16 {
} }
if res != nil { if res != nil {
res = append(res, 0) res = append(res, 0)
return &res[0]
} }
return res return nil
} }

View file

@ -56,8 +56,8 @@ func Test_applyOptions(t *testing.T) {
{name: "ConfirmCreate", args: ConfirmCreate(), want: options{confirmCreate: true}}, {name: "ConfirmCreate", args: ConfirmCreate(), want: options{confirmCreate: true}},
{name: "ShowHidden", args: ShowHidden(), want: options{showHidden: true}}, {name: "ShowHidden", args: ShowHidden(), want: options{showHidden: true}},
{name: "Filename", args: Filename("file.go"), want: options{filename: "file.go"}}, {name: "Filename", args: Filename("file.go"), want: options{filename: "file.go"}},
{name: "FileFilter", args: FileFilter{"Go files", []string{"*.go"}, true}, want: options{ {name: "FileFilter", args: FileFilter{"All files", []string{"*.*"}, true}, want: options{
fileFilters: FileFilters{{"Go files", []string{"*.go"}, true}}, fileFilters: FileFilters{{"All files", []string{"*.*"}, true}},
}}, }},
{name: "FileFilters", args: FileFilters{{"Go files", []string{"*.go"}, true}}, want: options{ {name: "FileFilters", args: FileFilters{{"Go files", []string{"*.go"}, true}}, want: options{
fileFilters: FileFilters{{"Go files", []string{"*.go"}, true}}, fileFilters: FileFilters{{"Go files", []string{"*.go"}, true}},