-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
73 lines (60 loc) · 1.43 KB
/
main.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
type menuItem struct {
label string
command string
}
func runCommand(item menuItem) func() {
return func() {
parts := strings.Fields(item.command)
cmd := exec.Command(parts[0], parts[1:]...)
cmd.Stdout = os.Stdout
err := cmd.Start()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
}
}
}
func main() {
// if the number of args isn't divisible by 2, we done goofed
if (len(os.Args)-1)%2 != 0 {
fmt.Fprintln(os.Stderr, "error: Invalid number of arguments")
os.Exit(1)
}
// make menu items from the args
var menuItems []menuItem
for i := 1; i < len(os.Args); i += 2 {
label := os.Args[i]
command := os.Args[i+1]
menuItems = append(menuItems, menuItem{label, command})
}
// hello andy :)
a := app.New()
w := a.NewWindow("8menu")
// add the buttons and their corresponding functions
buttons := make([]fyne.CanvasObject, len(menuItems)+1)
for i, item := range menuItems {
buttons[i] = widget.NewButton(item.label, runCommand(item))
}
// add a close button
exitButton := widget.NewButton("Exit", func() {
w.Close()
})
buttons[len(menuItems)] = exitButton
// make the layout
content := container.NewVBox(buttons...)
w.SetContent(content)
// set min width
minHeight := 1
w.Resize(fyne.NewSize(150, float32(minHeight)))
w.ShowAndRun()
}