11package cmd
22
33import (
4+ "bytes"
45 "fmt"
6+ "image"
57 "image/color"
8+ "net/http"
69 "os"
710 "regexp"
811 "strconv"
912
13+ _ "image/gif"
14+ _ "image/jpeg"
15+ _ "image/png"
16+
1017 "github.com/charmbracelet/glamour"
1118 "github.com/eliukblau/pixterm/pkg/ansimage"
19+ "github.com/mattn/go-sixel"
1220
1321 md "github.com/JohannesKaufmann/html-to-markdown"
1422 "github.com/spf13/cobra"
@@ -21,6 +29,7 @@ import (
2129var verbose bool
2230var noImages bool
2331var noPretty bool
32+ var sixelEncoder bool
2433
2534type InlineImage struct {
2635 URL string
@@ -113,6 +122,30 @@ func RenderMarkdown(title, markdown string, images []InlineImage) (string, error
113122 imgTitle := images [imgIndex ].Title
114123 imgURL := images [imgIndex ].URL
115124
125+ if sixelEncoder == true {
126+ res , err := http .Get (imgURL )
127+ if err != nil {
128+ return md
129+ }
130+
131+ defer res .Body .Close ()
132+
133+ im , _ , err := image .Decode (res .Body )
134+ if err != nil {
135+ return md
136+ }
137+
138+ var b bytes.Buffer
139+ enc := sixel .NewEncoder (& b )
140+ enc .Dither = true
141+ err = enc .Encode (im )
142+ if err != nil {
143+ return md
144+ }
145+
146+ return fmt .Sprintf ("\n %s\n %s" , string (b .Bytes ()), imgTitle )
147+ }
148+
116149 pix , err := ansimage .NewScaledFromURL (
117150 imgURL ,
118151 int ((float64 (width ) * 0.75 )),
@@ -181,11 +214,11 @@ var rootCmd = &cobra.Command{
181214
182215func Execute () {
183216 rootCmd .Flags ().BoolVarP (
184- & noImages ,
185- "no-images " ,
186- "i " ,
217+ & sixelEncoder ,
218+ "sixel-encoder " ,
219+ "s " ,
187220 false ,
188- "disable image rendering " ,
221+ "use sixel graphics encoder " ,
189222 )
190223 rootCmd .Flags ().BoolVarP (
191224 & noPretty ,
@@ -194,6 +227,13 @@ func Execute() {
194227 false ,
195228 "disable pretty output, output raw markdown instead" ,
196229 )
230+ rootCmd .Flags ().BoolVarP (
231+ & noImages ,
232+ "no-images" ,
233+ "i" ,
234+ false ,
235+ "disable image rendering" ,
236+ )
197237 rootCmd .Flags ().BoolVarP (
198238 & verbose ,
199239 "verbose" ,
0 commit comments