From e2a948c6fa1be9b61427f95c26cce2e79726120a Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Fri, 7 Apr 2023 11:24:44 -0600 Subject: [PATCH] configurable image resolution by user --- image.go | 4 ++-- image_test.go | 8 ++++---- main.go | 9 +++++---- testdata/tofugu.yaml | 1 + yamldb.go | 13 +++++++++++-- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/image.go b/image.go index ed86c78..229aaca 100644 --- a/image.go +++ b/image.go @@ -13,7 +13,7 @@ import ( "github.com/nfnt/resize" ) -func View(w io.Writer, p string) { +func ViewAt(w io.Writer, p string, resolution int) { in, err := os.Open(p) if err != nil { panic(err) @@ -32,7 +32,7 @@ func View(w io.Writer, p string) { if err != nil { panic(err) } - printImage(w, 40, src) + printImage(w, resolution, src) fmt.Fprintln(w, "") } diff --git a/image_test.go b/image_test.go index 1a77810..028e537 100644 --- a/image_test.go +++ b/image_test.go @@ -9,8 +9,8 @@ func TestView(t *testing.T) { if os.Getenv("INTEGRATION") == "" { t.SkipNow() } - View(os.Stderr, "./testdata/tofugu.d/a-hiragana-0.png") - View(os.Stderr, "./testdata/tofugu.d/a-hiragana-0.sm.png") - View(os.Stderr, "./testdata/tofugu.d/a-hiragana-1.png") - View(os.Stderr, "./testdata/tofugu.d/a-hiragana-2.png") + ViewAt(os.Stderr, "./testdata/tofugu.d/a-hiragana-0.png", 20) + ViewAt(os.Stderr, "./testdata/tofugu.d/a-hiragana-0.sm.png", 20) + ViewAt(os.Stderr, "./testdata/tofugu.d/a-hiragana-1.png", 20) + ViewAt(os.Stderr, "./testdata/tofugu.d/a-hiragana-2.png", 20) } diff --git a/main.go b/main.go index a8b1ad9..4a20ede 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ type ( LastAnswer(IDU, IDQ) (IDA, Answer) Answer(IDA) Answer PushAnswer(IDU, IDQ, Renderable, bool) error + UserResolution(IDU) int Close() } Question struct { @@ -107,7 +108,7 @@ func Review(db DB, user IDU) ([]IDQ, error) { func ReviewQ(db DB, user IDU, q IDQ) (string, string, error) { question := db.Question(q) - fmt.Printf("> Q: %s\n", question.Q) + fmt.Printf("> Q: %s\n", question.Q.ToString(db.UserResolution(user))) fmt.Printf("\n") fmt.Printf("> %+v\n", question.Tags) var response string @@ -121,7 +122,7 @@ func ReviewQ(db DB, user IDU, q IDQ) (string, string, error) { default: break } - fmt.Printf("> %s", question.Clues[i]) + fmt.Printf("> %s", question.Clues[i].ToString(db.UserResolution(user))) if i+1 < len(question.Clues) { fmt.Printf(" | /clue for another clue") } @@ -171,12 +172,12 @@ func (q Question) Tagged(tag IDT) bool { return false } -func (renderable Renderable) String() string { +func (renderable Renderable) ToString(resolution int) string { s := string(renderable) if !strings.HasPrefix(s, "img:") { return s } buff := bytes.NewBuffer(nil) - View(buff, s[4:]) + ViewAt(buff, s[4:], resolution) return string(buff.Bytes()) } diff --git a/testdata/tofugu.yaml b/testdata/tofugu.yaml index c830396..0864e03 100644 --- a/testdata/tofugu.yaml +++ b/testdata/tofugu.yaml @@ -93,6 +93,7 @@ knowledge: yu-katakana: {"q":"img:./testdata/tofugu.d/yu-katakana-0.png", "clues":["img:./testdata/tofugu.d/yu-katakana-1.png"], "tags": ["katakana"]} users: breel: + resolution: 60 tags: assignments: - hiragana diff --git a/yamldb.go b/yamldb.go index be6a510..02624cc 100644 --- a/yamldb.go +++ b/yamldb.go @@ -20,8 +20,9 @@ type ( Answers map[IDA]Answer } user struct { - Tags tags - Cadence duration + Tags tags + Cadence duration + Resolution int } tags struct { Assignments []IDT @@ -98,6 +99,14 @@ func (db yamlDB) Next(user IDU, q IDQ) time.Time { return SM2Next(last, log, db.cadence(user)) } +func (db yamlDB) UserResolution(user IDU) int { + u := db.Users[user] + if u.Resolution == 0 { + return 40 + } + return u.Resolution +} + func (db yamlDB) cadence(user IDU) time.Duration { u := db.Users[user] if u.Cadence == 0 {