Skip to content

Commit 0d53289

Browse files
authored
Merge pull request #14 from AbeEstrada/sixel
Add sixel encoder option
2 parents 867cc5b + 58bdf31 commit 0d53289

5 files changed

Lines changed: 58 additions & 4 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ Read from stdin:
4242
curl -o - https://superhighway84.com | reader -
4343
```
4444

45+
Render images using the SIXEL graphics encoder:
46+
47+
```sh
48+
reader -s https://xn--gckvb8fzb.com/travel-aruba/
49+
```
50+
51+
![sixel](sixel.png)
52+
4553
More options:
4654

4755
```sh

cmd/root.go

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package cmd
22

33
import (
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 (
2129
var verbose bool
2230
var noImages bool
2331
var noPretty bool
32+
var sixelEncoder bool
2433

2534
type 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

182215
func 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",

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ require (
5959
github.com/mattn/go-colorable v0.1.13 // indirect
6060
github.com/mattn/go-isatty v0.0.19 // indirect
6161
github.com/mattn/go-runewidth v0.0.15 // indirect
62+
github.com/mattn/go-sixel v0.0.5 // indirect
6263
github.com/mattn/go-sqlite3 v1.14.17 // indirect
6364
github.com/memclutter/go-cloudflare-scraper v0.0.0-20220907170638-a1faa8b189bd // indirect
6465
github.com/microcosm-cc/bluemonday v1.0.25 // indirect
@@ -77,6 +78,7 @@ require (
7778
github.com/rivo/uniseg v0.4.4 // indirect
7879
github.com/robertkrimen/otto v0.2.1 // indirect
7980
github.com/sirupsen/logrus v1.9.3 // indirect
81+
github.com/soniakeys/quant v1.0.0 // indirect
8082
github.com/spf13/afero v1.9.5 // indirect
8183
github.com/spf13/cast v1.5.1 // indirect
8284
github.com/spf13/jwalterweatherman v1.1.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRC
483483
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
484484
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
485485
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
486+
github.com/mattn/go-sixel v0.0.5 h1:55w2FR5ncuhKhXrM5ly1eiqMQfZsnAHIpYNGZX03Cv8=
487+
github.com/mattn/go-sixel v0.0.5/go.mod h1:h2Sss+DiUEHy0pUqcIB6PFXo5Cy8sTQEFr3a9/5ZLNw=
486488
github.com/mattn/go-slim v0.0.0-20200618151855-bde33eecb5ee/go.mod h1:ma9TUJeni8LGZMJvOwbAv/FOwiwqIMQN570LnpqCBSM=
487489
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
488490
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
@@ -623,6 +625,8 @@ github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYl
623625
github.com/smartystreets/assertions v1.2.1/go.mod h1:wDmR7qL282YbGsPy6H/yAsesrxfxaaSlJazyFLYVFx8=
624626
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
625627
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
628+
github.com/soniakeys/quant v1.0.0 h1:N1um9ktjbkZVcywBVAAYpZYSHxEfJGzshHCxx/DaI0Y=
629+
github.com/soniakeys/quant v1.0.0/go.mod h1:HI1k023QuVbD4H8i9YdfZP2munIHU4QpjsImz6Y6zds=
626630
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
627631
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
628632
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=

sixel.png

304 KB
Loading

0 commit comments

Comments
 (0)