1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-11-03 04:27:16 -05:00
neonmodem/system/lib/lib.go
2023-01-06 19:46:41 -05:00

37 lines
874 B
Go

package lib
import (
"fmt"
"image/color"
"regexp"
"github.com/eliukblau/pixterm/pkg/ansimage"
"github.com/mrusme/neonmodem/ui/ctx"
)
func RenderInlineImages(c *ctx.Ctx, s string, w int) string {
var re = regexp.MustCompile(`(?m)(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])\.(jpg|jpeg|png)`)
s = re.ReplaceAllStringFunc(s, func(url string) string {
c.Logger.Debugf("found string: %s\n", url)
pix, err := ansimage.NewScaledFromURL(
url,
int((float32(w) * 0.75)),
w,
color.Transparent,
ansimage.ScaleModeResize,
ansimage.NoDithering,
)
if err != nil {
c.Logger.Debugf("error: %s\n", err.Error())
return url
}
c.Logger.Debugf("returning rendered image\n")
return fmt.Sprintf("\n\n%s\nSource: %s\n\n", pix.RenderExt(false, false), url)
})
c.Logger.Debugf("returning s\n")
return s
}