diff --git a/commands.go b/commands.go index 571c6015..82f2edab 100644 --- a/commands.go +++ b/commands.go @@ -867,7 +867,7 @@ func neofetchCMD(_ string, u *User) { {"Uptime", uptimeStr}, {"Packages", fmt.Sprint(len(PluginCMDs)+len(MainCMDs)+len(RestCMDs)) + " commands"}, {"Shell", "devzat"}, - {"Memory", fmt.Sprintf("%v MiB alloc / %v MiB total, %v GC cycles", memstats.Alloc/1024/1024, memstats.TotalAlloc/1024/1024, memstats.NumGC)}, + {"Memory", fmt.Sprintf("%v MiB alloc / %v MiB sys, %v GC cycles", memstats.Alloc/1024/1024, memstats.Sys/1024/1024, memstats.NumGC)}, {"", ""}, {"", colorSwatch1}, {"", colorSwatch2}, diff --git a/util.go b/util.go index 462c3ce6..1d84d60d 100644 --- a/util.go +++ b/util.go @@ -9,13 +9,14 @@ import ( "encoding/json" "encoding/pem" "fmt" + "github.com/disintegration/imaging" "image" - stdcolor "image/color" "io" "math" "math/rand" "net/http" "os" + "runtime" "runtime/debug" "strings" "text/tabwriter" @@ -25,7 +26,7 @@ import ( "github.com/caarlos0/sshmarshal" "github.com/charmbracelet/glamour" "github.com/charmbracelet/glamour/ansi" - "github.com/eliukblau/pixterm/pkg/ansimage" + //"github.com/eliukblau/pixterm/pkg/ansimage" "github.com/fatih/color" "github.com/gliderlabs/ssh" //markdown "github.com/quackduck/go-term-markdown" @@ -185,11 +186,12 @@ func replaceImgs(md string, width int, cache map[string]image.Image) string { imgText = strings.ReplaceAll(strings.ReplaceAll(strings.TrimSpace(imgText), "\n", ""), " ", "") if img, ok := cache[imgText]; ok { - i, err := ansimage.NewScaledFromImage(img, math.MaxInt32, width/2, stdcolor.Transparent, ansimage.ScaleModeFit, ansimage.NoDithering) - if err != nil { - return replaceImgs(md[:start]+imgText+" (error rendering)"+md[end+6:], width, cache) - } - imgText = i.Render() + //i, err := ansimage.NewScaledFromImage(img, math.MaxInt32, width/2, stdcolor.Transparent, ansimage.ScaleModeFit, ansimage.NoDithering) + //if err != nil { + // return replaceImgs(md[:start]+imgText+" (error rendering)"+md[end+6:], width, cache) + //} + //imgText = i.Render() + imgText = imgRender(img, width/2) return replaceImgs(md[:start]+imgText+md[end+6:], width, cache) } @@ -204,28 +206,58 @@ func replaceImgs(md string, width int, cache map[string]image.Image) string { limitReader := io.LimitReader(res.Body, 30*1024*1024) // 30 megabyte limit // https://github.com/golang/go/issues/12512#issuecomment-137981217 header := new(bytes.Buffer) + //memStats("before image.DecodeConfig") config, _, err := image.DecodeConfig(io.TeeReader(limitReader, header)) if err != nil || config.Width > 4032*2 || config.Height > 3024*2 { return replaceImgs(md[:start]+imgText+" (invalid or too large to render)"+md[end+6:], width, cache) } + //fmt.Println(config) + //memStats("before image.Decode") //buf := new(bytes.Buffer) //img, _, err := image.Decode(io.TeeReader(io.MultiReader(header, limitReader), buf)) img, _, err := image.Decode(io.MultiReader(header, limitReader)) if err != nil { return replaceImgs(md[:start]+imgText+" (error decoding image)"+md[end+6:], width, cache) } - i, err := ansimage.NewScaledFromImage(img, math.MaxInt32, width/2, stdcolor.Transparent, ansimage.ScaleModeFit, ansimage.NoDithering) - if err != nil { - return replaceImgs(md[:start]+imgText+" (error rendering)"+md[end+6:], width, cache) - } + //memStats("after image.Decode") + //i, err := ansimage.NewScaledFromImage(img, math.MaxInt32, width/2, stdcolor.Transparent, ansimage.ScaleModeFit, ansimage.NoDithering) + //if err != nil { + // return replaceImgs(md[:start]+imgText+" (error rendering)"+md[end+6:], width, cache) + //} if cache != nil { cache[imgText] = img } - imgText = i.Render() + //imgText = i.Render() + imgText = imgRender(img, width/2) return replaceImgs(md[:start]+imgText+md[end+6:], width, cache) } +func memStats(note string) { + var m runtime.MemStats + runtime.ReadMemStats(&m) + fmt.Printf("Alloc = %v MiB %s\n", m.Alloc/1024/1024, note) +} + +func imgRender(img image.Image, width int) string { + result := "" + //fmt.Println("starting") + //memStats("before fit") + img = imaging.Fit(img, width, math.MaxInt32, imaging.Lanczos) + //memStats("after fit") + //fmt.Println("done fitting") + for y := 0; y < img.Bounds().Dy(); y += 2 { + for x := 0; x < img.Bounds().Dx(); x++ { + r1, g1, b1, _ := img.At(x, y).RGBA() + r2, g2, b2, _ := img.At(x, y+1).RGBA() + result += fmt.Sprintf("\x1b[38;2;%d;%d;%dm\x1b[48;2;%d;%d;%dmâ–€", r1/256, g1/256, b1/256, r2/256, g2/256, b2/256) + } + result += "\x1b[0m\n" + } + //fmt.Println("done render") + return result +} + func addLeftPad(a string, pad int) string { split := strings.Split(a, "\n") for i := 1; i < len(split); i++ { // skip first line