@dev-crowagent/pigeon-tui 0.0.0-stage → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/README.md +950 -2
  2. package/dist/alt-screen-search.d.ts +46 -0
  3. package/dist/alt-screen-search.d.ts.map +1 -0
  4. package/dist/alt-screen-search.js +282 -0
  5. package/dist/alt-screen-search.js.map +1 -0
  6. package/dist/autocomplete.d.ts +57 -0
  7. package/dist/autocomplete.d.ts.map +1 -0
  8. package/dist/autocomplete.js +691 -0
  9. package/dist/autocomplete.js.map +1 -0
  10. package/dist/colors.d.ts +73 -0
  11. package/dist/colors.d.ts.map +1 -0
  12. package/dist/colors.js +273 -0
  13. package/dist/colors.js.map +1 -0
  14. package/dist/components/alt-screen-flash.d.ts +13 -0
  15. package/dist/components/alt-screen-flash.d.ts.map +1 -0
  16. package/dist/components/alt-screen-flash.js +37 -0
  17. package/dist/components/alt-screen-flash.js.map +1 -0
  18. package/dist/components/box.d.ts +24 -0
  19. package/dist/components/box.d.ts.map +1 -0
  20. package/dist/components/box.js +110 -0
  21. package/dist/components/box.js.map +1 -0
  22. package/dist/components/cancellable-loader.d.ts +22 -0
  23. package/dist/components/cancellable-loader.d.ts.map +1 -0
  24. package/dist/components/cancellable-loader.js +35 -0
  25. package/dist/components/cancellable-loader.js.map +1 -0
  26. package/dist/components/editor.d.ts +271 -0
  27. package/dist/components/editor.d.ts.map +1 -0
  28. package/dist/components/editor.js +2070 -0
  29. package/dist/components/editor.js.map +1 -0
  30. package/dist/components/h-stack.d.ts +7 -0
  31. package/dist/components/h-stack.d.ts.map +1 -0
  32. package/dist/components/h-stack.js +43 -0
  33. package/dist/components/h-stack.js.map +1 -0
  34. package/dist/components/image.d.ts +40 -0
  35. package/dist/components/image.d.ts.map +1 -0
  36. package/dist/components/image.js +123 -0
  37. package/dist/components/image.js.map +1 -0
  38. package/dist/components/input.d.ts +52 -0
  39. package/dist/components/input.d.ts.map +1 -0
  40. package/dist/components/input.js +422 -0
  41. package/dist/components/input.js.map +1 -0
  42. package/dist/components/loader.d.ts +33 -0
  43. package/dist/components/loader.d.ts.map +1 -0
  44. package/dist/components/loader.js +76 -0
  45. package/dist/components/loader.js.map +1 -0
  46. package/dist/components/markdown.d.ts +112 -0
  47. package/dist/components/markdown.d.ts.map +1 -0
  48. package/dist/components/markdown.js +948 -0
  49. package/dist/components/markdown.js.map +1 -0
  50. package/dist/components/mouse-region.d.ts +12 -0
  51. package/dist/components/mouse-region.d.ts.map +1 -0
  52. package/dist/components/mouse-region.js +21 -0
  53. package/dist/components/mouse-region.js.map +1 -0
  54. package/dist/components/scroll-view.d.ts +59 -0
  55. package/dist/components/scroll-view.d.ts.map +1 -0
  56. package/dist/components/scroll-view.js +195 -0
  57. package/dist/components/scroll-view.js.map +1 -0
  58. package/dist/components/select-list.d.ts +54 -0
  59. package/dist/components/select-list.d.ts.map +1 -0
  60. package/dist/components/select-list.js +215 -0
  61. package/dist/components/select-list.js.map +1 -0
  62. package/dist/components/settings-list.d.ts +61 -0
  63. package/dist/components/settings-list.d.ts.map +1 -0
  64. package/dist/components/settings-list.js +264 -0
  65. package/dist/components/settings-list.js.map +1 -0
  66. package/dist/components/spacer.d.ts +12 -0
  67. package/dist/components/spacer.d.ts.map +1 -0
  68. package/dist/components/spacer.js +23 -0
  69. package/dist/components/spacer.js.map +1 -0
  70. package/dist/components/stack.d.ts +32 -0
  71. package/dist/components/stack.d.ts.map +1 -0
  72. package/dist/components/stack.js +111 -0
  73. package/dist/components/stack.js.map +1 -0
  74. package/dist/components/text.d.ts +20 -0
  75. package/dist/components/text.d.ts.map +1 -0
  76. package/dist/components/text.js +98 -0
  77. package/dist/components/text.js.map +1 -0
  78. package/dist/components/truncated-text.d.ts +13 -0
  79. package/dist/components/truncated-text.d.ts.map +1 -0
  80. package/dist/components/truncated-text.js +51 -0
  81. package/dist/components/truncated-text.js.map +1 -0
  82. package/dist/components/v-stack.d.ts +8 -0
  83. package/dist/components/v-stack.d.ts.map +1 -0
  84. package/dist/components/v-stack.js +26 -0
  85. package/dist/components/v-stack.js.map +1 -0
  86. package/dist/editor-component.d.ts +39 -0
  87. package/dist/editor-component.d.ts.map +1 -0
  88. package/dist/editor-component.js +2 -0
  89. package/dist/editor-component.js.map +1 -0
  90. package/dist/fuzzy.d.ts +16 -0
  91. package/dist/fuzzy.d.ts.map +1 -0
  92. package/dist/fuzzy.js +126 -0
  93. package/dist/fuzzy.js.map +1 -0
  94. package/dist/index.d.ts +38 -0
  95. package/dist/index.d.ts.map +1 -0
  96. package/dist/index.js +52 -0
  97. package/dist/index.js.map +1 -0
  98. package/dist/keybindings.d.ts +279 -0
  99. package/dist/keybindings.d.ts.map +1 -0
  100. package/dist/keybindings.js +248 -0
  101. package/dist/keybindings.js.map +1 -0
  102. package/dist/keys.d.ts +184 -0
  103. package/dist/keys.d.ts.map +1 -0
  104. package/dist/keys.js +1263 -0
  105. package/dist/keys.js.map +1 -0
  106. package/dist/kill-ring.d.ts +28 -0
  107. package/dist/kill-ring.d.ts.map +1 -0
  108. package/dist/kill-ring.js +44 -0
  109. package/dist/kill-ring.js.map +1 -0
  110. package/dist/latex.d.ts +10 -0
  111. package/dist/latex.d.ts.map +1 -0
  112. package/dist/latex.js +1378 -0
  113. package/dist/latex.js.map +1 -0
  114. package/dist/layout-node.d.ts +40 -0
  115. package/dist/layout-node.d.ts.map +1 -0
  116. package/dist/layout-node.js +6 -0
  117. package/dist/layout-node.js.map +1 -0
  118. package/dist/layout.d.ts +54 -0
  119. package/dist/layout.d.ts.map +1 -0
  120. package/dist/layout.js +360 -0
  121. package/dist/layout.js.map +1 -0
  122. package/dist/native-modifiers.d.ts +4 -0
  123. package/dist/native-modifiers.d.ts.map +1 -0
  124. package/dist/native-modifiers.js +13 -0
  125. package/dist/native-modifiers.js.map +1 -0
  126. package/dist/native-module-path.d.ts +7 -0
  127. package/dist/native-module-path.d.ts.map +1 -0
  128. package/dist/native-module-path.js +19 -0
  129. package/dist/native-module-path.js.map +1 -0
  130. package/dist/native-platform.d.ts +20 -0
  131. package/dist/native-platform.d.ts.map +1 -0
  132. package/dist/native-platform.js +42 -0
  133. package/dist/native-platform.js.map +1 -0
  134. package/dist/oklab.d.ts +43 -0
  135. package/dist/oklab.d.ts.map +1 -0
  136. package/dist/oklab.js +210 -0
  137. package/dist/oklab.js.map +1 -0
  138. package/dist/program-resume.d.ts +35 -0
  139. package/dist/program-resume.d.ts.map +1 -0
  140. package/dist/program-resume.js +68 -0
  141. package/dist/program-resume.js.map +1 -0
  142. package/dist/program-status.d.ts +23 -0
  143. package/dist/program-status.d.ts.map +1 -0
  144. package/dist/program-status.js +43 -0
  145. package/dist/program-status.js.map +1 -0
  146. package/dist/stdin-buffer.d.ts +60 -0
  147. package/dist/stdin-buffer.d.ts.map +1 -0
  148. package/dist/stdin-buffer.js +411 -0
  149. package/dist/stdin-buffer.js.map +1 -0
  150. package/dist/terminal-colors.d.ts +27 -0
  151. package/dist/terminal-colors.d.ts.map +1 -0
  152. package/dist/terminal-colors.js +64 -0
  153. package/dist/terminal-colors.js.map +1 -0
  154. package/dist/terminal-image.d.ts +145 -0
  155. package/dist/terminal-image.d.ts.map +1 -0
  156. package/dist/terminal-image.js +671 -0
  157. package/dist/terminal-image.js.map +1 -0
  158. package/dist/terminal.d.ts +149 -0
  159. package/dist/terminal.d.ts.map +1 -0
  160. package/dist/terminal.js +518 -0
  161. package/dist/terminal.js.map +1 -0
  162. package/dist/tui-alt-screen.d.ts +171 -0
  163. package/dist/tui-alt-screen.d.ts.map +1 -0
  164. package/dist/tui-alt-screen.js +1580 -0
  165. package/dist/tui-alt-screen.js.map +1 -0
  166. package/dist/tui-main-screen.d.ts +39 -0
  167. package/dist/tui-main-screen.d.ts.map +1 -0
  168. package/dist/tui-main-screen.js +606 -0
  169. package/dist/tui-main-screen.js.map +1 -0
  170. package/dist/tui.d.ts +449 -0
  171. package/dist/tui.d.ts.map +1 -0
  172. package/dist/tui.js +1218 -0
  173. package/dist/tui.js.map +1 -0
  174. package/dist/undo-stack.d.ts +17 -0
  175. package/dist/undo-stack.d.ts.map +1 -0
  176. package/dist/undo-stack.js +25 -0
  177. package/dist/undo-stack.js.map +1 -0
  178. package/dist/utils.d.ts +109 -0
  179. package/dist/utils.d.ts.map +1 -0
  180. package/dist/utils.js +1428 -0
  181. package/dist/utils.js.map +1 -0
  182. package/dist/wheel-scroll.d.ts +23 -0
  183. package/dist/wheel-scroll.d.ts.map +1 -0
  184. package/dist/wheel-scroll.js +73 -0
  185. package/dist/wheel-scroll.js.map +1 -0
  186. package/dist/word-navigation.d.ts +25 -0
  187. package/dist/word-navigation.d.ts.map +1 -0
  188. package/dist/word-navigation.js +96 -0
  189. package/dist/word-navigation.js.map +1 -0
  190. package/native/clipboard.h +208 -0
  191. package/native/darwin/README.md +22 -0
  192. package/native/darwin/build.sh +69 -0
  193. package/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
  194. package/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
  195. package/native/darwin/src/darwin-platform.m +132 -0
  196. package/native/linux/README.md +23 -0
  197. package/native/linux/build.sh +65 -0
  198. package/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  199. package/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  200. package/native/linux/src/clipboard-worker.h +70 -0
  201. package/native/linux/src/linux-platform-x11.c +274 -0
  202. package/native/napi.h +91 -0
  203. package/native/win32/README.md +31 -0
  204. package/native/win32/build.mjs +232 -0
  205. package/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  206. package/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  207. package/native/win32/src/win32-platform.c +241 -0
  208. package/package.json +62 -5
@@ -0,0 +1,123 @@
1
+ import { allocateImageId, getCapabilities, getCellDimensions, getImageDimensions, getPngDimensions, imageFallback, renderImage, } from "../terminal-image.js";
2
+ import { truncateToWidth } from "../utils.js";
3
+ let imageTranscoder;
4
+ // Backstop for callers that recreate Image instances. Keyed by source data, least recently used first.
5
+ const pngCache = new Map();
6
+ /**
7
+ * Register the converter used for non-PNG images on Kitty-protocol terminals, which only accept PNG.
8
+ * Without one, such images render as text fallbacks.
9
+ */
10
+ export function setImageTranscoder(transcoder) {
11
+ imageTranscoder = transcoder;
12
+ pngCache.clear();
13
+ }
14
+ function toPng(base64Data, mimeType) {
15
+ if (!imageTranscoder)
16
+ return null;
17
+ const cached = pngCache.get(base64Data);
18
+ const png = cached === undefined ? imageTranscoder(base64Data, mimeType) : cached;
19
+ pngCache.delete(base64Data);
20
+ pngCache.set(base64Data, png);
21
+ if (pngCache.size > 32)
22
+ pngCache.delete(pngCache.keys().next().value);
23
+ return png;
24
+ }
25
+ export class Image {
26
+ base64Data;
27
+ mimeType;
28
+ dimensions;
29
+ theme;
30
+ options;
31
+ imageId;
32
+ /** Converted PNG data for Kitty. Failures are not stored so a later transcoder can retry. */
33
+ pngData;
34
+ cachedLines;
35
+ cachedWidth;
36
+ constructor(base64Data, mimeType, theme, options = {}, dimensions) {
37
+ this.base64Data = base64Data;
38
+ this.mimeType = mimeType;
39
+ this.theme = theme;
40
+ this.options = options;
41
+ this.dimensions = dimensions || getImageDimensions(base64Data, mimeType) || { widthPx: 800, heightPx: 600 };
42
+ this.imageId = options.imageId;
43
+ }
44
+ /** Get the Kitty image ID used by this image (if any). */
45
+ getImageId() {
46
+ return this.imageId;
47
+ }
48
+ invalidate() {
49
+ this.cachedLines = undefined;
50
+ this.cachedWidth = undefined;
51
+ }
52
+ render(width) {
53
+ if (this.cachedLines && this.cachedWidth === width) {
54
+ return this.cachedLines;
55
+ }
56
+ const maxWidth = Math.max(1, Math.min(width - 2, this.options.maxWidthCells ?? 60));
57
+ const cellDimensions = getCellDimensions();
58
+ const defaultMaxHeight = Math.max(1, Math.ceil((maxWidth * cellDimensions.widthPx) / cellDimensions.heightPx));
59
+ const maxHeight = this.options.maxHeightCells ?? defaultMaxHeight;
60
+ const caps = getCapabilities();
61
+ let data = this.base64Data;
62
+ let dimensions = this.dimensions;
63
+ if (caps.images === "kitty" && this.mimeType !== "image/png") {
64
+ this.pngData ??= toPng(this.base64Data, this.mimeType) ?? undefined;
65
+ data = this.pngData ?? null;
66
+ // Conversion may apply EXIF rotation, so prefer the PNG's own dimensions.
67
+ if (data)
68
+ dimensions = getPngDimensions(data) ?? dimensions;
69
+ }
70
+ let lines;
71
+ if (caps.images && data) {
72
+ if (caps.images === "kitty" && this.imageId === undefined) {
73
+ this.imageId = allocateImageId();
74
+ }
75
+ const result = renderImage(data, dimensions, {
76
+ maxWidthCells: maxWidth,
77
+ maxHeightCells: maxHeight,
78
+ imageId: this.imageId,
79
+ moveCursor: false,
80
+ });
81
+ if (result) {
82
+ // Store the image ID for later cleanup
83
+ if (result.imageId) {
84
+ this.imageId = result.imageId;
85
+ }
86
+ if (caps.images === "kitty") {
87
+ // For Kitty: C=1 prevents cursor movement.
88
+ // Don't need the cursor movement.
89
+ lines = [result.sequence];
90
+ // Return `rows` lines so TUI accounts for image height.
91
+ for (let i = 0; i < result.rows - 1; i++) {
92
+ lines.push("");
93
+ }
94
+ }
95
+ else {
96
+ // Return `rows` lines so TUI accounts for image height.
97
+ // First (rows-1) lines are empty and cleared before the image is drawn.
98
+ // Last line: move cursor back up, draw the image, then move back down
99
+ // so TUI cursor accounting stays inside the scroll area.
100
+ lines = [];
101
+ for (let i = 0; i < result.rows - 1; i++) {
102
+ lines.push("");
103
+ }
104
+ const rowOffset = result.rows - 1;
105
+ const moveUp = rowOffset > 0 ? `\x1b[${rowOffset}A` : "";
106
+ lines.push(moveUp + result.sequence);
107
+ }
108
+ }
109
+ else {
110
+ const fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename);
111
+ lines = [truncateToWidth(this.theme.fallbackColor(fallback), width)];
112
+ }
113
+ }
114
+ else {
115
+ const fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename);
116
+ lines = [truncateToWidth(this.theme.fallbackColor(fallback), width)];
117
+ }
118
+ this.cachedLines = lines;
119
+ this.cachedWidth = width;
120
+ return lines;
121
+ }
122
+ }
123
+ //# sourceMappingURL=image.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"image.js","sourceRoot":"","sources":["../../src/components/image.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,eAAe,EACf,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAEhB,aAAa,EACb,WAAW,GACX,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAQ9C,IAAI,eAA4C,CAAC;AACjD,uGAAuG;AACvG,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAyB,CAAC;AAElD;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,UAAuC;IACzE,eAAe,GAAG,UAAU,CAAC;IAC7B,QAAQ,CAAC,KAAK,EAAE,CAAC;AAClB,CAAC;AAED,SAAS,KAAK,CAAC,UAAkB,EAAE,QAAgB;IAClD,IAAI,CAAC,eAAe;QAAE,OAAO,IAAI,CAAC;IAClC,MAAM,MAAM,GAAG,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxC,MAAM,GAAG,GAAG,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,eAAe,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAClF,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAC5B,QAAQ,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;IAC9B,IAAI,QAAQ,CAAC,IAAI,GAAG,EAAE;QAAE,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAM,CAAC,CAAC;IACvE,OAAO,GAAG,CAAC;AACZ,CAAC;AAcD,MAAM,OAAO,KAAK;IACT,UAAU,CAAS;IACnB,QAAQ,CAAS;IACjB,UAAU,CAAk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AAM,QAAQ,GAAG,aAAa,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;YACtF,KAAK,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;QACtE,CAAC;QAED,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;QACzB,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC;QAEzB,OAAO,KAAK,CAAC;IACd,CAAC;CACD","sourcesContent":["import {\n\tallocateImageId,\n\tgetCapabilities,\n\tgetCellDimensions,\n\tgetImageDimensions,\n\tgetPngDimensions,\n\ttype ImageDimensions,\n\timageFallback,\n\trenderImage,\n} from \"../terminal-image.ts\";\nimport type { Component } from \"../tui.ts\";\nimport { truncateToWidth } from \"../utils.ts\";\n\n/**\n * Converts base64 image data to base64 PNG data, or returns null if it cannot.\n * Called synchronously during rendering.\n */\nexport type ImageTranscoder = (base64Data: string, mimeType: string) => string | null;\n\nlet imageTranscoder: ImageTranscoder | undefined;\n// Backstop for callers that recreate Image instances. Keyed by source data, least recently used first.\nconst pngCache = new Map<string, string | null>();\n\n/**\n * Register the converter used for non-PNG images on Kitty-protocol terminals, which only accept PNG.\n * Without one, such images render as text fallbacks.\n */\nexport function setImageTranscoder(transcoder: ImageTranscoder | undefined): void {\n\timageTranscoder = transcoder;\n\tpngCache.clear();\n}\n\nfunction toPng(base64Data: string, mimeType: string): string | null {\n\tif (!imageTranscoder) return null;\n\tconst cached = pngCache.get(base64Data);\n\tconst png = cached === undefined ? imageTranscoder(base64Data, mimeType) : cached;\n\tpngCache.delete(base64Data);\n\tpngCache.set(base64Data, png);\n\tif (pngCache.size > 32) pngCache.delete(pngCache.keys().next().value!);\n\treturn png;\n}\n\nexport interface ImageTheme {\n\tfallbackColor: (str: string) => string;\n}\n\nexport interface ImageOptions {\n\tmaxWidthCells?: number;\n\tmaxHeightCells?: number;\n\tfilename?: string;\n\t/** Kitty image ID. If provided, reuses this ID (for animations/updates). */\n\timageId?: number;\n}\n\nexport class Image implements Component {\n\tprivate base64Data: string;\n\tprivate mimeType: string;\n\tprivate dimensions: ImageDimensions;\n\tprivate theme: ImageTheme;\n\tprivate options: ImageOptions;\n\tprivate imageId?: number;\n\t/** Converted PNG data for Kitty. Failures are not stored so a later transcoder can retry. */\n\tprivate pngData?: string;\n\n\tprivate cachedLines?: string[];\n\tprivate cachedWidth?: number;\n\n\tconstructor(\n\t\tbase64Data: string,\n\t\tmimeType: string,\n\t\ttheme: ImageTheme,\n\t\toptions: ImageOptions = {},\n\t\tdimensions?: ImageDimensions,\n\t) {\n\t\tthis.base64Data = base64Data;\n\t\tthis.mimeType = mimeType;\n\t\tthis.theme = theme;\n\t\tthis.options = options;\n\t\tthis.dimensions = dimensions || getImageDimensions(base64Data, mimeType) || { widthPx: 800, heightPx: 600 };\n\t\tthis.imageId = options.imageId;\n\t}\n\n\t/** Get the Kitty image ID used by this image (if any). */\n\tgetImageId(): number | undefined {\n\t\treturn this.imageId;\n\t}\n\n\tinvalidate(): void {\n\t\tthis.cachedLines = undefined;\n\t\tthis.cachedWidth = undefined;\n\t}\n\n\trender(width: number): readonly string[] {\n\t\tif (this.cachedLines && this.cachedWidth === width) {\n\t\t\treturn this.cachedLines;\n\t\t}\n\n\t\tconst maxWidth = Math.max(1, Math.min(width - 2, this.options.maxWidthCells ?? 60));\n\t\tconst cellDimensions = getCellDimensions();\n\t\tconst defaultMaxHeight = Math.max(1, Math.ceil((maxWidth * cellDimensions.widthPx) / cellDimensions.heightPx));\n\t\tconst maxHeight = this.options.maxHeightCells ?? defaultMaxHeight;\n\n\t\tconst caps = getCapabilities();\n\t\tlet data: string | null = this.base64Data;\n\t\tlet dimensions = this.dimensions;\n\t\tif (caps.images === \"kitty\" && this.mimeType !== \"image/png\") {\n\t\t\tthis.pngData ??= toPng(this.base64Data, this.mimeType) ?? undefined;\n\t\t\tdata = this.pngData ?? null;\n\t\t\t// Conversion may apply EXIF rotation, so prefer the PNG's own dimensions.\n\t\t\tif (data) dimensions = getPngDimensions(data) ?? dimensions;\n\t\t}\n\t\tlet lines: string[];\n\n\t\tif (caps.images && data) {\n\t\t\tif (caps.images === \"kitty\" && this.imageId === undefined) {\n\t\t\t\tthis.imageId = allocateImageId();\n\t\t\t}\n\t\t\tconst result = renderImage(data, dimensions, {\n\t\t\t\tmaxWidthCells: maxWidth,\n\t\t\t\tmaxHeightCells: maxHeight,\n\t\t\t\timageId: this.imageId,\n\t\t\t\tmoveCursor: false,\n\t\t\t});\n\n\t\t\tif (result) {\n\t\t\t\t// Store the image ID for later cleanup\n\t\t\t\tif (result.imageId) {\n\t\t\t\t\tthis.imageId = result.imageId;\n\t\t\t\t}\n\n\t\t\t\tif (caps.images === \"kitty\") {\n\t\t\t\t\t// For Kitty: C=1 prevents cursor movement.\n\t\t\t\t\t// Don't need the cursor movement.\n\t\t\t\t\tlines = [result.sequence];\n\n\t\t\t\t\t// Return `rows` lines so TUI accounts for image height.\n\t\t\t\t\tfor (let i = 0; i < result.rows - 1; i++) {\n\t\t\t\t\t\tlines.push(\"\");\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// Return `rows` lines so TUI accounts for image height.\n\t\t\t\t\t// First (rows-1) lines are empty and cleared before the image is drawn.\n\t\t\t\t\t// Last line: move cursor back up, draw the image, then move back down\n\t\t\t\t\t// so TUI cursor accounting stays inside the scroll area.\n\t\t\t\t\tlines = [];\n\t\t\t\t\tfor (let i = 0; i < result.rows - 1; i++) {\n\t\t\t\t\t\tlines.push(\"\");\n\t\t\t\t\t}\n\t\t\t\t\tconst rowOffset = result.rows - 1;\n\t\t\t\t\tconst moveUp = rowOffset > 0 ? `\\x1b[${rowOffset}A` : \"\";\n\t\t\t\t\tlines.push(moveUp + result.sequence);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tconst fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename);\n\t\t\t\tlines = [truncateToWidth(this.theme.fallbackColor(fallback), width)];\n\t\t\t}\n\t\t} else {\n\t\t\tconst fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename);\n\t\t\tlines = [truncateToWidth(this.theme.fallbackColor(fallback), width)];\n\t\t}\n\n\t\tthis.cachedLines = lines;\n\t\tthis.cachedWidth = width;\n\n\t\treturn lines;\n\t}\n}\n"]}
@@ -0,0 +1,52 @@
1
+ import { type Component, type Focusable, type TuiMouseEvent, type TuiMouseEventResult } from "../tui.ts";
2
+ export interface InputOptions {
3
+ prompt?: string;
4
+ placeholder?: string;
5
+ placeholderStyle?: (text: string) => string;
6
+ }
7
+ /**
8
+ * Input component - single-line text input with horizontal scrolling
9
+ */
10
+ export declare class Input implements Component, Focusable {
11
+ private value;
12
+ private cursor;
13
+ private readonly prompt;
14
+ private readonly placeholder;
15
+ private readonly placeholderStyle;
16
+ private renderedStartColumn;
17
+ onSubmit?: (value: string) => void;
18
+ onEscape?: () => void;
19
+ /** Focusable interface - set by TUI when focus changes */
20
+ focused: boolean;
21
+ private pasteBuffer;
22
+ private isInPaste;
23
+ private killRing;
24
+ private lastAction;
25
+ private undoStack;
26
+ constructor(options?: InputOptions);
27
+ getValue(): string;
28
+ /**
29
+ * Replace the value and put the cursor after it, as `Editor.setText` does, so typing continues a prefilled value.
30
+ * The change is one undo step, and typing, kills and yank-pop after it do not continue edits from before it.
31
+ */
32
+ setValue(value: string): void;
33
+ handleInput(data: string): void;
34
+ handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined;
35
+ private insertCharacter;
36
+ private handleBackspace;
37
+ private handleForwardDelete;
38
+ private deleteToLineStart;
39
+ private deleteToLineEnd;
40
+ private deleteWordBackwards;
41
+ private deleteWordForward;
42
+ private yank;
43
+ private yankPop;
44
+ private pushUndo;
45
+ private undo;
46
+ private moveWordBackwards;
47
+ private moveWordForwards;
48
+ private handlePaste;
49
+ invalidate(): void;
50
+ render(width: number): string[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,aAAa,GAAG,mBAAmB,GAAG,SAAS,CAgBjE;IAED,OAAO,CAAC,eAAe;IAWvB,OAAO,CAAC,eAAe;IAavB,OAAO,CAAC,mBAAmB;IAY3B,OAAO,CAAC,iBAAiB;IAUzB,OAAO,CAAC,eAAe;IASvB,OAAO,CAAC,mBAAmB;IAqB3B,OAAO,CAAC,iBAAiB;IAoBzB,OAAO,CAAC,IAAI;IAWZ,OAAO,CAAC,OAAO;IAkBf,OAAO,CAAC,QAAQ;IAIhB,OAAO,CAAC,IAAI;IAQZ,OAAO,CAAC,iBAAiB;IAMzB,OAAO,CAAC,gBAAgB;IAMxB,OAAO,CAAC,WAAW;IAYnB,UAAU,IAAI,IAAI,CAEjB;IAED,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAiF9B;CACD"}
@@ -0,0 +1,422 @@
1
+ import { getKeybindings } from "../keybindings.js";
2
+ import { decodePrintableKey } from "../keys.js";
3
+ import { KillRing } from "../kill-ring.js";
4
+ import { CURSOR_MARKER, renderFakeCursor, } from "../tui.js";
5
+ import { UndoStack } from "../undo-stack.js";
6
+ import { getGraphemeSegmenter, isWhitespaceChar, sliceByColumn, truncateToWidth, visibleWidth } from "../utils.js";
7
+ import { findWordBackward, findWordForward } from "../word-navigation.js";
8
+ const segmenter = getGraphemeSegmenter();
9
+ /**
10
+ * Input component - single-line text input with horizontal scrolling
11
+ */
12
+ export class Input {
13
+ value = "";
14
+ cursor = 0; // Cursor position in the value
15
+ prompt;
16
+ placeholder;
17
+ placeholderStyle;
18
+ renderedStartColumn = 0;
19
+ onSubmit;
20
+ onEscape;
21
+ /** Focusable interface - set by TUI when focus changes */
22
+ focused = false;
23
+ // Bracketed paste mode buffering
24
+ pasteBuffer = "";
25
+ isInPaste = false;
26
+ // Kill ring for Emacs-style kill/yank operations
27
+ killRing = new KillRing();
28
+ lastAction = null;
29
+ // Undo support
30
+ undoStack = new UndoStack();
31
+ constructor(options = {}) {
32
+ this.prompt = options.prompt ?? "> ";
33
+ this.placeholder = options.placeholder ?? "";
34
+ this.placeholderStyle = options.placeholderStyle ?? ((text) => text);
35
+ }
36
+ getValue() {
37
+ return this.value;
38
+ }
39
+ /**
40
+ * Replace the value and put the cursor after it, as `Editor.setText` does, so typing continues a prefilled value.
41
+ * The change is one undo step, and typing, kills and yank-pop after it do not continue edits from before it.
42
+ */
43
+ setValue(value) {
44
+ this.lastAction = null;
45
+ if (value !== this.value)
46
+ this.pushUndo();
47
+ this.value = value;
48
+ this.cursor = value.length;
49
+ }
50
+ handleInput(data) {
51
+ // Handle bracketed paste mode
52
+ // Start of paste: \x1b[200~
53
+ // End of paste: \x1b[201~
54
+ // Check if we're starting a bracketed paste
55
+ if (data.includes("\x1b[200~")) {
56
+ this.isInPaste = true;
57
+ this.pasteBuffer = "";
58
+ data = data.replace("\x1b[200~", "");
59
+ }
60
+ // If we're in a paste, buffer the data
61
+ if (this.isInPaste) {
62
+ // Check if this chunk contains the end marker
63
+ this.pasteBuffer += data;
64
+ const endIndex = this.pasteBuffer.indexOf("\x1b[201~");
65
+ if (endIndex !== -1) {
66
+ // Extract the pasted content
67
+ const pasteContent = this.pasteBuffer.substring(0, endIndex);
68
+ // Process the complete paste
69
+ this.handlePaste(pasteContent);
70
+ // Reset paste state
71
+ this.isInPaste = false;
72
+ // Handle any remaining input after the paste marker
73
+ const remaining = this.pasteBuffer.substring(endIndex + 6); // 6 = length of \x1b[201~
74
+ this.pasteBuffer = "";
75
+ if (remaining) {
76
+ this.handleInput(remaining);
77
+ }
78
+ }
79
+ return;
80
+ }
81
+ const kb = getKeybindings();
82
+ // Escape/Cancel
83
+ if (kb.matches(data, "tui.select.cancel")) {
84
+ if (this.onEscape)
85
+ this.onEscape();
86
+ return;
87
+ }
88
+ // Undo
89
+ if (kb.matches(data, "tui.editor.undo")) {
90
+ this.undo();
91
+ return;
92
+ }
93
+ // Submit
94
+ if (kb.matches(data, "tui.input.submit") || data === "\n") {
95
+ if (this.onSubmit)
96
+ this.onSubmit(this.value);
97
+ return;
98
+ }
99
+ // Deletion
100
+ if (kb.matches(data, "tui.editor.deleteCharBackward")) {
101
+ this.handleBackspace();
102
+ return;
103
+ }
104
+ if (kb.matches(data, "tui.editor.deleteCharForward")) {
105
+ this.handleForwardDelete();
106
+ return;
107
+ }
108
+ if (kb.matches(data, "tui.editor.deleteWordBackward")) {
109
+ this.deleteWordBackwards();
110
+ return;
111
+ }
112
+ if (kb.matches(data, "tui.editor.deleteWordForward")) {
113
+ this.deleteWordForward();
114
+ return;
115
+ }
116
+ if (kb.matches(data, "tui.editor.deleteToLineStart")) {
117
+ this.deleteToLineStart();
118
+ return;
119
+ }
120
+ if (kb.matches(data, "tui.editor.deleteToLineEnd")) {
121
+ this.deleteToLineEnd();
122
+ return;
123
+ }
124
+ // Kill ring actions
125
+ if (kb.matches(data, "tui.editor.yank")) {
126
+ this.yank();
127
+ return;
128
+ }
129
+ if (kb.matches(data, "tui.editor.yankPop")) {
130
+ this.yankPop();
131
+ return;
132
+ }
133
+ // Cursor movement
134
+ if (kb.matches(data, "tui.editor.cursorLeft")) {
135
+ this.lastAction = null;
136
+ if (this.cursor > 0) {
137
+ const beforeCursor = this.value.slice(0, this.cursor);
138
+ const graphemes = [...segmenter.segment(beforeCursor)];
139
+ const lastGrapheme = graphemes[graphemes.length - 1];
140
+ this.cursor -= lastGrapheme ? lastGrapheme.segment.length : 1;
141
+ }
142
+ return;
143
+ }
144
+ if (kb.matches(data, "tui.editor.cursorRight")) {
145
+ this.lastAction = null;
146
+ if (this.cursor < this.value.length) {
147
+ const afterCursor = this.value.slice(this.cursor);
148
+ const graphemes = [...segmenter.segment(afterCursor)];
149
+ const firstGrapheme = graphemes[0];
150
+ this.cursor += firstGrapheme ? firstGrapheme.segment.length : 1;
151
+ }
152
+ return;
153
+ }
154
+ if (kb.matches(data, "tui.editor.cursorLineStart")) {
155
+ this.lastAction = null;
156
+ this.cursor = 0;
157
+ return;
158
+ }
159
+ if (kb.matches(data, "tui.editor.cursorLineEnd")) {
160
+ this.lastAction = null;
161
+ this.cursor = this.value.length;
162
+ return;
163
+ }
164
+ if (kb.matches(data, "tui.editor.cursorWordLeft")) {
165
+ this.moveWordBackwards();
166
+ return;
167
+ }
168
+ if (kb.matches(data, "tui.editor.cursorWordRight")) {
169
+ this.moveWordForwards();
170
+ return;
171
+ }
172
+ // Kitty CSI-u and xterm modifyOtherKeys printable character (e.g. \x1b[97u or \x1b[27;2;69~).
173
+ // Decode before the control-char check since escape sequences contain \x1b which would be rejected.
174
+ const printable = decodePrintableKey(data);
175
+ if (printable !== undefined) {
176
+ this.insertCharacter(printable);
177
+ return;
178
+ }
179
+ // Regular character input - accept printable characters including Unicode,
180
+ // but reject control characters (C0: 0x00-0x1F, DEL: 0x7F, C1: 0x80-0x9F)
181
+ const hasControlChars = [...data].some((ch) => {
182
+ const code = ch.charCodeAt(0);
183
+ return code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f);
184
+ });
185
+ if (!hasControlChars) {
186
+ this.insertCharacter(data);
187
+ }
188
+ }
189
+ handleMouse(event) {
190
+ if (event.type !== "press" || event.button !== "left" || event.y !== 0)
191
+ return undefined;
192
+ const visibleColumn = Math.max(0, event.x - 2);
193
+ const targetColumn = this.renderedStartColumn + visibleColumn;
194
+ let currentColumn = 0;
195
+ this.cursor = this.value.length;
196
+ for (const grapheme of segmenter.segment(this.value)) {
197
+ const nextColumn = currentColumn + visibleWidth(grapheme.segment);
198
+ if (targetColumn < nextColumn) {
199
+ this.cursor = grapheme.index;
200
+ break;
201
+ }
202
+ currentColumn = nextColumn;
203
+ }
204
+ this.lastAction = null;
205
+ return { handled: true, focus: true };
206
+ }
207
+ insertCharacter(char) {
208
+ // Undo coalescing: consecutive word chars coalesce into one undo unit
209
+ if (isWhitespaceChar(char) || this.lastAction !== "type-word") {
210
+ this.pushUndo();
211
+ }
212
+ this.lastAction = "type-word";
213
+ this.value = this.value.slice(0, this.cursor) + char + this.value.slice(this.cursor);
214
+ this.cursor += char.length;
215
+ }
216
+ handleBackspace() {
217
+ this.lastAction = null;
218
+ if (this.cursor > 0) {
219
+ this.pushUndo();
220
+ const beforeCursor = this.value.slice(0, this.cursor);
221
+ const graphemes = [...segmenter.segment(beforeCursor)];
222
+ const lastGrapheme = graphemes[graphemes.length - 1];
223
+ const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
224
+ this.value = this.value.slice(0, this.cursor - graphemeLength) + this.value.slice(this.cursor);
225
+ this.cursor -= graphemeLength;
226
+ }
227
+ }
228
+ handleForwardDelete() {
229
+ this.lastAction = null;
230
+ if (this.cursor < this.value.length) {
231
+ this.pushUndo();
232
+ const afterCursor = this.value.slice(this.cursor);
233
+ const graphemes = [...segmenter.segment(afterCursor)];
234
+ const firstGrapheme = graphemes[0];
235
+ const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
236
+ this.value = this.value.slice(0, this.cursor) + this.value.slice(this.cursor + graphemeLength);
237
+ }
238
+ }
239
+ deleteToLineStart() {
240
+ if (this.cursor === 0)
241
+ return;
242
+ this.pushUndo();
243
+ const deletedText = this.value.slice(0, this.cursor);
244
+ this.killRing.push(deletedText, { prepend: true, accumulate: this.lastAction === "kill" });
245
+ this.lastAction = "kill";
246
+ this.value = this.value.slice(this.cursor);
247
+ this.cursor = 0;
248
+ }
249
+ deleteToLineEnd() {
250
+ if (this.cursor >= this.value.length)
251
+ return;
252
+ this.pushUndo();
253
+ const deletedText = this.value.slice(this.cursor);
254
+ this.killRing.push(deletedText, { prepend: false, accumulate: this.lastAction === "kill" });
255
+ this.lastAction = "kill";
256
+ this.value = this.value.slice(0, this.cursor);
257
+ }
258
+ deleteWordBackwards() {
259
+ if (this.cursor === 0)
260
+ return;
261
+ // Save lastAction before cursor movement (moveWordBackwards resets it)
262
+ const wasKill = this.lastAction === "kill";
263
+ this.pushUndo();
264
+ const oldCursor = this.cursor;
265
+ this.moveWordBackwards();
266
+ const deleteFrom = this.cursor;
267
+ this.cursor = oldCursor;
268
+ const deletedText = this.value.slice(deleteFrom, this.cursor);
269
+ this.killRing.push(deletedText, { prepend: true, accumulate: wasKill });
270
+ this.lastAction = "kill";
271
+ this.value = this.value.slice(0, deleteFrom) + this.value.slice(this.cursor);
272
+ this.cursor = deleteFrom;
273
+ }
274
+ deleteWordForward() {
275
+ if (this.cursor >= this.value.length)
276
+ return;
277
+ // Save lastAction before cursor movement (moveWordForwards resets it)
278
+ const wasKill = this.lastAction === "kill";
279
+ this.pushUndo();
280
+ const oldCursor = this.cursor;
281
+ this.moveWordForwards();
282
+ const deleteTo = this.cursor;
283
+ this.cursor = oldCursor;
284
+ const deletedText = this.value.slice(this.cursor, deleteTo);
285
+ this.killRing.push(deletedText, { prepend: false, accumulate: wasKill });
286
+ this.lastAction = "kill";
287
+ this.value = this.value.slice(0, this.cursor) + this.value.slice(deleteTo);
288
+ }
289
+ yank() {
290
+ const text = this.killRing.peek();
291
+ if (!text)
292
+ return;
293
+ this.pushUndo();
294
+ this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);
295
+ this.cursor += text.length;
296
+ this.lastAction = "yank";
297
+ }
298
+ yankPop() {
299
+ if (this.lastAction !== "yank" || this.killRing.length <= 1)
300
+ return;
301
+ this.pushUndo();
302
+ // Delete the previously yanked text (still at end of ring before rotation)
303
+ const prevText = this.killRing.peek() || "";
304
+ this.value = this.value.slice(0, this.cursor - prevText.length) + this.value.slice(this.cursor);
305
+ this.cursor -= prevText.length;
306
+ // Rotate and insert new entry
307
+ this.killRing.rotate();
308
+ const text = this.killRing.peek() || "";
309
+ this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);
310
+ this.cursor += text.length;
311
+ this.lastAction = "yank";
312
+ }
313
+ pushUndo() {
314
+ this.undoStack.push({ value: this.value, cursor: this.cursor });
315
+ }
316
+ undo() {
317
+ const snapshot = this.undoStack.pop();
318
+ if (!snapshot)
319
+ return;
320
+ this.value = snapshot.value;
321
+ this.cursor = snapshot.cursor;
322
+ this.lastAction = null;
323
+ }
324
+ moveWordBackwards() {
325
+ if (this.cursor === 0)
326
+ return;
327
+ this.lastAction = null;
328
+ this.cursor = findWordBackward(this.value, this.cursor);
329
+ }
330
+ moveWordForwards() {
331
+ if (this.cursor >= this.value.length)
332
+ return;
333
+ this.lastAction = null;
334
+ this.cursor = findWordForward(this.value, this.cursor);
335
+ }
336
+ handlePaste(pastedText) {
337
+ this.lastAction = null;
338
+ this.pushUndo();
339
+ // Clean the pasted text - remove newlines and carriage returns
340
+ const cleanText = pastedText.replace(/\r\n/g, "").replace(/\r/g, "").replace(/\n/g, "").replace(/\t/g, " ");
341
+ // Insert at cursor position
342
+ this.value = this.value.slice(0, this.cursor) + cleanText + this.value.slice(this.cursor);
343
+ this.cursor += cleanText.length;
344
+ }
345
+ invalidate() {
346
+ // No cached state to invalidate currently
347
+ }
348
+ render(width) {
349
+ // Calculate visible window
350
+ const availableWidth = width - visibleWidth(this.prompt);
351
+ if (availableWidth <= 0) {
352
+ return [truncateToWidth(this.prompt, width, "")];
353
+ }
354
+ if (this.value.length === 0 && this.placeholder) {
355
+ const placeholder = truncateToWidth(this.placeholder, availableWidth, "");
356
+ const graphemes = [...segmenter.segment(placeholder)];
357
+ const atCursor = graphemes[0]?.segment ?? " ";
358
+ const afterCursor = placeholder.slice(atCursor.length);
359
+ const marker = this.focused ? CURSOR_MARKER : "";
360
+ const cursorChar = renderFakeCursor(this.placeholderStyle(atCursor));
361
+ const textWithCursor = marker + cursorChar + this.placeholderStyle(afterCursor);
362
+ const padding = " ".repeat(Math.max(0, availableWidth - visibleWidth(textWithCursor)));
363
+ return [this.prompt + textWithCursor + padding];
364
+ }
365
+ let visibleText = "";
366
+ let cursorDisplay = this.cursor;
367
+ this.renderedStartColumn = 0;
368
+ const totalWidth = visibleWidth(this.value);
369
+ if (totalWidth < availableWidth) {
370
+ // Everything fits (leave room for cursor at end)
371
+ visibleText = this.value;
372
+ }
373
+ else {
374
+ // Need horizontal scrolling
375
+ // Reserve one column for cursor if it's at the end
376
+ const scrollWidth = this.cursor === this.value.length ? availableWidth - 1 : availableWidth;
377
+ const cursorCol = visibleWidth(this.value.slice(0, this.cursor));
378
+ if (scrollWidth > 0) {
379
+ const halfWidth = Math.floor(scrollWidth / 2);
380
+ let startCol = 0;
381
+ if (cursorCol < halfWidth) {
382
+ // Cursor near start
383
+ startCol = 0;
384
+ }
385
+ else if (cursorCol > totalWidth - halfWidth) {
386
+ // Cursor near end
387
+ startCol = Math.max(0, totalWidth - scrollWidth);
388
+ }
389
+ else {
390
+ // Cursor in middle
391
+ startCol = Math.max(0, cursorCol - halfWidth);
392
+ }
393
+ this.renderedStartColumn = startCol;
394
+ visibleText = sliceByColumn(this.value, startCol, scrollWidth, true);
395
+ const beforeCursor = sliceByColumn(this.value, startCol, Math.max(0, cursorCol - startCol), true);
396
+ cursorDisplay = beforeCursor.length;
397
+ }
398
+ else {
399
+ visibleText = "";
400
+ cursorDisplay = 0;
401
+ }
402
+ }
403
+ // Build line with fake cursor
404
+ // Insert cursor character at cursor position
405
+ const graphemes = [...segmenter.segment(visibleText.slice(cursorDisplay))];
406
+ const cursorGrapheme = graphemes[0];
407
+ const beforeCursor = visibleText.slice(0, cursorDisplay);
408
+ const atCursor = cursorGrapheme?.segment ?? " "; // Character at cursor, or space if at end
409
+ const afterCursor = visibleText.slice(cursorDisplay + atCursor.length);
410
+ // Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning)
411
+ const marker = this.focused ? CURSOR_MARKER : "";
412
+ // TUI draws the fake cursor in reverse video, or omits it when the hardware cursor is shown
413
+ const cursorChar = renderFakeCursor(atCursor);
414
+ const textWithCursor = beforeCursor + marker + cursorChar + afterCursor;
415
+ // Calculate visual width
416
+ const visualLength = visibleWidth(textWithCursor);
417
+ const padding = " ".repeat(Math.max(0, availableWidth - visualLength));
418
+ const line = this.prompt + textWithCursor + padding;
419
+ return [line];
420
+ }
421
+ }
422
+ //# sourceMappingURL=input.js.map