feat: showImage: center and scale image by default

This commit is contained in:
Alexander Gehrke 2024-05-20 23:50:36 +02:00
parent 35a6dfbe53
commit dabbd75c48

View file

@ -5,20 +5,28 @@ import Terminal.*
object KittyGraphicsProtocol: object KittyGraphicsProtocol:
val MaxID = 4294967295L // max 32-bit unsigned val MaxID = 4294967295L // max 32-bit unsigned
def checkSupport = def checkSupport() =
queryTerm(s"${apc}Gi=${KittyGraphicsProtocol.MaxID},s=1,v=1,a=q,t=d,f=24;AAAA${st}${csi}c") queryTerm(s"${apc}Gi=${KittyGraphicsProtocol.MaxID},s=1,v=1,a=q,t=d,f=24;AAAA${st}${csi}c")
.contains(s"${apc}Gi=${KittyGraphicsProtocol.MaxID}") .contains(s"${apc}Gi=${KittyGraphicsProtocol.MaxID}")
def showImage(img: os.Path) = def showImage(img: os.Path) =
import java.util.Base64 import java.util.Base64
val image = Base64.getEncoder.encodeToString(os.read.bytes(img)) (
if image.length > 4096 then for
val chunks = image.grouped(4096).toVector _ <- Option.when(checkSupport())(true)
termSize <- Terminal.getSize()
cursorPos <- Terminal.getCursorPos()
yield
val image = Base64.getEncoder.encodeToString(os.read.bytes(img))
if image.length > 4096 then
val chunks = image.grouped(4096).toVector
val width = termSize.cols - cursorPos.cols * 2
s"${apc}Gf=100,t=d,m=1,a=T;${chunks.head}${st}" + s"${apc}Gf=100,t=d,m=1,a=T,c=${width};${chunks.head}${st}" +
chunks.tail.init.map(c => s"${apc}Gm=1;${c}${st}").mkString + chunks.tail.init.map(c => s"${apc}Gm=1;${c}${st}").mkString +
s"${apc}Gm=0;${chunks.last}${st}" s"${apc}Gm=0;${chunks.last}${st}"
else s"${apc}Gf=100,t=d,a=T;${image}${st}" else s"${apc}Gf=100,t=d,a=T;${image}${st}"
).getOrElse("Could not show image")
trait Param: trait Param:
def code: String def code: String