summaryrefslogtreecommitdiffhomepage
path: root/internal/tui/image
diff options
context:
space:
mode:
authorphantomreactor <[email protected]>2025-05-03 01:53:58 +0530
committeradamdottv <[email protected]>2025-05-02 15:29:46 -0500
commitff0ef3bb432f1cedb6e5b8a0168bfa7c9e9e15f0 (patch)
treee027f8eee09fafe33b98c6316d84b0f5e6a8edc0 /internal/tui/image
parent0095832be3b6c9ae9c45dfed70ecd22302e08dc9 (diff)
downloadopencode-ff0ef3bb432f1cedb6e5b8a0168bfa7c9e9e15f0.tar.gz
opencode-ff0ef3bb432f1cedb6e5b8a0168bfa7c9e9e15f0.zip
feat: add support for images
Diffstat (limited to 'internal/tui/image')
-rw-r--r--internal/tui/image/images.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/internal/tui/image/images.go b/internal/tui/image/images.go
new file mode 100644
index 000000000..d10a169fd
--- /dev/null
+++ b/internal/tui/image/images.go
@@ -0,0 +1,72 @@
+package image
+
+import (
+ "fmt"
+ "image"
+ "os"
+ "strings"
+
+ "github.com/charmbracelet/lipgloss"
+ "github.com/disintegration/imaging"
+ "github.com/lucasb-eyer/go-colorful"
+)
+
+func ValidateFileSize(filePath string, sizeLimit int64) (bool, error) {
+ fileInfo, err := os.Stat(filePath)
+ if err != nil {
+ return false, fmt.Errorf("error getting file info: %w", err)
+ }
+
+ if fileInfo.Size() > sizeLimit {
+ return true, nil
+ }
+
+ return false, nil
+}
+
+func ToString(width int, img image.Image) string {
+ img = imaging.Resize(img, width, 0, imaging.Lanczos)
+ b := img.Bounds()
+ imageWidth := b.Max.X
+ h := b.Max.Y
+ str := strings.Builder{}
+
+ for heightCounter := 0; heightCounter < h; heightCounter += 2 {
+ for x := range imageWidth {
+ c1, _ := colorful.MakeColor(img.At(x, heightCounter))
+ color1 := lipgloss.Color(c1.Hex())
+
+ var color2 lipgloss.Color
+ if heightCounter+1 < h {
+ c2, _ := colorful.MakeColor(img.At(x, heightCounter+1))
+ color2 = lipgloss.Color(c2.Hex())
+ } else {
+ color2 = color1
+ }
+
+ str.WriteString(lipgloss.NewStyle().Foreground(color1).
+ Background(color2).Render("▀"))
+ }
+
+ str.WriteString("\n")
+ }
+
+ return str.String()
+}
+
+func ImagePreview(width int, filename string) (string, error) {
+ imageContent, err := os.Open(filename)
+ if err != nil {
+ return "", err
+ }
+ defer imageContent.Close()
+
+ img, _, err := image.Decode(imageContent)
+ if err != nil {
+ return "", err
+ }
+
+ imageString := ToString(width, img)
+
+ return imageString, nil
+}