Skip to content
This repository was archived by the owner on Jun 3, 2022. It is now read-only.

Commit ac5c933

Browse files
committed
Initial commit
0 parents  commit ac5c933

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2472
-0
lines changed

.appveyor.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: "{build}"
2+
3+
clone_depth: 1
4+
5+
clone_folder: c:\gopath\src\github.com\gen2brain\beeep
6+
7+
environment:
8+
GOPATH: c:\gopath
9+
10+
install:
11+
- echo %GOPATH%
12+
- set PATH=%GOPATH%\bin;c:\go\bin;%PATH%
13+
- go version
14+
- go env
15+
- go get -u github.com/gen2brain/beeep
16+
17+
build_script:
18+
- go get -t ./...

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: go
2+
3+
go:
4+
- 1.9.x
5+
6+
install:
7+
- go get -t ./...
8+
9+
script:
10+
- go build ./...

LICENSE

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyright (c) 2017, Milan Nikolic <gen2brain>
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## dlgs
2+
[![TravisCI Build Status](https://travis-ci.org/gen2brain/dlgs.svg?branch=master)](https://travis-ci.org/gen2brain/beeep)
3+
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/4u7avrhsdxua2c9b?svg=true)](https://ci.appveyor.com/project/gen2brain/dlgs)
4+
[![GoDoc](https://godoc.org/github.com/gen2brain/dlgs?status.svg)](https://godoc.org/github.com/gen2brain/dlgs)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/gen2brain/dlgs?branch=master)](https://goreportcard.com/report/github.com/gen2brain/dlgs)
6+
7+
`dlgs` is a cross-platform library for displaying dialogs and input boxes.
8+
9+
### Installation
10+
11+
go get -u github.com/gen2brain/dlgs
12+
13+
### Documentation
14+
15+
Documentation on [GoDoc](https://godoc.org/github.com/gen2brain/dlgs).
16+
17+
### Examples
18+
19+
```go
20+
item, _, err := dlgs.List("List", "Select item from list:", []string{"Bug", "New Feature", "Improvement"})
21+
if err != nil {
22+
panic(err)
23+
}
24+
```
25+
26+
```go
27+
passwd, _, err := dlgs.Password("Password", "Enter your API key:")
28+
if err != nil {
29+
panic(err)
30+
}
31+
```
32+
33+
```go
34+
yes, err := dlgs.Question("Question", "Are you sure you want to format this media?", true)
35+
if err != nil {
36+
panic(err)
37+
}
38+
```
39+
40+
## More
41+
42+
For cross-platform notifications and alerts see [beeep](https://github.com/gen2brain/beeep).

color_darwin.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// +build darwin,!linux,!windows,!js
2+
3+
package dlgs
4+
5+
import (
6+
"fmt"
7+
"image/color"
8+
"os/exec"
9+
"strconv"
10+
"strings"
11+
"syscall"
12+
)
13+
14+
// Color displays a color selection dialog, returning the selected color and a bool for success.
15+
func Color(title, defaultColorHex string) (color.Color, bool, error) {
16+
osa, err := exec.LookPath("osascript")
17+
if err != nil {
18+
return nil, false, err
19+
}
20+
21+
var ur, ug, ub uint8
22+
fmt.Sscanf(defaultColorHex, "#%02x%02x%02x", &ur, &ug, &ub)
23+
24+
r := strconv.Itoa(int(ur))
25+
g := strconv.Itoa(int(ug))
26+
b := strconv.Itoa(int(ub))
27+
28+
o, err := exec.Command(osa, "-e", `set T to choose color default color {`+r+`, `+g+`, `+b+`}`).Output()
29+
if err != nil {
30+
if exitError, ok := err.(*exec.ExitError); ok {
31+
ws := exitError.Sys().(syscall.WaitStatus)
32+
return nil, ws.ExitStatus() == 0, nil
33+
}
34+
}
35+
36+
ret := true
37+
out := strings.TrimSpace(string(o))
38+
if out == "" {
39+
ret = false
40+
}
41+
42+
return parseColor(out), ret, err
43+
}
44+
45+
// parseColor returns color from output string.
46+
func parseColor(out string) color.Color {
47+
col := color.RGBA{}
48+
49+
for _, s := range []string{"rgb", "(", ")"} {
50+
out = strings.Replace(out, s, "", -1)
51+
}
52+
t := strings.Split(out, ", ")
53+
if len(t) == 3 {
54+
r, _ := strconv.ParseUint(t[0], 10, 32)
55+
g, _ := strconv.ParseUint(t[1], 10, 32)
56+
b, _ := strconv.ParseUint(t[2], 10, 32)
57+
58+
col.R = uint8(r)
59+
col.G = uint8(g)
60+
col.B = uint8(b)
61+
}
62+
63+
return col
64+
}

color_js.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// +build js
2+
3+
package dlgs
4+
5+
import (
6+
"image/color"
7+
//"github.com/gopherjs/gopherjs/js"
8+
)
9+
10+
// Color displays a color selection dialog, returning the selected color and a bool for success.
11+
func Color(title, defaultColorHex string) (color.Color, bool, error) {
12+
return nil, false, ErrNotImplemented
13+
}

color_linux.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// +build linux,!windows,!darwin,!js
2+
3+
package dlgs
4+
5+
import (
6+
"fmt"
7+
"image/color"
8+
"os/exec"
9+
"strconv"
10+
"strings"
11+
"syscall"
12+
)
13+
14+
// Color displays a color selection dialog, returning the selected color and a bool for success.
15+
func Color(title, defaultColorHex string) (color.Color, bool, error) {
16+
cmd, err := cmdPath()
17+
if err != nil {
18+
return nil, false, err
19+
}
20+
21+
o, err := exec.Command(cmd, "--color-selection", "--title", title, "--color", defaultColorHex).Output()
22+
if err != nil {
23+
if exitError, ok := err.(*exec.ExitError); ok {
24+
ws := exitError.Sys().(syscall.WaitStatus)
25+
return nil, ws.ExitStatus() == 0, nil
26+
}
27+
}
28+
29+
ret := true
30+
out := strings.TrimSpace(string(o))
31+
if out == "" {
32+
ret = false
33+
}
34+
35+
return parseColor(out), ret, err
36+
}
37+
38+
// parseColor returns color from output string.
39+
func parseColor(out string) color.Color {
40+
col := color.RGBA{}
41+
42+
if strings.HasPrefix(out, "#") {
43+
var r, g, b uint8
44+
fmt.Sscanf(out, "#%02x%02x%02x", &r, &g, &b)
45+
46+
col.R = uint8(r)
47+
col.G = uint8(g)
48+
col.B = uint8(b)
49+
} else if strings.HasPrefix(out, "rgba(") {
50+
for _, s := range []string{"rgba", "(", ")"} {
51+
out = strings.Replace(out, s, "", -1)
52+
}
53+
t := strings.Split(out, ",")
54+
if len(t) == 4 {
55+
r, _ := strconv.ParseUint(t[0], 10, 8)
56+
g, _ := strconv.ParseUint(t[1], 10, 8)
57+
b, _ := strconv.ParseUint(t[2], 10, 8)
58+
a, _ := strconv.ParseUint(t[3], 10, 8)
59+
60+
col.R = uint8(r)
61+
col.G = uint8(g)
62+
col.B = uint8(b)
63+
col.A = uint8(a)
64+
}
65+
} else if strings.HasPrefix(out, "rgb(") {
66+
for _, s := range []string{"rgb", "(", ")"} {
67+
out = strings.Replace(out, s, "", -1)
68+
}
69+
t := strings.Split(out, ",")
70+
if len(t) == 3 {
71+
r, _ := strconv.ParseUint(t[0], 10, 8)
72+
g, _ := strconv.ParseUint(t[1], 10, 8)
73+
b, _ := strconv.ParseUint(t[2], 10, 8)
74+
75+
col.R = uint8(r)
76+
col.G = uint8(g)
77+
col.B = uint8(b)
78+
}
79+
}
80+
81+
return col
82+
}

color_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dlgs
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestColor(t *testing.T) {
8+
col, ret, err := Color("Pick color", "#BEBEBE")
9+
if err != nil {
10+
t.Error(err)
11+
}
12+
13+
if verboseTests {
14+
if col != nil {
15+
r, g, b, a := col.RGBA()
16+
println("r:", r, "g:", g, "b:", b, "a:", a)
17+
}
18+
println("ret:", ret)
19+
}
20+
}

color_unsupported.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// +build !linux,!windows,!darwin,!js
2+
3+
package dlgs
4+
5+
import (
6+
"image/color"
7+
)
8+
9+
// Color displays a color selection dialog, returning the selected color and a bool for success.
10+
func Color(title, defaultColorHex string) (color.Color, bool, error) {
11+
return nil, false, ErrUnsupported
12+
}

color_windows.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// +build windows,!linux,!darwin,!js
2+
3+
package dlgs
4+
5+
import (
6+
"fmt"
7+
"image/color"
8+
"unsafe"
9+
)
10+
11+
// Color displays a color selection dialog, returning the selected color and a bool for success.
12+
func Color(title, defaultColorHex string) (color.Color, bool, error) {
13+
c, ok := colorDialog(defaultColorHex)
14+
if ok {
15+
col := color.RGBA{}
16+
col.R = byte(c & 0xff)
17+
col.G = byte((c >> 8) & 0xff)
18+
col.B = byte((c >> 16) & 0xff)
19+
return col, true, nil
20+
}
21+
22+
return nil, false, nil
23+
}
24+
25+
// colorDialog displays color dialog.
26+
func colorDialog(defaultColorHex string) (uint32, bool) {
27+
var cc choosecolorW
28+
custom := make([]uint32, 16)
29+
30+
cc.lpCustColors = &custom[0]
31+
cc.lStructSize = uint32(unsafe.Sizeof(cc))
32+
cc.flags = ccFullOpen | ccRgbInit
33+
34+
var r, g, b uint8
35+
fmt.Sscanf(defaultColorHex, "#%02x%02x%02x", &r, &g, &b)
36+
cc.rgbResult = uint32(uint32(r) | uint32(g)<<8 | uint32(b)<<16)
37+
38+
ok := chooseColor(&cc)
39+
if ok {
40+
return cc.rgbResult, true
41+
}
42+
43+
return 0, false
44+
}

date_darwin.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// +build darwin,!linux,!windows,!js
2+
3+
package dlgs
4+
5+
import (
6+
"os/exec"
7+
"strconv"
8+
"strings"
9+
"syscall"
10+
"time"
11+
)
12+
13+
// Date displays a calendar dialog, returning the date and a bool for success.
14+
func Date(title, text string, defaultDate time.Time) (time.Time, bool, error) {
15+
osa, err := exec.LookPath("osascript")
16+
if err != nil {
17+
return time.Now(), false, err
18+
}
19+
20+
o, err := exec.Command(osa, "-e", `set defaultDate to do shell script "date -j -r `+strconv.Itoa(int(defaultDate.Unix()))+` +%m/%d/%Y"`,
21+
"-e", `set T to text returned of (display dialog "`+text+` (mm/dd/yyyy)" with title "`+title+`" default answer defaultDate)`).Output()
22+
if err != nil {
23+
if exitError, ok := err.(*exec.ExitError); ok {
24+
ws := exitError.Sys().(syscall.WaitStatus)
25+
return time.Now(), ws.ExitStatus() == 0, nil
26+
}
27+
}
28+
29+
ret := true
30+
out := strings.TrimSpace(string(o))
31+
if out == "" {
32+
ret = false
33+
}
34+
35+
tim, err := time.Parse("01/02/2006", out)
36+
if err != nil {
37+
return time.Now(), false, err
38+
}
39+
40+
return tim, ret, err
41+
}

date_js.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// +build js
2+
3+
package dlgs
4+
5+
import (
6+
//"github.com/gopherjs/gopherjs/js"
7+
)
8+
9+
// Date displays a calendar dialog, returning the date and a bool for success.
10+
func Date(title, text string, defaultDate time.Time) (time.Time, bool, error) {
11+
return nil, false, ErrNotImplemented
12+
}

0 commit comments

Comments
 (0)