Skip to content

Commit

Permalink
fix(gamification): properly resize avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Jan 6, 2025
1 parent fd5f129 commit 3a8156b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions tui/view/event/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion tui/view/gamification/gamification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
22 changes: 17 additions & 5 deletions tui/view/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ")
}

Expand Down

0 comments on commit 3a8156b

Please sign in to comment.