Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/api/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const viewerQuery = `query {
Viewer {
id
name
avatar {
large
}
statistics {
anime {
count
Expand Down
9 changes: 6 additions & 3 deletions internal/api/responses/user_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package responses
type Profile struct {
Data struct {
Viewer struct {
Name string `json:"name"`
SiteUrl string `json:"siteUrl"`
Id int `json:"id"`
Name string `json:"name"`
SiteUrl string `json:"siteUrl"`
Id int `json:"id"`
Avatar struct {
Large string `json:"large"`
} `json:"avatar"`
Statistics struct {
Anime struct {
Count int `json:"count"`
Expand Down
121 changes: 121 additions & 0 deletions internal/ui/image_renderer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package ui

import (
"bytes"
"encoding/base64"
"fmt"
"image"
_ "image/jpeg"
"image/png"
"io"
"net/http"
"os"
)

func convertToPNG(jpgData []byte) ([]byte, error) {
img, _, err := image.Decode(bytes.NewReader(jpgData))
//resized := resize.Resize(220, 300, img, resize.Lanczos3)

if err != nil {
return nil, err
}
var buf bytes.Buffer
err = png.Encode(&buf, img)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func serializeGRCommand(payload []byte, cmd map[string]string) []byte {
seq := "\033_G"
first := true
for k, v := range cmd {
if !first {
seq += ","
}
first = false
seq += fmt.Sprintf("%s=%s", k, v)
}
if payload != nil {
seq += ";"
}
result := []byte(seq)
if payload != nil {
result = append(result, payload...)
}
result = append(result, []byte("\033\\")...)
return result
}


func writeChunked(cmd map[string]string, data []byte) {
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
base64.StdEncoding.Encode(encoded, data)

chunkSize := 4096
for len(encoded) > 0 {
end := chunkSize
if end > len(encoded) {
end = len(encoded)
}
chunk := encoded[:end]
encoded = encoded[end:]

cmdCopy := make(map[string]string)
for k, v := range cmd {
cmdCopy[k] = v
}
if len(encoded) > 0 {
cmdCopy["m"] = "1"
} else {
cmdCopy["m"] = "0"
}

serialized := serializeGRCommand(chunk, cmdCopy)
os.Stdout.Write(serialized)
os.Stdout.Sync()
}
fmt.Println("")
}

type KGPParams struct {
R string
C string
}

func RenderWithImage(imageUrl string, content string, kgpParams KGPParams, numLines int) error {
resp, err := http.Get(imageUrl)
if err != nil {
panic(err)
}
defer resp.Body.Close()

imageBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}

pngData, err := convertToPNG(imageBytes)
if err != nil {
panic(err)
}

cmd := map[string]string{
"a": "T",
"f": "100",
"x": "10",
"r": kgpParams.R,
"c": kgpParams.C,
"z": "1",
}

fmt.Print("\n")
fmt.Println(content)
fmt.Printf("\033[%dA\033[2C", numLines)

writeChunked(cmd, pngData)
fmt.Print("\n")

return nil
}
12 changes: 10 additions & 2 deletions internal/ui/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ProfileUI struct {
TotalManga int
MinutesWatched int
ChaptersRead int
AvatarUrl string
SiteUrl string
}

Expand Down Expand Up @@ -59,14 +60,21 @@ func (p *ProfileUI) Render() error {
for _, kv := range dataSlice {
sb.WriteString(
fmt.Sprintf(
"%s : %s\n",
"%*s%s : %s\n",
20, "",
keyStyle.MarginRight(maxKeyLen-len(kv.Key)).Render(kv.Key),
valueStyle.Render(kv.Value),
),
)
}

// Display the output
fmt.Print(sb.String())
RenderWithImage(
p.AvatarUrl,
sb.String(),
KGPParams{
R: "7",
C: "15",
},8)
return nil
}
3 changes: 2 additions & 1 deletion internal/viewmodel/profile_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/CosmicPredator/chibi/internal/ui"
)

// get's user profile information form API and
// get's user profile information form API and
// displays it
func HandleProfile() error {
var profile *responses.Profile
Expand All @@ -32,6 +32,7 @@ func HandleProfile() error {
MinutesWatched: profile.Data.Viewer.Statistics.Anime.MinutesWatched,
ChaptersRead: profile.Data.Viewer.Statistics.Manga.ChaptersRead,
SiteUrl: profile.Data.Viewer.SiteUrl,
AvatarUrl: profile.Data.Viewer.Avatar.Large,
}

// display profile UI
Expand Down