/
main.go
363 lines (322 loc) · 8.72 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package main
import (
"bytes"
"fmt"
"gioui.org/app"
"gioui.org/font/gofont"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"gioui.org/x/explorer"
"github.com/nmaupu/chesscom_exporter/pkg/api/chesscom"
"github.com/nmaupu/chesscom_exporter/pkg/model"
mywidget "github.com/nmaupu/chesscom_exporter/pkg/ui/widget"
"golang.design/x/clipboard"
"image/color"
"io"
"io/ioutil"
"log"
"os"
"strings"
)
var (
AppName = "Chesscom Exporter"
AppVersion = "master"
BuildDate = ""
)
type (
C = layout.Context
D = layout.Dimensions
)
var (
theme = material.NewTheme(gofont.Collection())
usernameLineEditor = &widget.Editor{
SingleLine: true,
Submit: true,
}
usernameSubmitBtn = new(widget.Clickable)
archivesLoading bool
archiveListWidget = mywidget.NewArchiveList(theme)
archivesBorder = &widget.Border{
Color: color.NRGBA{A: 0xff},
CornerRadius: unit.Dp(8),
Width: unit.Px(2),
}
saveToFileBtn = new(widget.Clickable)
saveToClipboardBtn = new(widget.Clickable)
saveCancelBtn = new(widget.Clickable)
saveInProgress bool
saveStatus string
saveProgress float32
saveProgressCancelChan = make(chan bool, 1)
saveProgressChan = make(chan float32)
)
func main() {
go func() {
w := app.NewWindow(
app.Title(fmt.Sprintf("%s - %s (%s)", AppName, AppVersion, BuildDate)),
app.Size(unit.Dp(800), unit.Dp(600)),
app.MinSize(unit.Dp(400), unit.Dp(400)),
)
if err := draw(w); err != nil {
log.Fatal(err)
}
os.Exit(1)
}()
app.Main()
}
func draw(w *app.Window) error {
var ops op.Ops
// Focusing player's text edit by default
usernameLineEditor.Focus()
for {
select {
case e := <-w.Events():
switch e := e.(type) {
case system.DestroyEvent:
return e.Err
case system.FrameEvent: // repaint
gtx := layout.NewContext(&ops, e)
if usernameSubmitBtn.Clicked() {
go func() {
archivesLoading = true
defer func() {
archivesLoading = false
}()
archiveListWidget.ResetList()
var err error
username := strings.Trim(usernameLineEditor.Text(), " ")
if username == "" {
return
}
archives, err := chesscom.GetAllPlayerArchives(username)
if err != nil {
log.Printf("unable to get archives for %s, err=%v", username, err)
return
}
archiveListWidget.AddRows(archives)
w.Invalidate()
}()
}
if saveToClipboardBtn.Clicked() {
go func() { // Go routine to get all checked archives
loadAndSaveSelectedArchivesInMemory(func(r io.Reader) error {
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
clipboard.Write(clipboard.FmtText, data)
return nil
})
}()
}
if saveToFileBtn.Clicked() {
username := strings.Trim(usernameLineEditor.Text(), " ")
fileWriter, err := explorer.WriteFile(fmt.Sprintf("chesscom-export-%s.pgn", username))
if err != nil {
saveStatus = "Not supported, sorry :/"
} else {
go func() {
loadAndSaveSelectedArchivesInMemory(func(r io.Reader) error {
defer fileWriter.Close()
_, err := io.Copy(fileWriter, r)
return err
})
}()
}
}
if saveCancelBtn.Clicked() {
if saveInProgress { // button is normally disabled when not in progress though
saveProgressCancelChan <- true
}
}
kitchen(gtx, theme)
e.Frame(gtx.Ops)
}
case p := <-saveProgressChan:
if p <= 1 {
saveProgress = p
log.Printf("Progress=%f", saveProgress)
w.Invalidate()
}
}
}
}
func kitchen(gtx C, th *material.Theme) D {
usernameEditWidget := func(gtx C) D {
return layout.Flex{
Axis: layout.Horizontal,
WeightSum: 100,
Alignment: layout.Middle,
}.Layout(gtx,
layout.Flexed(78, func(gtx C) D {
return usernameEditorLayout(gtx, th)
}),
layout.Flexed(22, func(gtx C) D {
margins := layout.Inset{
Left: unit.Dp(5),
}
if archivesLoading {
gtx = gtx.Disabled()
}
btn := material.Button(th, usernameSubmitBtn, "Get archives")
return margins.Layout(gtx, btn.Layout)
}),
layout.Rigid(func(gtx C) D {
if archivesLoading {
return material.Loader(th).Layout(gtx)
}
return D{}
}),
)
}
archivesListWidget := func(gtx C) D {
return archivesBorder.Layout(gtx, func(gtx C) D {
insets := layout.UniformInset(unit.Dp(10))
return insets.Layout(gtx, func(gtx C) D {
if archiveListWidget.IsNil() || archiveListWidget.Size() == 0 {
txt := "Enter a player's name to display archives"
if !archiveListWidget.IsNil() {
txt = fmt.Sprintf("No archives available for the selected user")
}
if archivesLoading {
txt = "Loading archives..."
}
lbl := material.Label(th, unit.Dp(16), "")
lbl.Text = txt
lbl.Alignment = text.Middle
lbl.MaxLines = 1
lbl.Font.Style = text.Italic
return lbl.Layout(gtx)
}
return archiveListWidget.Layout(gtx)
})
})
}
saveWidget := func(gtx C) D {
return layout.Flex{
Alignment: layout.Middle,
Axis: layout.Vertical,
}.Layout(gtx,
layout.Rigid(material.ProgressBar(th, saveProgress).Layout),
layout.Rigid(layout.Spacer{Height: unit.Dp(2)}.Layout),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Flex{
Alignment: layout.Middle,
Axis: layout.Horizontal,
Spacing: layout.SpaceStart,
}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
if saveStatus != "" {
lbl := material.Label(th, unit.Dp(16), saveStatus)
lbl.Font.Style = text.Italic
return lbl.Layout(gtx)
}
return layout.Dimensions{}
}),
layout.Rigid(layout.Spacer{Width: unit.Dp(5)}.Layout),
layout.Rigid(func(gtx C) D {
if saveInProgress || !archiveListWidget.AtLeastOneSelected() {
gtx = gtx.Disabled()
}
return material.Button(th, saveToFileBtn, "Export to a file").Layout(gtx)
}),
layout.Rigid(layout.Spacer{Width: unit.Dp(5)}.Layout),
layout.Rigid(func(gtx C) D {
if saveInProgress || !archiveListWidget.AtLeastOneSelected() {
gtx = gtx.Disabled()
}
return material.Button(th, saveToClipboardBtn, "Export to clipboard").Layout(gtx)
}),
layout.Rigid(layout.Spacer{Width: unit.Dp(5)}.Layout),
layout.Rigid(func(gtx C) D {
if !saveInProgress {
gtx = gtx.Disabled()
}
return material.Button(th, saveCancelBtn, "Cancel").Layout(gtx)
}),
)
}),
)
}
outerInset := layout.UniformInset(unit.Dp(5))
return outerInset.Layout(gtx,
func(gtx C) D {
return layout.Flex{
Axis: layout.Vertical,
}.Layout(gtx,
layout.Rigid(usernameEditWidget),
layout.Rigid(layout.Spacer{Height: unit.Dp(5)}.Layout),
layout.Flexed(1, archivesListWidget),
layout.Rigid(layout.Spacer{Height: unit.Dp(5)}.Layout),
layout.Rigid(saveWidget),
)
})
}
func usernameEditorLayout(gtx C, th *material.Theme) D {
e := material.Editor(th, usernameLineEditor, "Enter player's name")
e.Font.Style = text.Italic
border := widget.Border{Color: color.NRGBA{A: 0xff}, CornerRadius: unit.Dp(8), Width: unit.Px(2)}
return border.Layout(gtx, func(gtx C) D {
return layout.UniformInset(unit.Dp(8)).Layout(gtx, e.Layout)
})
}
func loadAndSaveSelectedArchivesInMemory(saver func(o io.Reader) error) {
saveInProgress = true
defer func() {
saveInProgress = false
}()
saveStatus = "In progress"
saveProgressChan <- 0 // resetting progress
selectedArchives := archiveListWidget.GetSelectedArchives()
total := len(selectedArchives.Archives)
ch := make(chan struct {
idx int
archive model.ChesscomArchive
}, total)
// Pushing jobs in channel
for i, archive := range selectedArchives.Archives {
ch <- struct {
idx int
archive model.ChesscomArchive
}{idx: i, archive: archive}
}
close(ch)
buf := bytes.Buffer{}
loop:
for {
select {
case <-saveProgressCancelChan:
// Resetting progress
saveProgressChan <- 0
saveStatus = "Aborted."
return
case e, ok := <-ch:
if !ok { // no more jobs in channel
break loop
}
res, err := chesscom.GetPlayerMonthlyArchivesByURL(e.archive.GetURL())
if err != nil {
log.Printf("an error occurred trying to get archive %s", e.archive.GetURL())
continue
}
// Writing pgn games to the buffer
for _, game := range res.Games {
buf.Write([]byte(game.PGN + "\n"))
}
// update progress
saveProgressChan <- float32(e.idx) / float32(total)
}
}
if err := saver(&buf); err != nil {
saveStatus = fmt.Sprintf("Error: %v", err)
saveProgressChan <- 0 // resetting progress
return
}
saveProgressChan <- 1
saveStatus = "Success !"
}