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
|
|
|
|
2020-01-19 06:57:05 -05:00
|
|
|
"github.com/ncruces/zenity/internal/zenutil"
|
2019-12-11 06:29:32 -05:00
|
|
|
)
|
|
|
|
|
2020-01-26 11:04:49 -05:00
|
|
|
func selectFile(options []Option) (string, error) {
|
2020-01-24 07:52:45 -05:00
|
|
|
opts := applyOptions(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-24 07:52:45 -05:00
|
|
|
args = append(args, initFilters(opts.fileFilters)...)
|
2020-01-09 06:17:39 -05:00
|
|
|
|
2020-01-19 06:57:05 -05:00
|
|
|
out, err := zenutil.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-26 11:04:49 -05:00
|
|
|
func selectFileMutiple(options []Option) ([]string, error) {
|
2020-01-24 07:52:45 -05:00
|
|
|
opts := applyOptions(options)
|
2020-01-02 20:44:23 -05:00
|
|
|
|
2020-01-19 06:57:05 -05:00
|
|
|
args := []string{"--file-selection", "--multiple", "--separator", zenutil.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-24 07:52:45 -05:00
|
|
|
args = append(args, initFilters(opts.fileFilters)...)
|
2020-01-09 06:17:39 -05:00
|
|
|
|
2020-01-19 06:57:05 -05:00
|
|
|
out, err := zenutil.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-19 06:57:05 -05:00
|
|
|
return strings.Split(string(out), zenutil.Separator), nil
|
2019-12-11 06:29:32 -05:00
|
|
|
}
|
|
|
|
|
2020-01-26 11:04:49 -05:00
|
|
|
func selectFileSave(options []Option) (string, error) {
|
2020-01-24 07:52:45 -05:00
|
|
|
opts := applyOptions(options)
|
2020-01-02 20:44:23 -05:00
|
|
|
|
2019-12-11 06:29:32 -05:00
|
|
|
args := []string{"--file-selection", "--save"}
|
2020-01-12 18:09:11 -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-24 07:52:45 -05:00
|
|
|
if opts.confirmOverwrite {
|
2019-12-11 06:29:32 -05:00
|
|
|
args = append(args, "--confirm-overwrite")
|
|
|
|
}
|
2020-01-24 07:52:45 -05:00
|
|
|
args = append(args, initFilters(opts.fileFilters)...)
|
2020-01-09 06:17:39 -05:00
|
|
|
|
2020-01-19 06:57:05 -05:00
|
|
|
out, err := zenutil.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-12 18:09:11 -05:00
|
|
|
func initFilters(filters []FileFilter) []string {
|
2019-12-11 06:29:32 -05:00
|
|
|
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
|
|
|
|
}
|