forked from dolfly/termux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toast.go
52 lines (41 loc) · 1.07 KB
/
toast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package termux
import "bytes"
// ToastPosition enumerates current position of the toast
type ToastPosition string
const (
// Top lifts the toast ot the top of the screen
Top ToastPosition = "top"
// Middle is the default position of the toast
Middle ToastPosition = "middle"
// Bottom puts the toast to the bottom of the screen
Bottom ToastPosition = "bottom"
)
// ToastOption represents the optional options to a toast
type ToastOption struct {
BGColor string
FontColor string
Position ToastPosition
Short bool
}
// Toast creates a toast on the device
func Toast(text string, opts ToastOption) error {
in := bytes.NewBufferString(text)
buf := bytes.NewBuffer([]byte{})
params := map[string]interface{}{}
if opts.FontColor != "" {
params["text_color"] = opts.FontColor
}
if opts.BGColor != "" {
params["background"] = opts.BGColor
}
if opts.Short {
params["short"] = true
}
if opts.Position != "" {
params["gravity"] = string(opts.Position)
}
if err := exec(in, buf, "Toast", params, ""); err != nil {
return err
}
return checkErr(buf.Bytes())
}