diff --git a/.gitignore b/.gitignore index 24b160f..f38bd9b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ **/*.bak **/*.sw* +/anki +/anki.d diff --git a/anki.d b/anki.d deleted file mode 100755 index f09657b..0000000 Binary files a/anki.d and /dev/null differ diff --git a/go.mod b/go.mod index 2caa5b7..3161b2a 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module gogs.inhome.blapointe.com/anki.d +module gogs.inhome.blapointe.com/bel/anki go 1.19 diff --git a/image.go b/image.go index c2baeb3..b55d1e6 100644 --- a/image.go +++ b/image.go @@ -5,6 +5,7 @@ import ( "image" "image/jpeg" "image/png" + "io" "os" "path" @@ -12,7 +13,7 @@ import ( "github.com/nfnt/resize" ) -func View(p string) { +func View(w io.Writer, p string) { in, err := os.Open(p) if err != nil { panic(err) @@ -31,10 +32,10 @@ func View(p string) { if err != nil { panic(err) } - printImage(40, src) + printImage(w, 40, src) } -func printImage(limit int, image image.Image) { +func printImage(w io.Writer, limit int, image image.Image) { if image.Bounds().Max.X > image.Bounds().Max.Y { xmax := image.Bounds().Max.X ratio := float64(limit) / float64(xmax) @@ -46,14 +47,15 @@ func printImage(limit int, image image.Image) { height := uint(float64(ymax) * ratio) image = resize.Resize(0, height, image, resize.Bicubic) } + escape := "\x1b" for i := 0; i < image.Bounds().Max.Y; i++ { for j := 0; j < image.Bounds().Max.X; j++ { r, g, b, _ := image.At(j, i).RGBA() r = ansi.To256(r) g = ansi.To256(g) b = ansi.To256(b) - ansi.Print(r, g, b) + fmt.Fprintf(w, "%s[7m%s[38;2;%d;%d;%dm ", escape, escape, r, g, b) } - fmt.Printf("\n") + fmt.Fprintf(w, "\n") } } diff --git a/image_test.go b/image_test.go index d701ba4..1a77810 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("./testdata/tofugu.d/a-hiragana-0.png") - View("./testdata/tofugu.d/a-hiragana-0.sm.png") - View("./testdata/tofugu.d/a-hiragana-1.png") - View("./testdata/tofugu.d/a-hiragana-2.png") + 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") } diff --git a/main.go b/main.go index c8808b6..37d3fb1 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "bytes" "fmt" "log" "os" @@ -62,11 +63,7 @@ func Main() error { continue } question := db.Question(q) - fmt.Printf("> Q: ") - if strings.HasPrefix(string(question.Q), "img:") { - } else { - fmt.Printf("%s", question.Q) - } + fmt.Printf("> Q: %s\n", question.Q) fmt.Printf("\n") fmt.Printf("> %+v\n", question.Tags) var response string @@ -130,3 +127,13 @@ func (q Question) Tagged(tag IDT) bool { } return false } + +func (renderable Renderable) String() string { + s := string(renderable) + if !strings.HasPrefix(s, "img:") { + return s + } + buff := bytes.NewBuffer(nil) + View(buff, s[4:]) + return string(buff.Bytes()) +} diff --git a/testdata/tofugu.yaml b/testdata/tofugu.yaml index c7a4c16..0107f86 100644 --- a/testdata/tofugu.yaml +++ b/testdata/tofugu.yaml @@ -1,10 +1,9 @@ knowledge: questions: a-hiragana: - q: img:testdata/tofugu.d/a-hiragana-0.jpg + q: img:testdata/tofugu.d/a-hiragana-0.png clues: - - img:testdata/tofugu.d/a-hiragana-1.jpg - - img:testdata/tofugu.d/a-hiragana-2.jpg + - img:testdata/tofugu.d/a-hiragana-1.png tags: [hiragana, vowel] users: breel: