From 3a8156b892701b229cb310f60dedf70dbcaec237 Mon Sep 17 00:00:00 2001 From: Topvennie Date: Mon, 6 Jan 2025 22:47:12 +0100 Subject: [PATCH] fix(gamification): properly resize avatar --- tui/view/event/view.go | 4 ++-- tui/view/gamification/gamification.go | 2 +- tui/view/util.go | 22 +++++++++++++++++----- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/tui/view/event/view.go b/tui/view/event/view.go index 94e3816..c6edcb9 100644 --- a/tui/view/event/view.go +++ b/tui/view/event/view.go @@ -14,7 +14,7 @@ func (m *Model) viewToday() string { if m.today.Poster != nil { i, _, err := image.Decode(bytes.NewReader(m.today.Poster)) if err == nil { - poster = view.ImagetoString(wTodayPoster, i) + poster = view.ImageToString(i, wTodayPoster, 0) } } @@ -42,7 +42,7 @@ func (m *Model) viewOverview() string { if len(m.upcoming) > 0 && m.upcoming[0].Poster != nil { i, _, err := image.Decode(bytes.NewReader(m.upcoming[0].Poster)) if err == nil { - poster = view.ImagetoString(wOvPoster, i) + poster = view.ImageToString(i, wOvPoster, 0) } } diff --git a/tui/view/gamification/gamification.go b/tui/view/gamification/gamification.go index c19afb0..1ef9d78 100644 --- a/tui/view/gamification/gamification.go +++ b/tui/view/gamification/gamification.go @@ -85,7 +85,7 @@ func (m *Model) View() string { sScore.Render(strconv.Itoa(int(item.item.Score))), ) - column := lipgloss.JoinVertical(lipgloss.Left, view.ImagetoString(wAvatar, item.image), user) + column := lipgloss.JoinVertical(lipgloss.Left, view.ImageToString(item.image, wAvatar, sAll.GetHeight()-lipgloss.Height(user)), user) columns = append(columns, sColumn.Render(column)) } diff --git a/tui/view/util.go b/tui/view/util.go index d3fb57e..3b78a04 100644 --- a/tui/view/util.go +++ b/tui/view/util.go @@ -9,17 +9,29 @@ import ( "github.com/lucasb-eyer/go-colorful" ) -// ImagetoString converts an image to a -// The height gets resized according to the aspect ratio -func ImagetoString(width int, img image.Image) string { - img = imaging.Resize(img, width, 0, imaging.Lanczos) +// ImageToString converts an image to a string +// If either widht or height is 0 then the aspect ratio is kept +func ImageToString(img image.Image, width, height int) string { + if width == 0 || height == 0 { + return imageToString(imaging.Resize(img, width, height, imaging.Lanczos)) + } + + imgW := imaging.Resize(img, width, 0, imaging.Lanczos) + if imgW.Bounds().Dy() <= height { + return imageToString(imgW) + } + + return imageToString(imaging.Resize(img, 0, height, imaging.Lanczos)) +} + +func imageToString(img image.Image) string { b := img.Bounds() imageWidth := b.Max.X h := b.Max.Y str := strings.Builder{} for heightCounter := 0; heightCounter < h; heightCounter += 2 { - for x := imageWidth; x < width; x += 2 { + for x := imageWidth; x < img.Bounds().Dx(); x += 2 { str.WriteString(" ") }