first commit
This commit is contained in:
commit
4130aa1367
18
.gitignore
vendored
Normal file
18
.gitignore
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
# Own stuff
|
||||
main
|
244
bindata.go
Normal file
244
bindata.go
Normal file
File diff suppressed because one or more lines are too long
10474
litdata.json
Normal file
10474
litdata.json
Normal file
File diff suppressed because it is too large
Load Diff
68
main.go
Normal file
68
main.go
Normal file
@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
//go:generate go-bindata litdata.json
|
||||
//go:generate go fmt bindata.go
|
||||
|
||||
const (
|
||||
Purple = "\033[1;34m%s\033[0m"
|
||||
Teal = "\033[1;36m%s\033[0m"
|
||||
)
|
||||
|
||||
type Quote struct {
|
||||
Author string `json:"author"`
|
||||
Book string `json:"book"`
|
||||
Text string `json:"text"`
|
||||
Time string `json:"time"`
|
||||
}
|
||||
|
||||
func get(qs map[string][]Quote, t string) Quote {
|
||||
quote := Quote{}
|
||||
|
||||
if _, exists := qs[t]; exists {
|
||||
if len(qs[t]) != 1 {
|
||||
rand.Seed(time.Now().Unix())
|
||||
quote = qs[t][rand.Intn(len(qs[t]))]
|
||||
}
|
||||
}
|
||||
|
||||
return quote
|
||||
|
||||
}
|
||||
|
||||
func open(as string) []byte {
|
||||
data, err := Asset(as)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func (q *Quote) decorate() string {
|
||||
text := strings.Replace(q.Text, q.Time, fmt.Sprintf(Purple, q.Time), -1)
|
||||
|
||||
return fmt.Sprintf("\n%s\n\n - %s, %s\n", text, q.Book, fmt.Sprintf(Teal, q.Author))
|
||||
}
|
||||
|
||||
func main() {
|
||||
data := open("litdata.json")
|
||||
qs := make(map[string][]Quote)
|
||||
if err := json.Unmarshal(data, &qs); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
now := time.Now()
|
||||
t := fmt.Sprintf("%02d:%02d", now.Hour(), now.Minute())
|
||||
quote := get(qs, t)
|
||||
if quote != (Quote{}) {
|
||||
fmt.Println(quote.decorate())
|
||||
}
|
||||
}
|
28
main_test.go
Normal file
28
main_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDecorate(t *testing.T) {
|
||||
tables := []struct {
|
||||
q Quote
|
||||
n string
|
||||
}{
|
||||
{
|
||||
Quote{
|
||||
"Max Mustermann",
|
||||
"Testbook",
|
||||
"This is a time!",
|
||||
"time",
|
||||
},
|
||||
"\nThis is a \033[1;34mtime\033[0m!\n\n - Testbook, \033[1;36mMax Mustermann\033[0m\n",
|
||||
},
|
||||
}
|
||||
for _, table := range tables {
|
||||
r := table.q.decorate()
|
||||
if r != table.n {
|
||||
t.Errorf("string not is not \"%s\". got \"%s\".", table.n, r)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user