users can have tags to load all unanswered question for tags

master
bel 2023-04-04 20:06:37 -06:00
parent 8347b6053e
commit 1f1154d6af
4 changed files with 56 additions and 17 deletions

BIN
anki.d

Binary file not shown.

27
main.go
View File

@ -50,13 +50,17 @@ func Main() error {
return err return err
} }
user := os.Getenv("USER") user := os.Getenv("USER")
for q, history := range db.HistoryOf(user) { for q, _ := range db.HistoryOf(user) {
log.Printf("%s/%s/%+v", user, q, history)
if time.Until(db.Next(user, q)) > 0 { if time.Until(db.Next(user, q)) > 0 {
continue continue
} }
question := db.Question(q) question := db.Question(q)
fmt.Printf("> Q: %s\n", question.Q) fmt.Printf("> Q: ")
if strings.HasPrefix(question.Q, "img:") {
} else {
fmt.Printf("%s", question.Q)
}
fmt.Printf("\n")
fmt.Printf("> %+v\n", question.Tags) fmt.Printf("> %+v\n", question.Tags)
var response string var response string
for i := range question.Clues { for i := range question.Clues {
@ -108,11 +112,14 @@ func readline() string {
} }
func NewDB() (DB, error) { func NewDB() (DB, error) {
var db yamlDB return newYamlDB(os.Getenv("DB"))
if b, err := os.ReadFile(os.Getenv("DB")); err != nil { }
return nil, err
} else if err := yaml.Unmarshal(b, &db); err != nil { func (q Question) Tagged(tag string) bool {
return nil, err for i := range q.Tags {
} if q.Tags[i] == tag {
return db, nil return true
}
}
return false
} }

13
testdata/tofugu.yaml vendored
View File

@ -1,14 +1,15 @@
knowledge: knowledge:
questions: questions:
a: a-hiragana:
q: whats a fieldset q: img:testdata/a-hiragana-0.jpg
clues: clues:
- clue1 of 2 - img:testdata/a-hiragana-1.jpg
tags: tags: [hiragana, vowel]
- hiragana
- vowel
users: users:
breel: breel:
tags:
- hiragana
- katakana
history: {} history: {}
cadence: cadence:
- 1h - 1h

View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"os"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"gopkg.in/yaml.v2"
) )
type ( type (
@ -17,12 +19,41 @@ type (
Answers map[string]Answer Answers map[string]Answer
} }
user struct { user struct {
Tags []string
History map[string][]History History map[string][]History
} }
) )
func newYamlDB(p string) (yamlDB, error) {
db := yamlDB{
Knowledge: knowledge{
Questions: map[string]Question{},
Answers: map[string]Answer{},
},
Users: map[string]user{},
Cadence: []duration{},
}
if b, err := os.ReadFile(p); err != nil {
return yamlDB{}, err
} else if err := yaml.Unmarshal(b, &db); err != nil {
return yamlDB{}, err
}
return db, nil
}
func (db yamlDB) HistoryOf(user string) map[string][]History { func (db yamlDB) HistoryOf(user string) map[string][]History {
return db.Users[user].History result := map[string][]History{}
for k, v := range db.Users[user].History {
result[k] = append([]History{}, v...)
}
for _, tag := range db.Users[user].Tags {
for qid, q := range db.Knowledge.Questions {
if _, ok := result[qid]; !ok && q.Tagged(tag) {
result[qid] = []History{}
}
}
}
return result
} }
func (db yamlDB) Next(user, q string) time.Time { func (db yamlDB) Next(user, q string) time.Time {