diff --git a/copret/src/images.scala b/copret/src/images.scala index 6820a36..15c862c 100644 --- a/copret/src/images.scala +++ b/copret/src/images.scala @@ -5,20 +5,28 @@ import Terminal.* object KittyGraphicsProtocol: 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") .contains(s"${apc}Gi=${KittyGraphicsProtocol.MaxID}") def showImage(img: os.Path) = import java.util.Base64 - val image = Base64.getEncoder.encodeToString(os.read.bytes(img)) - if image.length > 4096 then - val chunks = image.grouped(4096).toVector + ( + for + _ <- 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}" + - chunks.tail.init.map(c => s"${apc}Gm=1;${c}${st}").mkString + - s"${apc}Gm=0;${chunks.last}${st}" - else s"${apc}Gf=100,t=d,a=T;${image}${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 + + s"${apc}Gm=0;${chunks.last}${st}" + else s"${apc}Gf=100,t=d,a=T;${image}${st}" + ).getOrElse("Could not show image") trait Param: def code: String