Add IsAvailable.

This commit is contained in:
Nuno Cruces 2022-12-14 23:53:30 +00:00
parent 958e0530a9
commit 02564b6232
6 changed files with 43 additions and 25 deletions

View file

@ -8,12 +8,16 @@ import (
"os" "os"
"os/exec" "os/exec"
"strconv" "strconv"
"sync"
"syscall" "syscall"
) )
var tool, path string var (
tool, path string
pathOnce sync.Once
)
func init() { func initPath() {
for _, tool = range [3]string{"qarma", "zenity", "matedialog"} { for _, tool = range [3]string{"qarma", "zenity", "matedialog"} {
path, _ = exec.LookPath(tool) path, _ = exec.LookPath(tool)
if path != "" { if path != "" {
@ -23,8 +27,15 @@ func init() {
tool = "zenity" tool = "zenity"
} }
// IsAvailable is internal.
func IsAvailable() bool {
pathOnce.Do(initPath)
return path != ""
}
// Run is internal. // Run is internal.
func Run(ctx context.Context, args []string) ([]byte, error) { func Run(ctx context.Context, args []string) ([]byte, error) {
pathOnce.Do(initPath)
if Command && path != "" { if Command && path != "" {
if Timeout > 0 { if Timeout > 0 {
args = append(args, "--timeout", strconv.Itoa(Timeout)) args = append(args, "--timeout", strconv.Itoa(Timeout))
@ -44,6 +55,7 @@ func Run(ctx context.Context, args []string) ([]byte, error) {
// RunProgress is internal. // RunProgress is internal.
func RunProgress(ctx context.Context, max int, extra *string, args []string) (*progressDialog, error) { func RunProgress(ctx context.Context, max int, extra *string, args []string) (*progressDialog, error) {
pathOnce.Do(initPath)
if Command && path != "" { if Command && path != "" {
if Timeout > 0 { if Timeout > 0 {
args = append(args, "--timeout", strconv.Itoa(Timeout)) args = append(args, "--timeout", strconv.Itoa(Timeout))

View file

@ -41,7 +41,7 @@ func TestRunProgress(t *testing.T) {
} }
func skip(err error) (bool, error) { func skip(err error) (bool, error) {
if _, ok := err.(*exec.Error); ok { if _, ok := err.(*exec.Error); ok && !IsAvailable() {
// zenity was not found in path // zenity was not found in path
return true, err return true, err
} }

View file

@ -30,6 +30,12 @@ const ErrExtraButton = zenutil.ErrExtraButton
// ErrUnsupported is returned when a combination of options is not supported. // ErrUnsupported is returned when a combination of options is not supported.
const ErrUnsupported = zenutil.ErrUnsupported const ErrUnsupported = zenutil.ErrUnsupported
// IsAvailable reports whether dependencies of the package are installed.
// It always returns true on Windows and macOS.
func IsAvailable() bool {
return isAvailable()
}
type options struct { type options struct {
// General options // General options
title *string title *string
@ -182,6 +188,16 @@ func WindowIcon(icon any) Option {
return funcOption(func(o *options) { o.windowIcon = icon }) return funcOption(func(o *options) { o.windowIcon = icon })
} }
// Attach returns an Option to set the parent window to attach to.
//
// Attach accepts:
// - a window id (int) on Unix
// - a window handle (~uintptr) on Windows
// - an application name (string) or process id (int) on macOS
func Attach(id any) Option {
return attach(id)
}
// Modal returns an Option to set the modal hint. // Modal returns an Option to set the modal hint.
func Modal() Option { func Modal() Option {
return funcOption(func(o *options) { o.modal = true }) return funcOption(func(o *options) { o.modal = true })

View file

@ -1,12 +1,8 @@
package zenity package zenity
// Attach returns an Option to set the parent window to attach to. func isAvailable() bool { return true }
//
// Attach accepts: func attach(id any) Option {
// - a window id (int) on Unix
// - a window handle (~uintptr) on Windows
// - an application name (string) or process id (int) on macOS
func Attach(id any) Option {
switch id.(type) { switch id.(type) {
case int, string: case int, string:
default: default:

View file

@ -2,12 +2,10 @@
package zenity package zenity
// Attach returns an Option to set the parent window to attach to. import "github.com/ncruces/zenity/internal/zenutil"
//
// Attach accepts: func isAvailable() bool { return zenutil.IsAvailable() }
// - a window id (int) on Unix
// - a window handle (~uintptr) on Windows func attach(id any) Option {
// - an application name (string) or process id (int) on macOS return funcOption(func(o *options) { o.attach = id.(int) })
func Attach(id int) Option {
return funcOption(func(o *options) { o.attach = id })
} }

View file

@ -6,13 +6,9 @@ import (
"github.com/ncruces/zenity/internal/win" "github.com/ncruces/zenity/internal/win"
) )
// Attach returns an Option to set the parent window to attach to. func isAvailable() bool { return true }
//
// Attach accepts: func attach(id any) Option {
// - a window id (int) on Unix
// - a window handle (~uintptr) on Windows
// - an application name (string) or process id (int) on macOS
func Attach(id any) Option {
if v := reflect.ValueOf(id); v.Kind() == reflect.Uintptr { if v := reflect.ValueOf(id); v.Kind() == reflect.Uintptr {
id = win.HWND(uintptr(v.Uint())) id = win.HWND(uintptr(v.Uint()))
} else { } else {