Skip to content

Instantly share code, notes, and snippets.

@Diti Diti/main.go
Created Jan 4, 2018

Embed
What would you like to do?
Losowowybieracz 2018
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"math/rand"
"os"
"strings"
"time"
"github.com/andlabs/ui"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func process(ioReader io.Reader) (output []string) {
scanner := bufio.NewScanner(ioReader)
scanGroups := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if i := bytes.IndexByte(data, ';'); i >= 0 {
return i + 1, data[0:i], nil
}
if atEOF {
return len(data), data[:len(data)], io.EOF
}
return 0, nil, nil
}
scanner.Split(scanGroups) // Split semicolon-separated string into parts
for scanner.Scan() {
parts := strings.Split(scanner.Text(), ",") // Split colon-separated string into parts
randPart := parts[rand.Intn(len(parts))] // Pick a random part
output = append(output, strings.Trim(randPart, " ")) // Trim it and append it to the output slice
}
return
}
func runGui() {
// Create handles to window and its components
window := ui.NewWindow("Losowowybieracz 2018", 600, 300, false)
btn_file := ui.NewButton("Browse...")
// Create a vertical "box" and place components inside
box := ui.NewVerticalBox()
box.Append(ui.NewLabel("Select a file:"), false)
box.Append(btn_file, false)
box.Append(ui.NewLabel("Wybrane słowa:"), false)
// Events
btn_file.OnClicked(func(*ui.Button) {
pathStr := ui.OpenFile(window) // File picker
reader, err := os.Open(pathStr)
defer reader.Close()
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
os.Exit(1) // Graceless shutdown, RIP
}
processedEntries := process(reader)
for _, entry := range processedEntries {
box.Append(ui.NewLabel(entry), false)
}
})
window.OnClosing(func(*ui.Window) bool {
ui.Quit()
return true
})
window.SetMargined(true)
window.SetChild(box)
window.Show()
}
func main() {
err := ui.Main(runGui)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.