macos refactoring.

This commit is contained in:
Nuno Cruces 2020-01-05 12:37:45 +00:00
parent 9b63531d6a
commit 1824aab27c
7 changed files with 55 additions and 50 deletions

View file

@ -2,11 +2,13 @@ package zenity
import (
"strings"
"github.com/ncruces/zenity/internal/osa"
)
func SelectFile(options ...Option) (string, error) {
opts := optsParse(options)
out, err := osaRun("file", osaFile{
out, err := osa.Run("file", osa.File{
Operation: "chooseFile",
Prompt: opts.title,
Location: opts.filename,
@ -23,7 +25,7 @@ func SelectFile(options ...Option) (string, error) {
func SelectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options)
out, err := osaRun("file", osaFile{
out, err := osa.Run("file", osa.File{
Operation: "chooseFile",
Multiple: true,
Prompt: opts.title,
@ -44,7 +46,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
func SelectFileSave(options ...Option) (string, error) {
opts := optsParse(options)
out, err := osaRun("file", osaFile{
out, err := osa.Run("file", osa.File{
Operation: "chooseFileName",
Prompt: opts.title,
Location: opts.filename,
@ -60,7 +62,7 @@ func SelectFileSave(options ...Option) (string, error) {
func SelectDirectory(options ...Option) (string, error) {
opts := optsParse(options)
out, err := osaRun("file", osaFile{
out, err := osa.Run("file", osa.File{
Operation: "chooseFolder",
Prompt: opts.title,
Location: opts.filename,
@ -83,11 +85,3 @@ func appleFilters(filters []FileFilter) []string {
}
return filter
}
type osaFile struct {
Operation string
Prompt string
Location string
Type []string
Multiple bool
}

View file

@ -0,0 +1,43 @@
package osa
import (
"os/exec"
"strings"
)
//go:generate go run scripts/generate.go scripts/
func Run(script string, data interface{}) ([]byte, error) {
var buf strings.Builder
err := scripts.ExecuteTemplate(&buf, script, data)
if err != nil {
return nil, err
}
res := buf.String()
res = res[len("<script>") : len(res)-len("</script>")]
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = strings.NewReader(res)
return cmd.Output()
}
type File struct {
Operation string
Prompt string
Location string
Type []string
Multiple bool
}
type Msg struct {
Dialog bool
Text string
Message string
As string
Title string
Icon string
Buttons []string
Cancel int
Default int
}

View file

@ -68,9 +68,9 @@ func main() {
var generator = template.Must(template.New("").Parse(`// Code generated by zenity; DO NOT EDIT.
package zenity
package osa
import "html/template"
var osaScripts = template.Must(template.New("").Parse(` + "`{{.}}`" + `))
var scripts = template.Must(template.New("").Parse(` + "`{{.}}`" + `))
`))

View file

@ -2,6 +2,8 @@ package zenity
import (
"os/exec"
"github.com/ncruces/zenity/internal/osa"
)
func Error(text string, options ...Option) (bool, error) {
@ -23,7 +25,7 @@ func Warning(text string, options ...Option) (bool, error) {
func message(dialog int, text string, options []Option) (bool, error) {
opts := optsParse(options)
data := osaMsg{
data := osa.Msg{
Text: text,
Title: opts.title,
Dialog: opts.icon != 0 || dialog == 2,
@ -96,7 +98,7 @@ func message(dialog int, text string, options []Option) (bool, error) {
}
}
_, err := osaRun("msg", data)
_, err := osa.Run("msg", data)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return false, nil
}
@ -105,15 +107,3 @@ func message(dialog int, text string, options []Option) (bool, error) {
}
return true, err
}
type osaMsg struct {
Dialog bool
Text string
Message string
As string
Title string
Icon string
Buttons []string
Cancel int
Default int
}

View file

@ -1,22 +0,0 @@
package zenity
import (
"os/exec"
"strings"
)
//go:generate go run osa_scripts/generate.go osa_scripts/
func osaRun(script string, data interface{}) ([]byte, error) {
var buf strings.Builder
err := osaScripts.ExecuteTemplate(&buf, script, data)
if err != nil {
return nil, err
}
var res = buf.String()
cmd := exec.Command("osascript", "-l", "JavaScript")
cmd.Stdin = strings.NewReader(res[len("<script>") : len(res)-len("</script>")])
return cmd.Output()
}