graphics.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package kitty
  2. import "errors"
  3. // ErrMissingFile is returned when the file path is missing.
  4. var ErrMissingFile = errors.New("missing file path")
  5. // MaxChunkSize is the maximum chunk size for the image data.
  6. const MaxChunkSize = 1024 * 4
  7. // Placeholder is a special Unicode character that can be used as a placeholder
  8. // for an image.
  9. const Placeholder = '\U0010EEEE'
  10. // Graphics image format.
  11. const (
  12. // 32-bit RGBA format.
  13. RGBA = 32
  14. // 24-bit RGB format.
  15. RGB = 24
  16. // PNG format.
  17. PNG = 100
  18. )
  19. // Compression types.
  20. const (
  21. Zlib = 'z'
  22. )
  23. // Transmission types.
  24. const (
  25. // The data transmitted directly in the escape sequence.
  26. Direct = 'd'
  27. // The data transmitted in a regular file.
  28. File = 'f'
  29. // A temporary file is used and deleted after transmission.
  30. TempFile = 't'
  31. // A shared memory object.
  32. // For POSIX see https://pubs.opengroup.org/onlinepubs/9699919799/functions/shm_open.html
  33. // For Windows see https://docs.microsoft.com/en-us/windows/win32/memory/creating-named-shared-memory
  34. SharedMemory = 's'
  35. )
  36. // Action types.
  37. const (
  38. // Transmit image data.
  39. Transmit = 't'
  40. // TransmitAndPut transmit image data and display (put) it.
  41. TransmitAndPut = 'T'
  42. // Query terminal for image info.
  43. Query = 'q'
  44. // Put (display) previously transmitted image.
  45. Put = 'p'
  46. // Delete image.
  47. Delete = 'd'
  48. // Frame transmits data for animation frames.
  49. Frame = 'f'
  50. // Animate controls animation.
  51. Animate = 'a'
  52. // Compose composes animation frames.
  53. Compose = 'c'
  54. )
  55. // Delete types.
  56. const (
  57. // Delete all placements visible on screen
  58. DeleteAll = 'a'
  59. // Delete all images with the specified id, specified using the i key. If
  60. // you specify a p key for the placement id as well, then only the
  61. // placement with the specified image id and placement id will be deleted.
  62. DeleteID = 'i'
  63. // Delete newest image with the specified number, specified using the I
  64. // key. If you specify a p key for the placement id as well, then only the
  65. // placement with the specified number and placement id will be deleted.
  66. DeleteNumber = 'n'
  67. // Delete all placements that intersect with the current cursor position.
  68. DeleteCursor = 'c'
  69. // Delete animation frames.
  70. DeleteFrames = 'f'
  71. // Delete all placements that intersect a specific cell, the cell is
  72. // specified using the x and y keys
  73. DeleteCell = 'p'
  74. // Delete all placements that intersect a specific cell having a specific
  75. // z-index. The cell and z-index is specified using the x, y and z keys.
  76. DeleteCellZ = 'q'
  77. // Delete all images whose id is greater than or equal to the value of the x
  78. // key and less than or equal to the value of the y.
  79. DeleteRange = 'r'
  80. // Delete all placements that intersect the specified column, specified using
  81. // the x key.
  82. DeleteColumn = 'x'
  83. // Delete all placements that intersect the specified row, specified using
  84. // the y key.
  85. DeleteRow = 'y'
  86. // Delete all placements that have the specified z-index, specified using the
  87. // z key.
  88. DeleteZ = 'z'
  89. )
  90. // Diacritic returns the diacritic rune at the specified index. If the index is
  91. // out of bounds, the first diacritic rune is returned.
  92. func Diacritic(i int) rune {
  93. if i < 0 || i >= len(diacritics) {
  94. return diacritics[0]
  95. }
  96. return diacritics[i]
  97. }
  98. // From https://sw.kovidgoyal.net/kitty/_downloads/f0a0de9ec8d9ff4456206db8e0814937/rowcolumn-diacritics.txt
  99. // See https://sw.kovidgoyal.net/kitty/graphics-protocol/#unicode-placeholders for further explanation.
  100. var diacritics = []rune{
  101. '\u0305',
  102. '\u030D',
  103. '\u030E',
  104. '\u0310',
  105. '\u0312',
  106. '\u033D',
  107. '\u033E',
  108. '\u033F',
  109. '\u0346',
  110. '\u034A',
  111. '\u034B',
  112. '\u034C',
  113. '\u0350',
  114. '\u0351',
  115. '\u0352',
  116. '\u0357',
  117. '\u035B',
  118. '\u0363',
  119. '\u0364',
  120. '\u0365',
  121. '\u0366',
  122. '\u0367',
  123. '\u0368',
  124. '\u0369',
  125. '\u036A',
  126. '\u036B',
  127. '\u036C',
  128. '\u036D',
  129. '\u036E',
  130. '\u036F',
  131. '\u0483',
  132. '\u0484',
  133. '\u0485',
  134. '\u0486',
  135. '\u0487',
  136. '\u0592',
  137. '\u0593',
  138. '\u0594',
  139. '\u0595',
  140. '\u0597',
  141. '\u0598',
  142. '\u0599',
  143. '\u059C',
  144. '\u059D',
  145. '\u059E',
  146. '\u059F',
  147. '\u05A0',
  148. '\u05A1',
  149. '\u05A8',
  150. '\u05A9',
  151. '\u05AB',
  152. '\u05AC',
  153. '\u05AF',
  154. '\u05C4',
  155. '\u0610',
  156. '\u0611',
  157. '\u0612',
  158. '\u0613',
  159. '\u0614',
  160. '\u0615',
  161. '\u0616',
  162. '\u0617',
  163. '\u0657',
  164. '\u0658',
  165. '\u0659',
  166. '\u065A',
  167. '\u065B',
  168. '\u065D',
  169. '\u065E',
  170. '\u06D6',
  171. '\u06D7',
  172. '\u06D8',
  173. '\u06D9',
  174. '\u06DA',
  175. '\u06DB',
  176. '\u06DC',
  177. '\u06DF',
  178. '\u06E0',
  179. '\u06E1',
  180. '\u06E2',
  181. '\u06E4',
  182. '\u06E7',
  183. '\u06E8',
  184. '\u06EB',
  185. '\u06EC',
  186. '\u0730',
  187. '\u0732',
  188. '\u0733',
  189. '\u0735',
  190. '\u0736',
  191. '\u073A',
  192. '\u073D',
  193. '\u073F',
  194. '\u0740',
  195. '\u0741',
  196. '\u0743',
  197. '\u0745',
  198. '\u0747',
  199. '\u0749',
  200. '\u074A',
  201. '\u07EB',
  202. '\u07EC',
  203. '\u07ED',
  204. '\u07EE',
  205. '\u07EF',
  206. '\u07F0',
  207. '\u07F1',
  208. '\u07F3',
  209. '\u0816',
  210. '\u0817',
  211. '\u0818',
  212. '\u0819',
  213. '\u081B',
  214. '\u081C',
  215. '\u081D',
  216. '\u081E',
  217. '\u081F',
  218. '\u0820',
  219. '\u0821',
  220. '\u0822',
  221. '\u0823',
  222. '\u0825',
  223. '\u0826',
  224. '\u0827',
  225. '\u0829',
  226. '\u082A',
  227. '\u082B',
  228. '\u082C',
  229. '\u082D',
  230. '\u0951',
  231. '\u0953',
  232. '\u0954',
  233. '\u0F82',
  234. '\u0F83',
  235. '\u0F86',
  236. '\u0F87',
  237. '\u135D',
  238. '\u135E',
  239. '\u135F',
  240. '\u17DD',
  241. '\u193A',
  242. '\u1A17',
  243. '\u1A75',
  244. '\u1A76',
  245. '\u1A77',
  246. '\u1A78',
  247. '\u1A79',
  248. '\u1A7A',
  249. '\u1A7B',
  250. '\u1A7C',
  251. '\u1B6B',
  252. '\u1B6D',
  253. '\u1B6E',
  254. '\u1B6F',
  255. '\u1B70',
  256. '\u1B71',
  257. '\u1B72',
  258. '\u1B73',
  259. '\u1CD0',
  260. '\u1CD1',
  261. '\u1CD2',
  262. '\u1CDA',
  263. '\u1CDB',
  264. '\u1CE0',
  265. '\u1DC0',
  266. '\u1DC1',
  267. '\u1DC3',
  268. '\u1DC4',
  269. '\u1DC5',
  270. '\u1DC6',
  271. '\u1DC7',
  272. '\u1DC8',
  273. '\u1DC9',
  274. '\u1DCB',
  275. '\u1DCC',
  276. '\u1DD1',
  277. '\u1DD2',
  278. '\u1DD3',
  279. '\u1DD4',
  280. '\u1DD5',
  281. '\u1DD6',
  282. '\u1DD7',
  283. '\u1DD8',
  284. '\u1DD9',
  285. '\u1DDA',
  286. '\u1DDB',
  287. '\u1DDC',
  288. '\u1DDD',
  289. '\u1DDE',
  290. '\u1DDF',
  291. '\u1DE0',
  292. '\u1DE1',
  293. '\u1DE2',
  294. '\u1DE3',
  295. '\u1DE4',
  296. '\u1DE5',
  297. '\u1DE6',
  298. '\u1DFE',
  299. '\u20D0',
  300. '\u20D1',
  301. '\u20D4',
  302. '\u20D5',
  303. '\u20D6',
  304. '\u20D7',
  305. '\u20DB',
  306. '\u20DC',
  307. '\u20E1',
  308. '\u20E7',
  309. '\u20E9',
  310. '\u20F0',
  311. '\u2CEF',
  312. '\u2CF0',
  313. '\u2CF1',
  314. '\u2DE0',
  315. '\u2DE1',
  316. '\u2DE2',
  317. '\u2DE3',
  318. '\u2DE4',
  319. '\u2DE5',
  320. '\u2DE6',
  321. '\u2DE7',
  322. '\u2DE8',
  323. '\u2DE9',
  324. '\u2DEA',
  325. '\u2DEB',
  326. '\u2DEC',
  327. '\u2DED',
  328. '\u2DEE',
  329. '\u2DEF',
  330. '\u2DF0',
  331. '\u2DF1',
  332. '\u2DF2',
  333. '\u2DF3',
  334. '\u2DF4',
  335. '\u2DF5',
  336. '\u2DF6',
  337. '\u2DF7',
  338. '\u2DF8',
  339. '\u2DF9',
  340. '\u2DFA',
  341. '\u2DFB',
  342. '\u2DFC',
  343. '\u2DFD',
  344. '\u2DFE',
  345. '\u2DFF',
  346. '\uA66F',
  347. '\uA67C',
  348. '\uA67D',
  349. '\uA6F0',
  350. '\uA6F1',
  351. '\uA8E0',
  352. '\uA8E1',
  353. '\uA8E2',
  354. '\uA8E3',
  355. '\uA8E4',
  356. '\uA8E5',
  357. '\uA8E6',
  358. '\uA8E7',
  359. '\uA8E8',
  360. '\uA8E9',
  361. '\uA8EA',
  362. '\uA8EB',
  363. '\uA8EC',
  364. '\uA8ED',
  365. '\uA8EE',
  366. '\uA8EF',
  367. '\uA8F0',
  368. '\uA8F1',
  369. '\uAAB0',
  370. '\uAAB2',
  371. '\uAAB3',
  372. '\uAAB7',
  373. '\uAAB8',
  374. '\uAABE',
  375. '\uAABF',
  376. '\uAAC1',
  377. '\uFE20',
  378. '\uFE21',
  379. '\uFE22',
  380. '\uFE23',
  381. '\uFE24',
  382. '\uFE25',
  383. '\uFE26',
  384. '\U00010A0F',
  385. '\U00010A38',
  386. '\U0001D185',
  387. '\U0001D186',
  388. '\U0001D187',
  389. '\U0001D188',
  390. '\U0001D189',
  391. '\U0001D1AA',
  392. '\U0001D1AB',
  393. '\U0001D1AC',
  394. '\U0001D1AD',
  395. '\U0001D242',
  396. '\U0001D243',
  397. '\U0001D244',
  398. }