|
| 1 | +package term |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "golang.org/x/crypto/ssh/terminal" |
| 12 | +) |
| 13 | + |
| 14 | +var kittyEnabled = false |
| 15 | +var kittyCurrentID = 1 // Track current image ID globally |
| 16 | +var kittyFirstImage = true // Track if this is the first image |
| 17 | + |
| 18 | +func init() { |
| 19 | + if os.Getenv("TERM_PROGRAM") != "iTerm.app" { |
| 20 | + kittyEnabled = checkKitty() |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// checkKitty detects if the terminal supports Kitty graphics protocol |
| 25 | +func checkKitty() bool { |
| 26 | + s, err := terminal.MakeRaw(1) |
| 27 | + if err != nil { |
| 28 | + return false |
| 29 | + } |
| 30 | + defer terminal.Restore(1, s) |
| 31 | + |
| 32 | + // Send Kitty graphics query to check if graphics are supported |
| 33 | + // Use a simple query that Kitty will respond to if it supports graphics |
| 34 | + _, err = os.Stdout.Write([]byte("\033_Gi=1,a=q,t=d,f=24\033\\")) |
| 35 | + if err != nil { |
| 36 | + return false |
| 37 | + } |
| 38 | + |
| 39 | + fileSetReadDeadline(os.Stdout, time.Now().Add(500*time.Millisecond)) |
| 40 | + defer fileSetReadDeadline(os.Stdout, time.Time{}) |
| 41 | + |
| 42 | + var b [200]byte |
| 43 | + n, err := os.Stdout.Read(b[:]) |
| 44 | + if err != nil { |
| 45 | + return false |
| 46 | + } |
| 47 | + |
| 48 | + // Look for Kitty's graphics response |
| 49 | + // Kitty will respond with \033_Gi=1;...\033\ if it supports graphics |
| 50 | + response := b[:n] |
| 51 | + return bytes.Contains(response, []byte("_G")) && bytes.Contains(response, []byte("i=1")) |
| 52 | +} |
| 53 | + |
| 54 | +// kittyWriter is a writer that displays PNG images in Kitty terminal using the Kitty graphics protocol |
| 55 | +type kittyWriter struct { |
| 56 | + Name string |
| 57 | + Width int |
| 58 | + Height int |
| 59 | + |
| 60 | + once sync.Once |
| 61 | + buf *bytes.Buffer |
| 62 | +} |
| 63 | + |
| 64 | +func (w *kittyWriter) init() { |
| 65 | + w.buf = &bytes.Buffer{} |
| 66 | +} |
| 67 | + |
| 68 | +// Write writes the PNG image data into the kittyWriter buffer. |
| 69 | +func (w *kittyWriter) Write(p []byte) (n int, err error) { |
| 70 | + w.once.Do(w.init) |
| 71 | + return w.buf.Write(p) |
| 72 | +} |
| 73 | + |
| 74 | +// Close flushes the image to the terminal using Kitty's graphics protocol and closes the writer. |
| 75 | +func (w *kittyWriter) Close() error { |
| 76 | + w.once.Do(w.init) |
| 77 | + |
| 78 | + // Encode the image data as base64 |
| 79 | + b64data := base64.StdEncoding.EncodeToString(w.buf.Bytes()) |
| 80 | + |
| 81 | + // Calculate next image ID (alternate between 1 and 2) |
| 82 | + nextID := 3 - kittyCurrentID |
| 83 | + |
| 84 | + // Step 1: Display the new image with absolute positioning |
| 85 | + // a=T (transmit and display), f=100 (PNG), t=d (direct), i=nextID (image ID), |
| 86 | + // X=0,Y=0 (absolute position), q=2 (suppress responses) |
| 87 | + fmt.Printf("\033_Ga=T,f=100,t=d,i=%d,X=0,Y=0,q=2;%s\033\\", nextID, b64data) |
| 88 | + |
| 89 | + // Step 2: Delete the previously displayed image (skip deletion only on very first image) |
| 90 | + // Remove q=2 from delete to ensure it executes properly |
| 91 | + if !kittyFirstImage { |
| 92 | + fmt.Printf("\033_Ga=d,d=i,i=%d,q=2\033\\", kittyCurrentID) |
| 93 | + } |
| 94 | + kittyFirstImage = false // After first image, we always delete |
| 95 | + |
| 96 | + // Update current ID for next time |
| 97 | + kittyCurrentID = nextID |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
0 commit comments