options.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package kitty
  2. import (
  3. "encoding"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. var (
  9. _ encoding.TextMarshaler = Options{}
  10. _ encoding.TextUnmarshaler = &Options{}
  11. )
  12. // Options represents a Kitty Graphics Protocol options.
  13. type Options struct {
  14. // Common options.
  15. // Action (a=t) is the action to be performed on the image. Can be one of
  16. // [Transmit], [TransmitDisplay], [Query], [Put], [Delete], [Frame],
  17. // [Animate], [Compose].
  18. Action byte
  19. // Quite mode (q=0) is the quiet mode. Can be either zero, one, or two
  20. // where zero is the default, 1 suppresses OK responses, and 2 suppresses
  21. // both OK and error responses.
  22. Quite byte
  23. // Transmission options.
  24. // ID (i=) is the image ID. The ID is a unique identifier for the image.
  25. // Must be a positive integer up to [math.MaxUint32].
  26. ID int
  27. // PlacementID (p=) is the placement ID. The placement ID is a unique
  28. // identifier for the placement of the image. Must be a positive integer up
  29. // to [math.MaxUint32].
  30. PlacementID int
  31. // Number (I=0) is the number of images to be transmitted.
  32. Number int
  33. // Format (f=32) is the image format. One of [RGBA], [RGB], [PNG].
  34. Format int
  35. // ImageWidth (s=0) is the transmitted image width.
  36. ImageWidth int
  37. // ImageHeight (v=0) is the transmitted image height.
  38. ImageHeight int
  39. // Compression (o=) is the image compression type. Can be [Zlib] or zero.
  40. Compression byte
  41. // Transmission (t=d) is the image transmission type. Can be [Direct], [File],
  42. // [TempFile], or[SharedMemory].
  43. Transmission byte
  44. // File is the file path to be used when the transmission type is [File].
  45. // If [Options.Transmission] is omitted i.e. zero and this is non-empty,
  46. // the transmission type is set to [File].
  47. File string
  48. // Size (S=0) is the size to be read from the transmission medium.
  49. Size int
  50. // Offset (O=0) is the offset byte to start reading from the transmission
  51. // medium.
  52. Offset int
  53. // Chunk (m=) whether the image is transmitted in chunks. Can be either
  54. // zero or one. When true, the image is transmitted in chunks. Each chunk
  55. // must be a multiple of 4, and up to [MaxChunkSize] bytes. Each chunk must
  56. // have the m=1 option except for the last chunk which must have m=0.
  57. Chunk bool
  58. // Display options.
  59. // X (x=0) is the pixel X coordinate of the image to start displaying.
  60. X int
  61. // Y (y=0) is the pixel Y coordinate of the image to start displaying.
  62. Y int
  63. // Z (z=0) is the Z coordinate of the image to display.
  64. Z int
  65. // Width (w=0) is the width of the image to display.
  66. Width int
  67. // Height (h=0) is the height of the image to display.
  68. Height int
  69. // OffsetX (X=0) is the OffsetX coordinate of the cursor cell to start
  70. // displaying the image. OffsetX=0 is the leftmost cell. This must be
  71. // smaller than the terminal cell width.
  72. OffsetX int
  73. // OffsetY (Y=0) is the OffsetY coordinate of the cursor cell to start
  74. // displaying the image. OffsetY=0 is the topmost cell. This must be
  75. // smaller than the terminal cell height.
  76. OffsetY int
  77. // Columns (c=0) is the number of columns to display the image. The image
  78. // will be scaled to fit the number of columns.
  79. Columns int
  80. // Rows (r=0) is the number of rows to display the image. The image will be
  81. // scaled to fit the number of rows.
  82. Rows int
  83. // VirtualPlacement (U=0) whether to use virtual placement. This is used
  84. // with Unicode [Placeholder] to display images.
  85. VirtualPlacement bool
  86. // DoNotMoveCursor (C=0) whether to move the cursor after displaying the
  87. // image.
  88. DoNotMoveCursor bool
  89. // ParentID (P=0) is the parent image ID. The parent ID is the ID of the
  90. // image that is the parent of the current image. This is used with Unicode
  91. // [Placeholder] to display images relative to the parent image.
  92. ParentID int
  93. // ParentPlacementID (Q=0) is the parent placement ID. The parent placement
  94. // ID is the ID of the placement of the parent image. This is used with
  95. // Unicode [Placeholder] to display images relative to the parent image.
  96. ParentPlacementID int
  97. // Delete options.
  98. // Delete (d=a) is the delete action. Can be one of [DeleteAll],
  99. // [DeleteID], [DeleteNumber], [DeleteCursor], [DeleteFrames],
  100. // [DeleteCell], [DeleteCellZ], [DeleteRange], [DeleteColumn], [DeleteRow],
  101. // [DeleteZ].
  102. Delete byte
  103. // DeleteResources indicates whether to delete the resources associated
  104. // with the image.
  105. DeleteResources bool
  106. }
  107. // Options returns the options as a slice of a key-value pairs.
  108. func (o *Options) Options() (opts []string) {
  109. opts = []string{}
  110. if o.Format == 0 {
  111. o.Format = RGBA
  112. }
  113. if o.Action == 0 {
  114. o.Action = Transmit
  115. }
  116. if o.Delete == 0 {
  117. o.Delete = DeleteAll
  118. }
  119. if o.Transmission == 0 {
  120. if len(o.File) > 0 {
  121. o.Transmission = File
  122. } else {
  123. o.Transmission = Direct
  124. }
  125. }
  126. if o.Format != RGBA {
  127. opts = append(opts, fmt.Sprintf("f=%d", o.Format))
  128. }
  129. if o.Quite > 0 {
  130. opts = append(opts, fmt.Sprintf("q=%d", o.Quite))
  131. }
  132. if o.ID > 0 {
  133. opts = append(opts, fmt.Sprintf("i=%d", o.ID))
  134. }
  135. if o.PlacementID > 0 {
  136. opts = append(opts, fmt.Sprintf("p=%d", o.PlacementID))
  137. }
  138. if o.Number > 0 {
  139. opts = append(opts, fmt.Sprintf("I=%d", o.Number))
  140. }
  141. if o.ImageWidth > 0 {
  142. opts = append(opts, fmt.Sprintf("s=%d", o.ImageWidth))
  143. }
  144. if o.ImageHeight > 0 {
  145. opts = append(opts, fmt.Sprintf("v=%d", o.ImageHeight))
  146. }
  147. if o.Transmission != Direct {
  148. opts = append(opts, fmt.Sprintf("t=%c", o.Transmission))
  149. }
  150. if o.Size > 0 {
  151. opts = append(opts, fmt.Sprintf("S=%d", o.Size))
  152. }
  153. if o.Offset > 0 {
  154. opts = append(opts, fmt.Sprintf("O=%d", o.Offset))
  155. }
  156. if o.Compression == Zlib {
  157. opts = append(opts, fmt.Sprintf("o=%c", o.Compression))
  158. }
  159. if o.VirtualPlacement {
  160. opts = append(opts, "U=1")
  161. }
  162. if o.DoNotMoveCursor {
  163. opts = append(opts, "C=1")
  164. }
  165. if o.ParentID > 0 {
  166. opts = append(opts, fmt.Sprintf("P=%d", o.ParentID))
  167. }
  168. if o.ParentPlacementID > 0 {
  169. opts = append(opts, fmt.Sprintf("Q=%d", o.ParentPlacementID))
  170. }
  171. if o.X > 0 {
  172. opts = append(opts, fmt.Sprintf("x=%d", o.X))
  173. }
  174. if o.Y > 0 {
  175. opts = append(opts, fmt.Sprintf("y=%d", o.Y))
  176. }
  177. if o.Z > 0 {
  178. opts = append(opts, fmt.Sprintf("z=%d", o.Z))
  179. }
  180. if o.Width > 0 {
  181. opts = append(opts, fmt.Sprintf("w=%d", o.Width))
  182. }
  183. if o.Height > 0 {
  184. opts = append(opts, fmt.Sprintf("h=%d", o.Height))
  185. }
  186. if o.OffsetX > 0 {
  187. opts = append(opts, fmt.Sprintf("X=%d", o.OffsetX))
  188. }
  189. if o.OffsetY > 0 {
  190. opts = append(opts, fmt.Sprintf("Y=%d", o.OffsetY))
  191. }
  192. if o.Columns > 0 {
  193. opts = append(opts, fmt.Sprintf("c=%d", o.Columns))
  194. }
  195. if o.Rows > 0 {
  196. opts = append(opts, fmt.Sprintf("r=%d", o.Rows))
  197. }
  198. if o.Delete != DeleteAll || o.DeleteResources {
  199. da := o.Delete
  200. if o.DeleteResources {
  201. da = da - ' ' // to uppercase
  202. }
  203. opts = append(opts, fmt.Sprintf("d=%c", da))
  204. }
  205. if o.Action != Transmit {
  206. opts = append(opts, fmt.Sprintf("a=%c", o.Action))
  207. }
  208. return
  209. }
  210. // String returns the string representation of the options.
  211. func (o Options) String() string {
  212. return strings.Join(o.Options(), ",")
  213. }
  214. // MarshalText returns the string representation of the options.
  215. func (o Options) MarshalText() ([]byte, error) {
  216. return []byte(o.String()), nil
  217. }
  218. // UnmarshalText parses the options from the given string.
  219. func (o *Options) UnmarshalText(text []byte) error {
  220. opts := strings.Split(string(text), ",")
  221. for _, opt := range opts {
  222. ps := strings.SplitN(opt, "=", 2)
  223. if len(ps) != 2 || len(ps[1]) == 0 {
  224. continue
  225. }
  226. switch ps[0] {
  227. case "a":
  228. o.Action = ps[1][0]
  229. case "o":
  230. o.Compression = ps[1][0]
  231. case "t":
  232. o.Transmission = ps[1][0]
  233. case "d":
  234. d := ps[1][0]
  235. if d >= 'A' && d <= 'Z' {
  236. o.DeleteResources = true
  237. d = d + ' ' // to lowercase
  238. }
  239. o.Delete = d
  240. case "i", "q", "p", "I", "f", "s", "v", "S", "O", "m", "x", "y", "z", "w", "h", "X", "Y", "c", "r", "U", "P", "Q":
  241. v, err := strconv.Atoi(ps[1])
  242. if err != nil {
  243. continue
  244. }
  245. switch ps[0] {
  246. case "i":
  247. o.ID = v
  248. case "q":
  249. o.Quite = byte(v)
  250. case "p":
  251. o.PlacementID = v
  252. case "I":
  253. o.Number = v
  254. case "f":
  255. o.Format = v
  256. case "s":
  257. o.ImageWidth = v
  258. case "v":
  259. o.ImageHeight = v
  260. case "S":
  261. o.Size = v
  262. case "O":
  263. o.Offset = v
  264. case "m":
  265. o.Chunk = v == 0 || v == 1
  266. case "x":
  267. o.X = v
  268. case "y":
  269. o.Y = v
  270. case "z":
  271. o.Z = v
  272. case "w":
  273. o.Width = v
  274. case "h":
  275. o.Height = v
  276. case "X":
  277. o.OffsetX = v
  278. case "Y":
  279. o.OffsetY = v
  280. case "c":
  281. o.Columns = v
  282. case "r":
  283. o.Rows = v
  284. case "U":
  285. o.VirtualPlacement = v == 1
  286. case "P":
  287. o.ParentID = v
  288. case "Q":
  289. o.ParentPlacementID = v
  290. }
  291. }
  292. }
  293. return nil
  294. }