Skip to content

Commit 163b347

Browse files
committed
feat(markdown): do not bother trying to render high res img if terminal does not support it
1 parent ffa6439 commit 163b347

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

internal/img/chafa_backend.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ func NewChafaBackend() *chafaBackend {
5151
}
5252
}
5353

54+
func (b *chafaBackend) SymbolsOnly() bool {
55+
return b.detectTerminal().pixelMode == chafa.CHAFA_PIXEL_MODE_SYMBOLS
56+
}
57+
5458
func (b *chafaBackend) Render(path string, width, height int, symbols bool) (string, error) {
5559
var err error
5660

@@ -118,15 +122,15 @@ func (b chafaBackend) render(path string, width, height int32, symbols bool) (st
118122
return printable.String(), nil
119123
}
120124

121-
type terminalCapabilities struct {
125+
type chafaTerminalCapabilities struct {
122126
termInfo *chafa.TermInfo
123127
canvasMode chafa.CanvasMode
124128
pixelMode chafa.PixelMode
125129
passthrough chafa.Passthrough
126130
symbolMap *chafa.SymbolMap
127131
}
128132

129-
func (b chafaBackend) detectTerminal() terminalCapabilities {
133+
func (b chafaBackend) detectTerminal() chafaTerminalCapabilities {
130134
termInfo := chafa.TermDbDetect(chafa.TermDbGetDefault(), os.Environ())
131135

132136
mode := chafa.TermInfoGetBestCanvasMode(termInfo)
@@ -140,7 +144,7 @@ func (b chafaBackend) detectTerminal() terminalCapabilities {
140144
symbolMap := chafa.SymbolMapNew()
141145
chafa.SymbolMapAddByTags(symbolMap, chafa.TermInfoGetSafeSymbolTags(termInfo))
142146

143-
return terminalCapabilities{
147+
return chafaTerminalCapabilities{
144148
termInfo: termInfo,
145149
canvasMode: mode,
146150
pixelMode: pixelMode,

internal/img/images.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package img
22

33
type ImageBackend interface {
4+
SymbolsOnly() bool
45
Render(path string, width, height int, symbols bool) (string, error)
56
}
67

internal/markdown/renderer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,17 @@ func (r *Renderer) RenderBytes(in []byte, animating bool) (string, error) {
8686
continue
8787
}
8888

89+
if r.options.imgBackend.SymbolsOnly() {
90+
b.WriteString(limg)
91+
continue
92+
}
93+
8994
himg, err := r.options.imgBackend.Render(n.Path, n.Width, n.Height, false)
9095
if err != nil {
9196
b.WriteString(fmt.Sprintf("[Error rendering image: %s]", n.Label))
9297
continue
9398
}
9499

95-
// TODO: check if terminal supports rendering high res images in the
96-
// first place otherwise you will stack two low res images
97100
if !animating {
98101
b.WriteString(ansi.SaveCursor)
99102
b.WriteString(limg)

0 commit comments

Comments
 (0)