From 9668c61d8befdc1307df3f95a45496f641321025 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sun, 19 Jan 2020 11:57:05 +0000 Subject: [PATCH] Refactoring. --- README.md | 5 +++-- cmd/zenity/main.go | 18 +++++++++--------- color_darwin.go | 9 ++++----- file_darwin.go | 19 +++++++++---------- file_unix.go | 13 ++++++------- go.sum | 1 + internal/osa/generate.go | 3 --- internal/{zen => zenutil}/color.go | 2 +- .../cmd_darwin.go => zenutil/env_darwin.go} | 2 +- .../{cmd/cmd_unix.go => zenutil/env_unix.go} | 2 +- .../cmd_windows.go => zenutil/env_windows.go} | 2 +- internal/zenutil/osa_generate.go | 3 +++ .../generated.go => zenutil/osa_generated.go} | 2 +- .../generator.go => zenutil/osa_generator.go} | 4 ++-- .../scripts => zenutil/osascripts}/color.gots | 0 .../scripts => zenutil/osascripts}/file.gots | 0 .../scripts => zenutil/osascripts}/msg.gots | 0 internal/{osa => zenutil}/run_darwin.go | 6 ++---- internal/{zen => zenutil}/run_unix.go | 6 ++---- msg_darwin.go | 6 +++--- msg_unix.go | 4 ++-- 21 files changed, 51 insertions(+), 56 deletions(-) delete mode 100644 internal/osa/generate.go rename internal/{zen => zenutil}/color.go (98%) rename internal/{cmd/cmd_darwin.go => zenutil/env_darwin.go} (80%) rename internal/{cmd/cmd_unix.go => zenutil/env_unix.go} (85%) rename internal/{cmd/cmd_windows.go => zenutil/env_windows.go} (80%) create mode 100644 internal/zenutil/osa_generate.go rename internal/{osa/generated.go => zenutil/osa_generated.go} (99%) rename internal/{osa/generator.go => zenutil/osa_generator.go} (95%) rename internal/{osa/scripts => zenutil/osascripts}/color.gots (100%) rename internal/{osa/scripts => zenutil/osascripts}/file.gots (100%) rename internal/{osa/scripts => zenutil/osascripts}/msg.gots (100%) rename internal/{osa => zenutil}/run_darwin.go (93%) rename internal/{zen => zenutil}/run_unix.go (82%) diff --git a/README.md b/README.md index f07862a..3c98483 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,9 @@ Why reinvent this particular wheel? * Unicode support * WSL/Cygwin/MSYS2 [support](https://github.com/ncruces/zenity/wiki/Zenity-for-WSL,-Cygwin,-MSYS2) * on macOS: - * only dependency is `osascript` (with [JXA](https://developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/Introduction.html))\ - JavaScript is easier to template (with `html/template`) + * only dependency is `osascript` + (with [JXA](https://developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/Introduction.html); + `html/template` makes JavaScript easy to template) * on other Unixes: * wraps either one of `qarma`, `zenity`, `matedialog`,\ in that order of preference diff --git a/cmd/zenity/main.go b/cmd/zenity/main.go index d7eb84d..2dca13a 100644 --- a/cmd/zenity/main.go +++ b/cmd/zenity/main.go @@ -9,7 +9,7 @@ import ( "strings" "github.com/ncruces/zenity" - "github.com/ncruces/zenity/internal/cmd" + "github.com/ncruces/zenity/internal/zenutil" ) //go:generate go run github.com/josephspurrier/goversioninfo/cmd/goversioninfo -platform-specific -manifest=win.manifest @@ -56,7 +56,7 @@ func main() { flag.Parse() validateFlags() opts := loadFlags() - cmd.Command = true + zenutil.Command = true switch { case errorDlg: @@ -201,7 +201,7 @@ func loadFlags() []zenity.Option { options = append(options, zenity.ShowHidden()) } - cmd.Separator = separator + zenutil.Separator = separator return options } @@ -209,12 +209,12 @@ func loadFlags() []zenity.Option { func msgResult(ok bool, err error) { if err == zenity.ErrExtraButton { os.Stdout.WriteString(extraButton) - os.Stdout.WriteString(cmd.LineBreak) + os.Stdout.WriteString(zenutil.LineBreak) os.Exit(1) } if err != nil { os.Stderr.WriteString(err.Error()) - os.Stderr.WriteString(cmd.LineBreak) + os.Stderr.WriteString(zenutil.LineBreak) os.Exit(-1) } if ok { @@ -226,25 +226,25 @@ func msgResult(ok bool, err error) { func strResult(s string, err error) { if err != nil { os.Stderr.WriteString(err.Error()) - os.Stderr.WriteString(cmd.LineBreak) + os.Stderr.WriteString(zenutil.LineBreak) os.Exit(-1) } if s == "" { os.Exit(1) } os.Stdout.WriteString(s) - os.Stdout.WriteString(cmd.LineBreak) + os.Stdout.WriteString(zenutil.LineBreak) os.Exit(0) } func lstResult(l []string, err error) { if err != nil { os.Stderr.WriteString(err.Error()) - os.Stderr.WriteString(cmd.LineBreak) + os.Stderr.WriteString(zenutil.LineBreak) os.Exit(-1) } os.Stdout.WriteString(strings.Join(l, separator)) - os.Stdout.WriteString(cmd.LineBreak) + os.Stdout.WriteString(zenutil.LineBreak) if l == nil { os.Exit(1) } diff --git a/color_darwin.go b/color_darwin.go index 686ee09..de76781 100644 --- a/color_darwin.go +++ b/color_darwin.go @@ -4,25 +4,24 @@ import ( "image/color" "os/exec" - "github.com/ncruces/zenity/internal/osa" - "github.com/ncruces/zenity/internal/zen" + "github.com/ncruces/zenity/internal/zenutil" ) func SelectColor(options ...Option) (color.Color, error) { opts := optsParse(options) - var data osa.Color + var data zenutil.Color if opts.color != nil { r, g, b, _ := opts.color.RGBA() data.Color = []float32{float32(r) / 0xffff, float32(g) / 0xffff, float32(b) / 0xffff} } - out, err := osa.Run("color", data) + out, err := zenutil.Run("color", data) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { return nil, nil } if err != nil { return nil, err } - return zen.ParseColor(string(out)), nil + return zenutil.ParseColor(string(out)), nil } diff --git a/file_darwin.go b/file_darwin.go index e8463f8..565ff98 100644 --- a/file_darwin.go +++ b/file_darwin.go @@ -4,14 +4,13 @@ import ( "os/exec" "strings" - "github.com/ncruces/zenity/internal/cmd" - "github.com/ncruces/zenity/internal/osa" + "github.com/ncruces/zenity/internal/zenutil" ) func SelectFile(options ...Option) (string, error) { opts := optsParse(options) - data := osa.File{ + data := zenutil.File{ Prompt: opts.title, Invisibles: opts.hidden, } @@ -23,7 +22,7 @@ func SelectFile(options ...Option) (string, error) { } data.Location, _ = splitDirAndName(opts.filename) - out, err := osa.Run("file", data) + out, err := zenutil.Run("file", data) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { return "", nil } @@ -39,11 +38,11 @@ func SelectFile(options ...Option) (string, error) { func SelectFileMutiple(options ...Option) ([]string, error) { opts := optsParse(options) - data := osa.File{ + data := zenutil.File{ Prompt: opts.title, Invisibles: opts.hidden, Multiple: true, - Separator: cmd.Separator, + Separator: zenutil.Separator, } if opts.directory { data.Operation = "chooseFolder" @@ -53,7 +52,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) { } data.Location, _ = splitDirAndName(opts.filename) - out, err := osa.Run("file", data) + out, err := zenutil.Run("file", data) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { return nil, nil } @@ -66,13 +65,13 @@ func SelectFileMutiple(options ...Option) ([]string, error) { if len(out) == 0 { return nil, nil } - return strings.Split(string(out), cmd.Separator), nil + return strings.Split(string(out), zenutil.Separator), nil } func SelectFileSave(options ...Option) (string, error) { opts := optsParse(options) - data := osa.File{ + data := zenutil.File{ Prompt: opts.title, } if opts.directory { @@ -83,7 +82,7 @@ func SelectFileSave(options ...Option) (string, error) { } data.Location, data.Name = splitDirAndName(opts.filename) - out, err := osa.Run("file", data) + out, err := zenutil.Run("file", data) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { return "", nil } diff --git a/file_unix.go b/file_unix.go index 5498814..a51228a 100644 --- a/file_unix.go +++ b/file_unix.go @@ -6,8 +6,7 @@ import ( "os/exec" "strings" - "github.com/ncruces/zenity/internal/cmd" - "github.com/ncruces/zenity/internal/zen" + "github.com/ncruces/zenity/internal/zenutil" ) // Display file selection dialog. @@ -30,7 +29,7 @@ func SelectFile(options ...Option) (string, error) { } args = append(args, initFilters(opts.filters)...) - out, err := zen.Run(args) + out, err := zenutil.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { return "", nil } @@ -51,7 +50,7 @@ func SelectFile(options ...Option) (string, error) { func SelectFileMutiple(options ...Option) ([]string, error) { opts := optsParse(options) - args := []string{"--file-selection", "--multiple", "--separator", cmd.Separator} + args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator} if opts.directory { args = append(args, "--directory") } @@ -63,7 +62,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) { } args = append(args, initFilters(opts.filters)...) - out, err := zen.Run(args) + out, err := zenutil.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { return nil, nil } @@ -73,7 +72,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) { if len(out) > 0 { out = out[:len(out)-1] } - return strings.Split(string(out), cmd.Separator), nil + return strings.Split(string(out), zenutil.Separator), nil } // Display save file selection dialog. @@ -99,7 +98,7 @@ func SelectFileSave(options ...Option) (string, error) { } args = append(args, initFilters(opts.filters)...) - out, err := zen.Run(args) + out, err := zenutil.Run(args) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 { return "", nil } diff --git a/go.sum b/go.sum index 9760eff..90284fd 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ golang.org/x/image v0.0.0-20191214001246-9130b4cfad52 h1:2fktqPPvDiVEEVT/vSTeoUPXfmRxRaGy6GU8jypvEn0= golang.org/x/image v0.0.0-20191214001246-9130b4cfad52/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/internal/osa/generate.go b/internal/osa/generate.go deleted file mode 100644 index 774ea8d..0000000 --- a/internal/osa/generate.go +++ /dev/null @@ -1,3 +0,0 @@ -package osa - -//go:generate go run generator.go scripts/ diff --git a/internal/zen/color.go b/internal/zenutil/color.go similarity index 98% rename from internal/zen/color.go rename to internal/zenutil/color.go index 68e8d92..2c8011b 100644 --- a/internal/zen/color.go +++ b/internal/zenutil/color.go @@ -1,4 +1,4 @@ -package zen +package zenutil import ( "fmt" diff --git a/internal/cmd/cmd_darwin.go b/internal/zenutil/env_darwin.go similarity index 80% rename from internal/cmd/cmd_darwin.go rename to internal/zenutil/env_darwin.go index 7d8d806..097179b 100644 --- a/internal/cmd/cmd_darwin.go +++ b/internal/zenutil/env_darwin.go @@ -1,4 +1,4 @@ -package cmd +package zenutil const LineBreak = "\n" diff --git a/internal/cmd/cmd_unix.go b/internal/zenutil/env_unix.go similarity index 85% rename from internal/cmd/cmd_unix.go rename to internal/zenutil/env_unix.go index d905bd8..977b44a 100644 --- a/internal/cmd/cmd_unix.go +++ b/internal/zenutil/env_unix.go @@ -1,6 +1,6 @@ // +build !windows,!darwin -package cmd +package zenutil const LineBreak = "\n" diff --git a/internal/cmd/cmd_windows.go b/internal/zenutil/env_windows.go similarity index 80% rename from internal/cmd/cmd_windows.go rename to internal/zenutil/env_windows.go index e3235fc..3e2fa2c 100644 --- a/internal/cmd/cmd_windows.go +++ b/internal/zenutil/env_windows.go @@ -1,4 +1,4 @@ -package cmd +package zenutil const LineBreak = "\r\n" diff --git a/internal/zenutil/osa_generate.go b/internal/zenutil/osa_generate.go new file mode 100644 index 0000000..0bcc980 --- /dev/null +++ b/internal/zenutil/osa_generate.go @@ -0,0 +1,3 @@ +package zenutil + +//go:generate go run osa_generator.go osascripts/ diff --git a/internal/osa/generated.go b/internal/zenutil/osa_generated.go similarity index 99% rename from internal/osa/generated.go rename to internal/zenutil/osa_generated.go index ab02535..57e0038 100644 --- a/internal/osa/generated.go +++ b/internal/zenutil/osa_generated.go @@ -1,7 +1,7 @@ // Code generated by zenity; DO NOT EDIT. // +build darwin -package osa +package zenutil import "html/template" diff --git a/internal/osa/generator.go b/internal/zenutil/osa_generator.go similarity index 95% rename from internal/osa/generator.go rename to internal/zenutil/osa_generator.go index fb3cfdc..f0bb274 100644 --- a/internal/osa/generator.go +++ b/internal/zenutil/osa_generator.go @@ -52,7 +52,7 @@ func main() { str.WriteString("{{end}}") } - out, err := os.Create("generated.go") + out, err := os.Create("osa_generated.go") if err != nil { log.Fatal(err) } @@ -71,7 +71,7 @@ func main() { var generator = template.Must(template.New("").Parse(`// Code generated by zenity; DO NOT EDIT. // +build darwin -package osa +package zenutil import "html/template" diff --git a/internal/osa/scripts/color.gots b/internal/zenutil/osascripts/color.gots similarity index 100% rename from internal/osa/scripts/color.gots rename to internal/zenutil/osascripts/color.gots diff --git a/internal/osa/scripts/file.gots b/internal/zenutil/osascripts/file.gots similarity index 100% rename from internal/osa/scripts/file.gots rename to internal/zenutil/osascripts/file.gots diff --git a/internal/osa/scripts/msg.gots b/internal/zenutil/osascripts/msg.gots similarity index 100% rename from internal/osa/scripts/msg.gots rename to internal/zenutil/osascripts/msg.gots diff --git a/internal/osa/run_darwin.go b/internal/zenutil/run_darwin.go similarity index 93% rename from internal/osa/run_darwin.go rename to internal/zenutil/run_darwin.go index f9c0b9b..a574092 100644 --- a/internal/osa/run_darwin.go +++ b/internal/zenutil/run_darwin.go @@ -1,12 +1,10 @@ -package osa +package zenutil import ( "os" "os/exec" "strings" "syscall" - - "github.com/ncruces/zenity/internal/cmd" ) func Run(script string, data interface{}) ([]byte, error) { @@ -20,7 +18,7 @@ func Run(script string, data interface{}) ([]byte, error) { res := buf.String() res = res[len("")] - if cmd.Command { + if Command { path, err := exec.LookPath("osascript") if err == nil { os.Stderr.Close() diff --git a/internal/zen/run_unix.go b/internal/zenutil/run_unix.go similarity index 82% rename from internal/zen/run_unix.go rename to internal/zenutil/run_unix.go index cc5c0ff..22b6f39 100644 --- a/internal/zen/run_unix.go +++ b/internal/zenutil/run_unix.go @@ -1,13 +1,11 @@ // +build !windows,!darwin -package zen +package zenutil import ( "os" "os/exec" "syscall" - - "github.com/ncruces/zenity/internal/cmd" ) var tool, path string @@ -23,7 +21,7 @@ func init() { } func Run(args []string) ([]byte, error) { - if cmd.Command && path != "" { + if Command && path != "" { syscall.Exec(path, append([]string{tool}, args...), os.Environ()) } return exec.Command(tool, args...).Output() diff --git a/msg_darwin.go b/msg_darwin.go index f7195ee..ae55bff 100644 --- a/msg_darwin.go +++ b/msg_darwin.go @@ -3,7 +3,7 @@ package zenity import ( "os/exec" - "github.com/ncruces/zenity/internal/osa" + "github.com/ncruces/zenity/internal/zenutil" ) func Question(text string, options ...Option) (bool, error) { @@ -24,7 +24,7 @@ func Error(text string, options ...Option) (bool, error) { func message(typ int, text string, options []Option) (bool, error) { opts := optsParse(options) - data := osa.Msg{Text: text} + data := zenutil.Msg{Text: text} dialog := typ == 0 || opts.icon != 0 if dialog { @@ -99,7 +99,7 @@ func message(typ int, text string, options []Option) (bool, error) { } } - out, err := osa.Run("msg", data) + out, err := zenutil.Run("msg", data) if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { return false, nil } diff --git a/msg_unix.go b/msg_unix.go index b9071dc..a4a79aa 100644 --- a/msg_unix.go +++ b/msg_unix.go @@ -5,7 +5,7 @@ package zenity import ( "os/exec" - "github.com/ncruces/zenity/internal/zen" + "github.com/ncruces/zenity/internal/zenutil" ) // Display question dialog. @@ -84,7 +84,7 @@ func message(arg, text string, options []Option) (bool, error) { args = append(args, "--icon-name=dialog-question") } - out, err := zen.Run(args) + out, err := zenutil.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