@@ -2,7 +2,6 @@ package image
22
33import (
44 "bytes"
5− "errors"
65 "fmt"
76 "hash/fnv"
87 "image"
@@ -195,50 +194,12 @@ func (e Encoding) Transmit(id string, img image.Image, cs CellSize, cols, rows i
195194 }
196195
197196 var buf bytes.Buffer
198− rp, wp := io.Pipe()
199− go func() {
200− for {
201− // Read single Kitty graphic chunks from the pipe and wrap them
202− // for tmux if needed.
203− var out bytes.Buffer
204− seenEsc := false
205− for {
206− var p [1]byte
207− n, err := rp.Read(p[:])
208− if n > 0 {
209− out.WriteByte(p[0])
210− if p[0] == ansi.ESC {
211− seenEsc = true
212− } else if seenEsc && p[0] == '\\' {
213− // End of Kitty graphics sequence
214− break
215− } else {
216− seenEsc = false
217− }
218− }
219− if err != nil {
220− if !errors.Is(err, io.EOF) {
221− slog.Error("error reading from pipe", "err", err)
222− }
223− return
224− }
225− }
226−
227− seq := out.String()
228− if tmux {
229− seq = ansi.TmuxPassthrough(seq)
230− }
231−
232− buf.WriteString(seq)
233− }
234− }()
235−
236197 img := fitImage(id, img, cs, cols, rows)
237198 bounds := img.Bounds()
238199 imgWidth := bounds.Dx()
239200 imgHeight := bounds.Dy()
240201 imgID := int(key.Hash())
241− if err := kitty.EncodeGraphics(wp, img, &kitty.Options{
202+ if err := kitty.EncodeGraphics(&buf, img, &kitty.Options{
242203 ID: imgID,
243204 Action: kitty.TransmitAndPut,
244205 Transmission: kitty.Direct,
@@ -250,9 +211,18 @@ func (e Encoding) Transmit(id string, img image.Image, cs CellSize, cols, rows i
250211 VirtualPlacement: true,
251212 Quite: 1,
252213 Chunk: true,
214+ ChunkFormatter: func(chunk string) string {
215+ if tmux {
216+ return ansi.TmuxPassthrough(chunk)
217+ }
218+ return chunk
219+ },
253220 }); err != nil {
254221 slog.Error("failed to encode image for kitty graphics", "err", err)
255− return uiutil.ReportError(fmt.Errorf("failed to encode image"))
222+ return uiutil.InfoMsg{
223+ Type: uiutil.InfoTypeError,
224+ Msg: "failed to encode image",
225+ }
256226 }
257227
258228 return tea.RawMsg{Msg: buf.String()}