From 1824aab27c1f90953d5ff9cde1f7c4422e7a7449 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sun, 5 Jan 2020 12:37:45 +0000 Subject: [PATCH] macos refactoring. --- file_darwin.go | 18 +++----- internal/osa/osa_darwin.go | 43 +++++++++++++++++++ .../osa/scripts}/file.gots | 0 .../osa/scripts}/generate.go | 4 +- .../osa/scripts}/msg.gots | 0 msg_darwin.go | 18 ++------ osa_darwin.go | 22 ---------- 7 files changed, 55 insertions(+), 50 deletions(-) create mode 100644 internal/osa/osa_darwin.go rename {osa_scripts => internal/osa/scripts}/file.gots (100%) rename {osa_scripts => internal/osa/scripts}/generate.go (93%) rename {osa_scripts => internal/osa/scripts}/msg.gots (100%) delete mode 100644 osa_darwin.go diff --git a/file_darwin.go b/file_darwin.go index e46ad9e..a8e89a8 100644 --- a/file_darwin.go +++ b/file_darwin.go @@ -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 -} diff --git a/internal/osa/osa_darwin.go b/internal/osa/osa_darwin.go new file mode 100644 index 0000000..b497663 --- /dev/null +++ b/internal/osa/osa_darwin.go @@ -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("")] + 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 +} diff --git a/osa_scripts/file.gots b/internal/osa/scripts/file.gots similarity index 100% rename from osa_scripts/file.gots rename to internal/osa/scripts/file.gots diff --git a/osa_scripts/generate.go b/internal/osa/scripts/generate.go similarity index 93% rename from osa_scripts/generate.go rename to internal/osa/scripts/generate.go index 30c3049..9c6fe9f 100644 --- a/osa_scripts/generate.go +++ b/internal/osa/scripts/generate.go @@ -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(` + "`{{.}}`" + `)) `)) diff --git a/osa_scripts/msg.gots b/internal/osa/scripts/msg.gots similarity index 100% rename from osa_scripts/msg.gots rename to internal/osa/scripts/msg.gots diff --git a/msg_darwin.go b/msg_darwin.go index cfb945a..251ca87 100644 --- a/msg_darwin.go +++ b/msg_darwin.go @@ -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 -} diff --git a/osa_darwin.go b/osa_darwin.go deleted file mode 100644 index 4f5c793..0000000 --- a/osa_darwin.go +++ /dev/null @@ -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("")]) - return cmd.Output() -}