Refactoring.

This commit is contained in:
Nuno Cruces 2020-01-19 11:57:05 +00:00
parent f20505e1f4
commit 9668c61d8b
21 changed files with 51 additions and 56 deletions

View file

@ -34,8 +34,9 @@ Why reinvent this particular wheel?
* Unicode support * Unicode support
* WSL/Cygwin/MSYS2 [support](https://github.com/ncruces/zenity/wiki/Zenity-for-WSL,-Cygwin,-MSYS2) * WSL/Cygwin/MSYS2 [support](https://github.com/ncruces/zenity/wiki/Zenity-for-WSL,-Cygwin,-MSYS2)
* on macOS: * on macOS:
* only dependency is `osascript` (with [JXA](https://developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/Introduction.html))\ * only dependency is `osascript`
JavaScript is easier to template (with `html/template`) (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: * on other Unixes:
* wraps either one of `qarma`, `zenity`, `matedialog`,\ * wraps either one of `qarma`, `zenity`, `matedialog`,\
in that order of preference in that order of preference

View file

@ -9,7 +9,7 @@ import (
"strings" "strings"
"github.com/ncruces/zenity" "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 //go:generate go run github.com/josephspurrier/goversioninfo/cmd/goversioninfo -platform-specific -manifest=win.manifest
@ -56,7 +56,7 @@ func main() {
flag.Parse() flag.Parse()
validateFlags() validateFlags()
opts := loadFlags() opts := loadFlags()
cmd.Command = true zenutil.Command = true
switch { switch {
case errorDlg: case errorDlg:
@ -201,7 +201,7 @@ func loadFlags() []zenity.Option {
options = append(options, zenity.ShowHidden()) options = append(options, zenity.ShowHidden())
} }
cmd.Separator = separator zenutil.Separator = separator
return options return options
} }
@ -209,12 +209,12 @@ func loadFlags() []zenity.Option {
func msgResult(ok bool, err error) { func msgResult(ok bool, err error) {
if err == zenity.ErrExtraButton { if err == zenity.ErrExtraButton {
os.Stdout.WriteString(extraButton) os.Stdout.WriteString(extraButton)
os.Stdout.WriteString(cmd.LineBreak) os.Stdout.WriteString(zenutil.LineBreak)
os.Exit(1) os.Exit(1)
} }
if err != nil { if err != nil {
os.Stderr.WriteString(err.Error()) os.Stderr.WriteString(err.Error())
os.Stderr.WriteString(cmd.LineBreak) os.Stderr.WriteString(zenutil.LineBreak)
os.Exit(-1) os.Exit(-1)
} }
if ok { if ok {
@ -226,25 +226,25 @@ func msgResult(ok bool, err error) {
func strResult(s string, err error) { func strResult(s string, err error) {
if err != nil { if err != nil {
os.Stderr.WriteString(err.Error()) os.Stderr.WriteString(err.Error())
os.Stderr.WriteString(cmd.LineBreak) os.Stderr.WriteString(zenutil.LineBreak)
os.Exit(-1) os.Exit(-1)
} }
if s == "" { if s == "" {
os.Exit(1) os.Exit(1)
} }
os.Stdout.WriteString(s) os.Stdout.WriteString(s)
os.Stdout.WriteString(cmd.LineBreak) os.Stdout.WriteString(zenutil.LineBreak)
os.Exit(0) os.Exit(0)
} }
func lstResult(l []string, err error) { func lstResult(l []string, err error) {
if err != nil { if err != nil {
os.Stderr.WriteString(err.Error()) os.Stderr.WriteString(err.Error())
os.Stderr.WriteString(cmd.LineBreak) os.Stderr.WriteString(zenutil.LineBreak)
os.Exit(-1) os.Exit(-1)
} }
os.Stdout.WriteString(strings.Join(l, separator)) os.Stdout.WriteString(strings.Join(l, separator))
os.Stdout.WriteString(cmd.LineBreak) os.Stdout.WriteString(zenutil.LineBreak)
if l == nil { if l == nil {
os.Exit(1) os.Exit(1)
} }

View file

@ -4,25 +4,24 @@ import (
"image/color" "image/color"
"os/exec" "os/exec"
"github.com/ncruces/zenity/internal/osa" "github.com/ncruces/zenity/internal/zenutil"
"github.com/ncruces/zenity/internal/zen"
) )
func SelectColor(options ...Option) (color.Color, error) { func SelectColor(options ...Option) (color.Color, error) {
opts := optsParse(options) opts := optsParse(options)
var data osa.Color var data zenutil.Color
if opts.color != nil { if opts.color != nil {
r, g, b, _ := opts.color.RGBA() r, g, b, _ := opts.color.RGBA()
data.Color = []float32{float32(r) / 0xffff, float32(g) / 0xffff, float32(b) / 0xffff} 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 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return nil, nil return nil, nil
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
return zen.ParseColor(string(out)), nil return zenutil.ParseColor(string(out)), nil
} }

View file

@ -4,14 +4,13 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"github.com/ncruces/zenity/internal/cmd" "github.com/ncruces/zenity/internal/zenutil"
"github.com/ncruces/zenity/internal/osa"
) )
func SelectFile(options ...Option) (string, error) { func SelectFile(options ...Option) (string, error) {
opts := optsParse(options) opts := optsParse(options)
data := osa.File{ data := zenutil.File{
Prompt: opts.title, Prompt: opts.title,
Invisibles: opts.hidden, Invisibles: opts.hidden,
} }
@ -23,7 +22,7 @@ func SelectFile(options ...Option) (string, error) {
} }
data.Location, _ = splitDirAndName(opts.filename) 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 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return "", nil return "", nil
} }
@ -39,11 +38,11 @@ func SelectFile(options ...Option) (string, error) {
func SelectFileMutiple(options ...Option) ([]string, error) { func SelectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options) opts := optsParse(options)
data := osa.File{ data := zenutil.File{
Prompt: opts.title, Prompt: opts.title,
Invisibles: opts.hidden, Invisibles: opts.hidden,
Multiple: true, Multiple: true,
Separator: cmd.Separator, Separator: zenutil.Separator,
} }
if opts.directory { if opts.directory {
data.Operation = "chooseFolder" data.Operation = "chooseFolder"
@ -53,7 +52,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
} }
data.Location, _ = splitDirAndName(opts.filename) 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 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return nil, nil return nil, nil
} }
@ -66,13 +65,13 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
if len(out) == 0 { if len(out) == 0 {
return nil, nil 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) { func SelectFileSave(options ...Option) (string, error) {
opts := optsParse(options) opts := optsParse(options)
data := osa.File{ data := zenutil.File{
Prompt: opts.title, Prompt: opts.title,
} }
if opts.directory { if opts.directory {
@ -83,7 +82,7 @@ func SelectFileSave(options ...Option) (string, error) {
} }
data.Location, data.Name = splitDirAndName(opts.filename) 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 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return "", nil return "", nil
} }

View file

@ -6,8 +6,7 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"github.com/ncruces/zenity/internal/cmd" "github.com/ncruces/zenity/internal/zenutil"
"github.com/ncruces/zenity/internal/zen"
) )
// Display file selection dialog. // Display file selection dialog.
@ -30,7 +29,7 @@ func SelectFile(options ...Option) (string, error) {
} }
args = append(args, initFilters(opts.filters)...) 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 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
return "", nil return "", nil
} }
@ -51,7 +50,7 @@ func SelectFile(options ...Option) (string, error) {
func SelectFileMutiple(options ...Option) ([]string, error) { func SelectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options) opts := optsParse(options)
args := []string{"--file-selection", "--multiple", "--separator", cmd.Separator} args := []string{"--file-selection", "--multiple", "--separator", zenutil.Separator}
if opts.directory { if opts.directory {
args = append(args, "--directory") args = append(args, "--directory")
} }
@ -63,7 +62,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
} }
args = append(args, initFilters(opts.filters)...) 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 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
return nil, nil return nil, nil
} }
@ -73,7 +72,7 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
if len(out) > 0 { if len(out) > 0 {
out = out[:len(out)-1] 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. // Display save file selection dialog.
@ -99,7 +98,7 @@ func SelectFileSave(options ...Option) (string, error) {
} }
args = append(args, initFilters(opts.filters)...) 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 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
return "", nil return "", nil
} }

1
go.sum
View file

@ -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 h1:2fktqPPvDiVEEVT/vSTeoUPXfmRxRaGy6GU8jypvEn0=
golang.org/x/image v0.0.0-20191214001246-9130b4cfad52/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= 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= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View file

@ -1,3 +0,0 @@
package osa
//go:generate go run generator.go scripts/

View file

@ -1,4 +1,4 @@
package zen package zenutil
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package cmd package zenutil
const LineBreak = "\n" const LineBreak = "\n"

View file

@ -1,6 +1,6 @@
// +build !windows,!darwin // +build !windows,!darwin
package cmd package zenutil
const LineBreak = "\n" const LineBreak = "\n"

View file

@ -1,4 +1,4 @@
package cmd package zenutil
const LineBreak = "\r\n" const LineBreak = "\r\n"

View file

@ -0,0 +1,3 @@
package zenutil
//go:generate go run osa_generator.go osascripts/

View file

@ -1,7 +1,7 @@
// Code generated by zenity; DO NOT EDIT. // Code generated by zenity; DO NOT EDIT.
// +build darwin // +build darwin
package osa package zenutil
import "html/template" import "html/template"

View file

@ -52,7 +52,7 @@ func main() {
str.WriteString("</script>{{end}}") str.WriteString("</script>{{end}}")
} }
out, err := os.Create("generated.go") out, err := os.Create("osa_generated.go")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -71,7 +71,7 @@ func main() {
var generator = template.Must(template.New("").Parse(`// Code generated by zenity; DO NOT EDIT. var generator = template.Must(template.New("").Parse(`// Code generated by zenity; DO NOT EDIT.
// +build darwin // +build darwin
package osa package zenutil
import "html/template" import "html/template"

View file

@ -1,12 +1,10 @@
package osa package zenutil
import ( import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"syscall" "syscall"
"github.com/ncruces/zenity/internal/cmd"
) )
func Run(script string, data interface{}) ([]byte, error) { func Run(script string, data interface{}) ([]byte, error) {
@ -20,7 +18,7 @@ func Run(script string, data interface{}) ([]byte, error) {
res := buf.String() res := buf.String()
res = res[len("<script>") : len(res)-len("\n</script>")] res = res[len("<script>") : len(res)-len("\n</script>")]
if cmd.Command { if Command {
path, err := exec.LookPath("osascript") path, err := exec.LookPath("osascript")
if err == nil { if err == nil {
os.Stderr.Close() os.Stderr.Close()

View file

@ -1,13 +1,11 @@
// +build !windows,!darwin // +build !windows,!darwin
package zen package zenutil
import ( import (
"os" "os"
"os/exec" "os/exec"
"syscall" "syscall"
"github.com/ncruces/zenity/internal/cmd"
) )
var tool, path string var tool, path string
@ -23,7 +21,7 @@ func init() {
} }
func Run(args []string) ([]byte, error) { func Run(args []string) ([]byte, error) {
if cmd.Command && path != "" { if Command && path != "" {
syscall.Exec(path, append([]string{tool}, args...), os.Environ()) syscall.Exec(path, append([]string{tool}, args...), os.Environ())
} }
return exec.Command(tool, args...).Output() return exec.Command(tool, args...).Output()

View file

@ -3,7 +3,7 @@ package zenity
import ( import (
"os/exec" "os/exec"
"github.com/ncruces/zenity/internal/osa" "github.com/ncruces/zenity/internal/zenutil"
) )
func Question(text string, options ...Option) (bool, error) { 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) { func message(typ int, text string, options []Option) (bool, error) {
opts := optsParse(options) opts := optsParse(options)
data := osa.Msg{Text: text} data := zenutil.Msg{Text: text}
dialog := typ == 0 || opts.icon != 0 dialog := typ == 0 || opts.icon != 0
if dialog { 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 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return false, nil return false, nil
} }

View file

@ -5,7 +5,7 @@ package zenity
import ( import (
"os/exec" "os/exec"
"github.com/ncruces/zenity/internal/zen" "github.com/ncruces/zenity/internal/zenutil"
) )
// Display question dialog. // Display question dialog.
@ -84,7 +84,7 @@ func message(arg, text string, options []Option) (bool, error) {
args = append(args, "--icon-name=dialog-question") 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 err, ok := err.(*exec.ExitError); ok && err.ExitCode() != 255 {
if len(out) > 0 && string(out[:len(out)-1]) == opts.extra { if len(out) > 0 && string(out[:len(out)-1]) == opts.extra {
return false, ErrExtraButton return false, ErrExtraButton