/
main.go
167 lines (155 loc) · 3.47 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bufio"
"fmt"
"io"
"log"
"os/exec"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)
// あとこのへん調べたい
// Canvas
// CheckBox
// ComboBox
// Dialog
// FileDialog
// Form
// Label
// GridLayout
// LineEdit
// ListBox
// Menu
// PushButton
// RadioButton
// StatusBar
// TableView
// Must treats unwanted error.
// If error has occurred, Must will panic.
func Must(err error) {
if err != nil {
log.Panic(err)
}
}
// OutputConverter returns CP932 to UTF8 reader, and writer
func OutputConverter() (io.Reader, io.WriteCloser) {
r, w := io.Pipe()
jr := transform.NewReader(r, japanese.ShiftJIS.NewDecoder())
return jr, w
}
func main() {
var (
pingMainWindow *walk.MainWindow
pingStatus *walk.StatusBarItem
pingStdoutTE, pingStderrTE *walk.TextEdit
pingButton *walk.PushButton
pingCB *walk.ComboBox
pingIndex *int
)
{
i := 0
pingIndex = &i
}
setEnabled := func(enabled bool) {
pingButton.SetEnabled(enabled)
pingCB.SetEnabled(enabled)
}
wnd := MainWindow{
AssignTo: &pingMainWindow,
Title: "sandbox",
MinSize: Size{Width: 320, Height: 240},
Size: Size{Width: 1024, Height: 800},
Layout: VBox{},
Children: []Widget{
Composite{
Layout: Grid{Columns: 2},
Children: []Widget{
Label{
Text: "address",
Row: 0,
Column: 0,
},
ComboBox{
AssignTo: &pingCB,
Row: 0,
Column: 1,
Model: []string{
"8.8.8.8", "1.1.1.1", "127.0.0.1",
},
CurrentIndex: *pingIndex,
},
Label{
Text: "command",
Row: 1, Column: 0,
},
PushButton{
AssignTo: &pingButton,
Text: "ping",
OnClicked: func() {
setEnabled(false)
pingMainWindow.AsFormBase().ProgressIndicator().SetState(walk.PIIndeterminate)
command := fmt.Sprint("ping ", pingCB.Text())
Must(pingStatus.SetText(fmt.Sprint("executing ", command)))
Must(pingStdoutTE.SetText(""))
Must(pingStderrTE.SetText(""))
go func() {
defer setEnabled(true)
defer pingMainWindow.AsFormBase().ProgressIndicator().SetCompleted(100)
cmd := exec.Command("cmd.exe", "/c", command)
stdoutR, stdoutW := OutputConverter()
stderrR, stderrW := OutputConverter()
cmd.Stdout = stdoutW
cmd.Stderr = stderrW
err := cmd.Start()
if err != nil {
Must(pingStatus.SetText(fmt.Sprint(err)))
return
}
go func() {
s := bufio.NewScanner(stdoutR)
for s.Scan() {
pingStdoutTE.AppendText(s.Text() + "\r\n")
}
}()
go func() {
s := bufio.NewScanner(stderrR)
for s.Scan() {
pingStderrTE.AppendText(s.Text() + "\r\n")
}
}()
err = cmd.Wait()
if err != nil {
Must(pingStatus.SetText(fmt.Sprint(err)))
}
Must(pingStatus.SetText("finished"))
}()
},
Row: 1,
Column: 1,
},
},
},
TextEdit{
AssignTo: &pingStdoutTE,
ReadOnly: true,
VScroll: true,
HScroll: true,
},
TextEdit{
AssignTo: &pingStderrTE,
ReadOnly: true,
VScroll: true,
HScroll: true,
},
},
StatusBarItems: []StatusBarItem{
{
AssignTo: &pingStatus,
},
},
}
i, err := wnd.Run()
log.Println(i, err)
}