Attach check types.
This commit is contained in:
parent
b902eebb1d
commit
52eb3aeda1
6 changed files with 56 additions and 12 deletions
|
@ -15,7 +15,6 @@ import (
|
|||
"path/filepath"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
@ -448,11 +447,7 @@ func loadFlags() []zenity.Option {
|
|||
}
|
||||
|
||||
if attach != "" {
|
||||
if i, err := strconv.ParseUint(attach, 0, 64); err != nil {
|
||||
opts = append(opts, zenity.Attach(attach))
|
||||
} else {
|
||||
opts = append(opts, zenity.Attach(i))
|
||||
}
|
||||
opts = append(opts, zenity.Attach(zenutil.ParseWindowId(attach)))
|
||||
}
|
||||
|
||||
// Message options
|
||||
|
|
|
@ -1,7 +1,16 @@
|
|||
package zenutil
|
||||
|
||||
import "strconv"
|
||||
|
||||
// These are internal.
|
||||
var (
|
||||
Separator = "\x00"
|
||||
LineBreak = "\n"
|
||||
)
|
||||
|
||||
func ParseWindowId(id string) any {
|
||||
if pid, err := strconv.ParseUint(id, 0, 64); err == nil {
|
||||
return int(pid)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
|
|
@ -2,8 +2,15 @@
|
|||
|
||||
package zenutil
|
||||
|
||||
import "strconv"
|
||||
|
||||
// These are internal.
|
||||
var (
|
||||
Separator = "\x1e"
|
||||
LineBreak = "\n"
|
||||
)
|
||||
|
||||
func ParseWindowId(id string) int {
|
||||
wid, _ := strconv.ParseUint(id, 0, 64)
|
||||
return int(wid)
|
||||
}
|
||||
|
|
|
@ -1,7 +1,14 @@
|
|||
package zenutil
|
||||
|
||||
import "strconv"
|
||||
|
||||
// These are internal.
|
||||
var (
|
||||
Separator string
|
||||
LineBreak = "\r\n"
|
||||
)
|
||||
|
||||
func ParseWindowId(id string) uintptr {
|
||||
hwnd, _ := strconv.ParseUint(id, 0, 64)
|
||||
return uintptr(hwnd)
|
||||
}
|
||||
|
|
33
zenity.go
33
zenity.go
|
@ -13,6 +13,8 @@ package zenity
|
|||
import (
|
||||
"context"
|
||||
"image/color"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/ncruces/zenity/internal/zenutil"
|
||||
|
@ -163,8 +165,7 @@ const (
|
|||
// The string can be a GTK icon name (Unix), or a path (Windows and macOS).
|
||||
func Icon(icon any) Option {
|
||||
switch icon.(type) {
|
||||
case string:
|
||||
case DialogIcon:
|
||||
case DialogIcon, string:
|
||||
default:
|
||||
panic("interface conversion: expected string or DialogIcon")
|
||||
}
|
||||
|
@ -176,8 +177,7 @@ func Icon(icon any) Option {
|
|||
// WindowIcon accepts a DialogIcon, or a string path.
|
||||
func WindowIcon(icon any) Option {
|
||||
switch icon.(type) {
|
||||
case string:
|
||||
case DialogIcon:
|
||||
case DialogIcon, string:
|
||||
default:
|
||||
panic("interface conversion: expected string or DialogIcon")
|
||||
}
|
||||
|
@ -192,7 +192,32 @@ func CustomIcon(path string) Option {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
if v := reflect.ValueOf(id); v.Kind() == reflect.Uintptr {
|
||||
id = uintptr(v.Uint())
|
||||
} else {
|
||||
panic("interface conversion: expected uintptr")
|
||||
}
|
||||
|
||||
case "darwin":
|
||||
switch id.(type) {
|
||||
case int, string:
|
||||
default:
|
||||
panic("interface conversion: expected int or string")
|
||||
}
|
||||
|
||||
default:
|
||||
if _, ok := id.(int); !ok {
|
||||
panic("interface conversion: expected int")
|
||||
}
|
||||
}
|
||||
return funcOption(func(o *options) { o.attach = id })
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,8 @@ import (
|
|||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ncruces/zenity/internal/zenutil"
|
||||
)
|
||||
|
||||
func Test_applyOptions(t *testing.T) {
|
||||
|
@ -27,9 +29,8 @@ func Test_applyOptions(t *testing.T) {
|
|||
{name: "WindowIcon", args: WindowIcon("error"), want: options{windowIcon: "error"}},
|
||||
{name: "Icon", args: Icon(ErrorIcon), want: options{icon: ErrorIcon}},
|
||||
{name: "Icon", args: Icon("error"), want: options{icon: "error"}},
|
||||
{name: "Attach", args: Attach(12345), want: options{attach: 12345}},
|
||||
{name: "Attach", args: Attach("Terminal"), want: options{attach: "Terminal"}},
|
||||
{name: "Modal", args: Modal(), want: options{modal: true}},
|
||||
{name: "Attach", args: Attach(zenutil.ParseWindowId("12345")), want: options{attach: zenutil.ParseWindowId("12345")}},
|
||||
|
||||
// Message options
|
||||
{name: "NoWrap", args: NoWrap(), want: options{noWrap: true}},
|
||||
|
|
Loading…
Reference in a new issue