niedzielski / cb

📋 Universal command-line clipboard with automatic copy and paste detection. Eg, `cb|sort|cb`. The missing link between GUIs and CLIs!
The Unlicense
120 stars 6 forks source link

Image preview #8

Open chapmanjacobd opened 3 weeks ago

chapmanjacobd commented 3 weeks ago

I made a little cb inspired thing:

image

#!/usr/bin/env fish
# Public Domain

if test -t 0
    set file (mktemp --suffix .png)
    xclip -selection clipboard -target image/png -out > $file
    if test -t 1
        kitty +kitten icat $file
    else
        cat $file
    end
else
    set file (mktemp --suffix .png | tee -a /dev/tty)
    cat > $file
    xclip -selection clipboard $file
end

Thankfully at least ImageMagick supports stdin/stdout:

cbi | convert -flop png:- png:- | cbi
niedzielski commented 3 weeks ago

very cool! this must use sixel graphics? I never developed much skill with fish but it looks like this is cb with shell image support. my current thinking is that I kind of like how simple and dumb cb is but what I like about yours is that it adds some extra value over tools like xclip. I'll leave this issue open to think about it some more but, regardless, this is a very neat idea!

chapmanjacobd commented 3 weeks ago

Yeah it's not fully compatible with cb and it's far from cross-platform.

Sixel adoption is pretty good but not quite universal: https://www.arewesixelyet.com/ It looks like kitty uses its own graphics protocol

But I agree, the idea likely has potential... The key insight for me was this:

if test -t 1
    bat $file
else
    cat $file

If stdout is connected to a terminal we can use a different program to preview it. Otherwise, we are connected to a pipe which could be a second cb instance, etc so we send the raw data.

(The above example is useful for showing why you might want a different program for text previewing even though it is not technically needed because bat also checks stdout is a tty and then behaves like cat. But image printing programs like lsix or kitten icat have no reason to pipe the original data forward and so they don't check stdout--they output the image data the same way such that it could be re-printed but no longer png format so it can't be re-sent to ImageMagick...)

Actually, ImageMagick supports decoding/encoding Sixel: https://github.com/ImageMagick/ImageMagick/blob/master/coders/sixel.c so that might be an interesting exploration point. Maybe checking stdout is not really needed