zenity/file_unix.go

129 lines
2.8 KiB
Go
Raw Normal View History

2020-01-08 13:12:29 -05:00
// +build !windows,!darwin
2019-12-29 20:00:50 -05:00
package zenity
2019-12-11 06:29:32 -05:00
import (
"os/exec"
"strings"
2020-01-09 06:17:39 -05:00
"github.com/ncruces/zenity/internal/cmd"
"github.com/ncruces/zenity/internal/zen"
2019-12-11 06:29:32 -05:00
)
2020-01-10 07:00:38 -05:00
// Display file selection dialog.
//
// Returns an empty string on cancel.
//
// Valid options: Title, Directory, Filename, FileFilters.
2020-01-04 22:21:39 -05:00
func SelectFile(options ...Option) (string, error) {
opts := optsParse(options)
2020-01-02 20:44:23 -05:00
2019-12-11 06:29:32 -05:00
args := []string{"--file-selection"}
2020-01-09 20:46:53 -05:00
if opts.directory {
args = append(args, "--directory")
}
2020-01-02 20:44:23 -05:00
if opts.title != "" {
2020-01-04 22:21:39 -05:00
args = append(args, "--title", opts.title)
2019-12-11 06:29:32 -05:00
}
2020-01-02 20:44:23 -05:00
if opts.filename != "" {
2020-01-04 22:21:39 -05:00
args = append(args, "--filename", opts.filename)
2019-12-11 06:29:32 -05:00
}
2020-01-02 20:44:23 -05:00
args = append(args, zenityFilters(opts.filters)...)
2020-01-09 06:17:39 -05:00
out, err := zen.Run(args)
2020-01-06 07:35:45 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
2019-12-11 19:42:40 -05:00
return "", nil
}
2019-12-11 06:29:32 -05:00
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
2020-01-10 07:00:38 -05:00
// Display multiple file selection dialog.
//
// Returns a nil slice on cancel.
//
// Valid options: Title, Directory, Filename, FileFilters.
2020-01-04 22:21:39 -05:00
func SelectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options)
2020-01-02 20:44:23 -05:00
2020-01-09 06:17:39 -05:00
args := []string{"--file-selection", "--multiple", "--separator", cmd.Separator}
2020-01-09 20:46:53 -05:00
if opts.directory {
args = append(args, "--directory")
}
2020-01-02 20:44:23 -05:00
if opts.title != "" {
2020-01-04 22:21:39 -05:00
args = append(args, "--title", opts.title)
2019-12-11 06:29:32 -05:00
}
2020-01-02 20:44:23 -05:00
if opts.filename != "" {
2020-01-04 22:21:39 -05:00
args = append(args, "--filename", opts.filename)
2019-12-11 06:29:32 -05:00
}
2020-01-02 20:44:23 -05:00
args = append(args, zenityFilters(opts.filters)...)
2020-01-09 06:17:39 -05:00
out, err := zen.Run(args)
2020-01-06 07:35:45 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
2019-12-11 19:42:40 -05:00
return nil, nil
}
2019-12-11 06:29:32 -05:00
if err != nil {
return nil, err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
2020-01-09 06:17:39 -05:00
return strings.Split(string(out), cmd.Separator), nil
2019-12-11 06:29:32 -05:00
}
2020-01-10 07:00:38 -05:00
// Display save file selection dialog.
//
// Returns an empty string on cancel.
//
// Valid options: Title, Filename, ConfirmOverwrite, FileFilters.
2020-01-04 22:21:39 -05:00
func SelectFileSave(options ...Option) (string, error) {
opts := optsParse(options)
2020-01-02 20:44:23 -05:00
2019-12-11 06:29:32 -05:00
args := []string{"--file-selection", "--save"}
2020-01-02 20:44:23 -05:00
if opts.title != "" {
2020-01-04 22:21:39 -05:00
args = append(args, "--title", opts.title)
2019-12-11 06:29:32 -05:00
}
2020-01-02 20:44:23 -05:00
if opts.filename != "" {
2020-01-04 22:21:39 -05:00
args = append(args, "--filename", opts.filename)
2019-12-11 06:29:32 -05:00
}
2020-01-02 20:44:23 -05:00
if opts.overwrite {
2019-12-11 06:29:32 -05:00
args = append(args, "--confirm-overwrite")
}
2020-01-02 20:44:23 -05:00
args = append(args, zenityFilters(opts.filters)...)
2020-01-09 06:17:39 -05:00
out, err := zen.Run(args)
2020-01-06 07:35:45 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
2019-12-11 19:42:40 -05:00
return "", nil
}
2019-12-11 06:29:32 -05:00
if err != nil {
return "", err
}
if len(out) > 0 {
out = out[:len(out)-1]
}
return string(out), nil
}
func zenityFilters(filters []FileFilter) []string {
var res []string
for _, f := range filters {
var buf strings.Builder
buf.WriteString("--file-filter=")
if f.Name != "" {
buf.WriteString(f.Name)
buf.WriteRune('|')
}
2020-01-08 19:54:34 -05:00
for _, p := range f.Patterns {
buf.WriteString(p)
2019-12-11 06:29:32 -05:00
buf.WriteRune(' ')
}
res = append(res, buf.String())
}
return res
}