zenity/file_linux.go

128 lines
2.8 KiB
Go
Raw Normal View History

2019-12-29 20:00:50 -05:00
package zenity
2019-12-11 06:29:32 -05:00
import (
"os/exec"
"strings"
)
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-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)...)
2019-12-11 06:29:32 -05:00
cmd := exec.Command("zenity", args...)
out, err := cmd.Output()
2019-12-11 19:42:40 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
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-04 22:21:39 -05:00
func SelectFileMutiple(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", "--multiple", "--separator=\x1e"}
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)...)
2019-12-11 06:29:32 -05:00
cmd := exec.Command("zenity", args...)
out, err := cmd.Output()
2019-12-11 19:42:40 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
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]
}
return strings.Split(string(out), "\x1e"), nil
}
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)...)
2019-12-11 06:29:32 -05:00
cmd := exec.Command("zenity", args...)
out, err := cmd.Output()
2019-12-11 19:42:40 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
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-04 22:21:39 -05:00
func SelectDirectory(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", "--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
}
cmd := exec.Command("zenity", args...)
out, err := cmd.Output()
2019-12-11 19:42:40 -05:00
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
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('|')
}
for _, e := range f.Exts {
buf.WriteRune('*')
buf.WriteString(e)
buf.WriteRune(' ')
}
res = append(res, buf.String())
}
return res
}