WIP: zenity command (linux).

This commit is contained in:
Nuno Cruces 2020-01-09 11:17:39 +00:00
parent 51595d7ecc
commit 3fe55c184b
7 changed files with 64 additions and 17 deletions

View file

@ -5,6 +5,9 @@ package zenity
import (
"os/exec"
"strings"
"github.com/ncruces/zenity/internal/cmd"
"github.com/ncruces/zenity/internal/zen"
)
func SelectFile(options ...Option) (string, error) {
@ -18,8 +21,8 @@ func SelectFile(options ...Option) (string, error) {
args = append(args, "--filename", opts.filename)
}
args = append(args, zenityFilters(opts.filters)...)
cmd := exec.Command("zenity", args...)
out, err := cmd.Output()
out, err := zen.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
return "", nil
}
@ -35,7 +38,7 @@ func SelectFile(options ...Option) (string, error) {
func SelectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options)
args := []string{"--file-selection", "--multiple", "--separator=\x1e"}
args := []string{"--file-selection", "--multiple", "--separator", cmd.Separator}
if opts.title != "" {
args = append(args, "--title", opts.title)
}
@ -43,8 +46,8 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
args = append(args, "--filename", opts.filename)
}
args = append(args, zenityFilters(opts.filters)...)
cmd := exec.Command("zenity", args...)
out, err := cmd.Output()
out, err := zen.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
return nil, nil
}
@ -54,7 +57,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
if len(out) > 0 {
out = out[:len(out)-1]
}
return strings.Split(string(out), "\x1e"), nil
return strings.Split(string(out), cmd.Separator), nil
}
func SelectFileSave(options ...Option) (string, error) {
@ -71,8 +74,8 @@ func SelectFileSave(options ...Option) (string, error) {
args = append(args, "--confirm-overwrite")
}
args = append(args, zenityFilters(opts.filters)...)
cmd := exec.Command("zenity", args...)
out, err := cmd.Output()
out, err := zen.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
return "", nil
}
@ -95,8 +98,8 @@ func SelectDirectory(options ...Option) (string, error) {
if opts.filename != "" {
args = append(args, "--filename", opts.filename)
}
cmd := exec.Command("zenity", args...)
out, err := cmd.Output()
out, err := zen.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
return "", nil
}

View file

@ -1,4 +1,6 @@
package cmd
const LineBreak = "\n"
var Command bool
var Separator = "\x00"

View file

@ -1,5 +1,8 @@
// +build !windows
// +build !windows,!darwin
package cmd
const LineBreak = "\n"
var Command bool
var Separator = "\x1e"

View file

@ -1,3 +1,6 @@
package cmd
const LineBreak = "\r\n"
var Command bool
var Separator string

View file

@ -21,10 +21,10 @@ func Run(script string, data interface{}) ([]byte, error) {
res = res[len("<script>") : len(res)-len("\n</script>")]
if cmd.Command {
cmd, err := exec.LookPath("osascript")
path, err := exec.LookPath("osascript")
if err == nil {
os.Stderr.Close()
syscall.Exec(cmd, []string{"osascript", "-l", "JavaScript", "-e", res}, nil)
syscall.Exec(path, []string{"osascript", "-l", "JavaScript", "-e", res}, nil)
}
}

30
internal/zen/zen_unix.go Normal file
View file

@ -0,0 +1,30 @@
// +build !windows,!darwin
package zen
import (
"os"
"os/exec"
"syscall"
"github.com/ncruces/zenity/internal/cmd"
)
var tool, path string
func init() {
for _, tool = range [3]string{"matedialog", "qarma", "zenity"} {
path, _ = exec.LookPath(tool)
if path != "" {
return
}
}
tool = "zenity"
}
func Run(args []string) ([]byte, error) {
if cmd.Command && path != "" {
syscall.Exec(path, append([]string{tool}, args...), os.Environ())
}
return exec.Command(tool, args...).Output()
}

View file

@ -2,7 +2,11 @@
package zenity
import "os/exec"
import (
"os/exec"
"github.com/ncruces/zenity/internal/zen"
)
func Error(text string, options ...Option) (bool, error) {
return message("--error", text, options)
@ -23,7 +27,10 @@ func Warning(text string, options ...Option) (bool, error) {
func message(arg, text string, options []Option) (bool, error) {
opts := optsParse(options)
args := []string{arg, "--text", text, "--no-markup"}
args := []string{arg}
if text != "" {
args = append(args, "--text", text, "--no-markup")
}
if opts.title != "" {
args = append(args, "--title", opts.title)
}
@ -56,8 +63,7 @@ func message(arg, text string, options []Option) (bool, error) {
args = append(args, "--icon-name=dialog-warning")
}
cmd := exec.Command("zenity", args...)
out, err := cmd.Output()
out, err := zen.Run(args)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
if len(out) > 0 && string(out[:len(out)-1]) == opts.extra {
return false, ErrExtraButton