@ny4n/tui 0.0.0-stage → 1.0.1

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 (205) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +948 -2
  3. package/dist/alt-screen-search.d.ts +46 -0
  4. package/dist/alt-screen-search.d.ts.map +1 -0
  5. package/dist/alt-screen-search.js +282 -0
  6. package/dist/alt-screen-search.js.map +1 -0
  7. package/dist/autocomplete.d.ts +57 -0
  8. package/dist/autocomplete.d.ts.map +1 -0
  9. package/dist/autocomplete.js +693 -0
  10. package/dist/autocomplete.js.map +1 -0
  11. package/dist/colors.d.ts +73 -0
  12. package/dist/colors.d.ts.map +1 -0
  13. package/dist/colors.js +273 -0
  14. package/dist/colors.js.map +1 -0
  15. package/dist/components/alt-screen-flash.d.ts +13 -0
  16. package/dist/components/alt-screen-flash.d.ts.map +1 -0
  17. package/dist/components/alt-screen-flash.js +37 -0
  18. package/dist/components/alt-screen-flash.js.map +1 -0
  19. package/dist/components/box.d.ts +25 -0
  20. package/dist/components/box.d.ts.map +1 -0
  21. package/dist/components/box.js +138 -0
  22. package/dist/components/box.js.map +1 -0
  23. package/dist/components/cancellable-loader.d.ts +22 -0
  24. package/dist/components/cancellable-loader.d.ts.map +1 -0
  25. package/dist/components/cancellable-loader.js +35 -0
  26. package/dist/components/cancellable-loader.js.map +1 -0
  27. package/dist/components/editor.d.ts +260 -0
  28. package/dist/components/editor.d.ts.map +1 -0
  29. package/dist/components/editor.js +2044 -0
  30. package/dist/components/editor.js.map +1 -0
  31. package/dist/components/h-stack.d.ts +7 -0
  32. package/dist/components/h-stack.d.ts.map +1 -0
  33. package/dist/components/h-stack.js +43 -0
  34. package/dist/components/h-stack.js.map +1 -0
  35. package/dist/components/image.d.ts +40 -0
  36. package/dist/components/image.d.ts.map +1 -0
  37. package/dist/components/image.js +123 -0
  38. package/dist/components/image.js.map +1 -0
  39. package/dist/components/input.d.ts +48 -0
  40. package/dist/components/input.d.ts.map +1 -0
  41. package/dist/components/input.js +417 -0
  42. package/dist/components/input.js.map +1 -0
  43. package/dist/components/loader.d.ts +33 -0
  44. package/dist/components/loader.d.ts.map +1 -0
  45. package/dist/components/loader.js +76 -0
  46. package/dist/components/loader.js.map +1 -0
  47. package/dist/components/markdown.d.ts +104 -0
  48. package/dist/components/markdown.d.ts.map +1 -0
  49. package/dist/components/markdown.js +821 -0
  50. package/dist/components/markdown.js.map +1 -0
  51. package/dist/components/mouse-region.d.ts +12 -0
  52. package/dist/components/mouse-region.d.ts.map +1 -0
  53. package/dist/components/mouse-region.js +21 -0
  54. package/dist/components/mouse-region.js.map +1 -0
  55. package/dist/components/scroll-view.d.ts +59 -0
  56. package/dist/components/scroll-view.d.ts.map +1 -0
  57. package/dist/components/scroll-view.js +195 -0
  58. package/dist/components/scroll-view.js.map +1 -0
  59. package/dist/components/select-list.d.ts +53 -0
  60. package/dist/components/select-list.d.ts.map +1 -0
  61. package/dist/components/select-list.js +206 -0
  62. package/dist/components/select-list.js.map +1 -0
  63. package/dist/components/settings-list.d.ts +60 -0
  64. package/dist/components/settings-list.d.ts.map +1 -0
  65. package/dist/components/settings-list.js +252 -0
  66. package/dist/components/settings-list.js.map +1 -0
  67. package/dist/components/spacer.d.ts +12 -0
  68. package/dist/components/spacer.d.ts.map +1 -0
  69. package/dist/components/spacer.js +23 -0
  70. package/dist/components/spacer.js.map +1 -0
  71. package/dist/components/stack.d.ts +32 -0
  72. package/dist/components/stack.d.ts.map +1 -0
  73. package/dist/components/stack.js +111 -0
  74. package/dist/components/stack.js.map +1 -0
  75. package/dist/components/text.d.ts +20 -0
  76. package/dist/components/text.d.ts.map +1 -0
  77. package/dist/components/text.js +95 -0
  78. package/dist/components/text.js.map +1 -0
  79. package/dist/components/truncated-text.d.ts +13 -0
  80. package/dist/components/truncated-text.d.ts.map +1 -0
  81. package/dist/components/truncated-text.js +51 -0
  82. package/dist/components/truncated-text.js.map +1 -0
  83. package/dist/components/v-stack.d.ts +8 -0
  84. package/dist/components/v-stack.d.ts.map +1 -0
  85. package/dist/components/v-stack.js +26 -0
  86. package/dist/components/v-stack.js.map +1 -0
  87. package/dist/editor-component.d.ts +39 -0
  88. package/dist/editor-component.d.ts.map +1 -0
  89. package/dist/editor-component.js +2 -0
  90. package/dist/editor-component.js.map +1 -0
  91. package/dist/fuzzy.d.ts +16 -0
  92. package/dist/fuzzy.d.ts.map +1 -0
  93. package/dist/fuzzy.js +111 -0
  94. package/dist/fuzzy.js.map +1 -0
  95. package/dist/index.d.ts +37 -0
  96. package/dist/index.d.ts.map +1 -0
  97. package/dist/index.js +50 -0
  98. package/dist/index.js.map +1 -0
  99. package/dist/keybindings.d.ts +273 -0
  100. package/dist/keybindings.d.ts.map +1 -0
  101. package/dist/keybindings.js +233 -0
  102. package/dist/keybindings.js.map +1 -0
  103. package/dist/keys.d.ts +184 -0
  104. package/dist/keys.d.ts.map +1 -0
  105. package/dist/keys.js +1206 -0
  106. package/dist/keys.js.map +1 -0
  107. package/dist/kill-ring.d.ts +28 -0
  108. package/dist/kill-ring.d.ts.map +1 -0
  109. package/dist/kill-ring.js +44 -0
  110. package/dist/kill-ring.js.map +1 -0
  111. package/dist/latex.d.ts +10 -0
  112. package/dist/latex.d.ts.map +1 -0
  113. package/dist/latex.js +1378 -0
  114. package/dist/latex.js.map +1 -0
  115. package/dist/layout-node.d.ts +40 -0
  116. package/dist/layout-node.d.ts.map +1 -0
  117. package/dist/layout-node.js +6 -0
  118. package/dist/layout-node.js.map +1 -0
  119. package/dist/layout.d.ts +42 -0
  120. package/dist/layout.d.ts.map +1 -0
  121. package/dist/layout.js +344 -0
  122. package/dist/layout.js.map +1 -0
  123. package/dist/native-modifiers.d.ts +4 -0
  124. package/dist/native-modifiers.d.ts.map +1 -0
  125. package/dist/native-modifiers.js +13 -0
  126. package/dist/native-modifiers.js.map +1 -0
  127. package/dist/native-module-path.d.ts +7 -0
  128. package/dist/native-module-path.d.ts.map +1 -0
  129. package/dist/native-module-path.js +19 -0
  130. package/dist/native-module-path.js.map +1 -0
  131. package/dist/native-platform.d.ts +20 -0
  132. package/dist/native-platform.d.ts.map +1 -0
  133. package/dist/native-platform.js +42 -0
  134. package/dist/native-platform.js.map +1 -0
  135. package/dist/oklab.d.ts +43 -0
  136. package/dist/oklab.d.ts.map +1 -0
  137. package/dist/oklab.js +210 -0
  138. package/dist/oklab.js.map +1 -0
  139. package/dist/program-status.d.ts +23 -0
  140. package/dist/program-status.d.ts.map +1 -0
  141. package/dist/program-status.js +43 -0
  142. package/dist/program-status.js.map +1 -0
  143. package/dist/stdin-buffer.d.ts +56 -0
  144. package/dist/stdin-buffer.d.ts.map +1 -0
  145. package/dist/stdin-buffer.js +366 -0
  146. package/dist/stdin-buffer.js.map +1 -0
  147. package/dist/terminal-colors.d.ts +27 -0
  148. package/dist/terminal-colors.d.ts.map +1 -0
  149. package/dist/terminal-colors.js +64 -0
  150. package/dist/terminal-colors.js.map +1 -0
  151. package/dist/terminal-image.d.ts +123 -0
  152. package/dist/terminal-image.d.ts.map +1 -0
  153. package/dist/terminal-image.js +603 -0
  154. package/dist/terminal-image.js.map +1 -0
  155. package/dist/terminal.d.ts +143 -0
  156. package/dist/terminal.d.ts.map +1 -0
  157. package/dist/terminal.js +493 -0
  158. package/dist/terminal.js.map +1 -0
  159. package/dist/tui-alt-screen.d.ts +168 -0
  160. package/dist/tui-alt-screen.d.ts.map +1 -0
  161. package/dist/tui-alt-screen.js +1552 -0
  162. package/dist/tui-alt-screen.js.map +1 -0
  163. package/dist/tui-main-screen.d.ts +39 -0
  164. package/dist/tui-main-screen.d.ts.map +1 -0
  165. package/dist/tui-main-screen.js +595 -0
  166. package/dist/tui-main-screen.js.map +1 -0
  167. package/dist/tui.d.ts +398 -0
  168. package/dist/tui.d.ts.map +1 -0
  169. package/dist/tui.js +1096 -0
  170. package/dist/tui.js.map +1 -0
  171. package/dist/undo-stack.d.ts +17 -0
  172. package/dist/undo-stack.d.ts.map +1 -0
  173. package/dist/undo-stack.js +25 -0
  174. package/dist/undo-stack.js.map +1 -0
  175. package/dist/utils.d.ts +107 -0
  176. package/dist/utils.d.ts.map +1 -0
  177. package/dist/utils.js +1263 -0
  178. package/dist/utils.js.map +1 -0
  179. package/dist/wheel-scroll.d.ts +23 -0
  180. package/dist/wheel-scroll.d.ts.map +1 -0
  181. package/dist/wheel-scroll.js +73 -0
  182. package/dist/wheel-scroll.js.map +1 -0
  183. package/dist/word-navigation.d.ts +25 -0
  184. package/dist/word-navigation.d.ts.map +1 -0
  185. package/dist/word-navigation.js +96 -0
  186. package/dist/word-navigation.js.map +1 -0
  187. package/native/clipboard.h +208 -0
  188. package/native/darwin/README.md +22 -0
  189. package/native/darwin/build.sh +69 -0
  190. package/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
  191. package/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
  192. package/native/darwin/src/darwin-platform.m +132 -0
  193. package/native/linux/README.md +23 -0
  194. package/native/linux/build.sh +65 -0
  195. package/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  196. package/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  197. package/native/linux/src/clipboard-worker.h +70 -0
  198. package/native/linux/src/linux-platform-x11.c +274 -0
  199. package/native/napi.h +91 -0
  200. package/native/win32/README.md +31 -0
  201. package/native/win32/build.mjs +232 -0
  202. package/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  203. package/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  204. package/native/win32/src/win32-platform.c +241 -0
  205. package/package.json +61 -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
@@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AAC,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): 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,48 @@
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
+ setValue(value: string): void;
29
+ handleInput(data: string): void;
30
+ handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined;
31
+ private insertCharacter;
32
+ private handleBackspace;
33
+ private handleForwardDelete;
34
+ private deleteToLineStart;
35
+ private deleteToLineEnd;
36
+ private deleteWordBackwards;
37
+ private deleteWordForward;
38
+ private yank;
39
+ private yankPop;
40
+ private pushUndo;
41
+ private undo;
42
+ private moveWordBackwards;
43
+ private moveWordForwards;
44
+ private handlePaste;
45
+ invalidate(): void;
46
+ render(width: number): string[];
47
+ }
48
+ //# sourceMappingURL=input.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../../src/components/input.ts"],"names":[],"mappings":"AAGA,OAAO,EACN,KAAK,SAAS,EAEd,KAAK,SAAS,EAEd,KAAK,aAAa,EAClB,KAAK,mBAAmB,EACxB,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BzB,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,417 @@
1
+ import { getKeybindings } from "../keybindings.js";
2
+ import { decodeKittyPrintable } 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
+ setValue(value) {
40
+ this.value = value;
41
+ this.cursor = Math.min(this.cursor, value.length);
42
+ }
43
+ handleInput(data) {
44
+ // Handle bracketed paste mode
45
+ // Start of paste: \x1b[200~
46
+ // End of paste: \x1b[201~
47
+ // Check if we're starting a bracketed paste
48
+ if (data.includes("\x1b[200~")) {
49
+ this.isInPaste = true;
50
+ this.pasteBuffer = "";
51
+ data = data.replace("\x1b[200~", "");
52
+ }
53
+ // If we're in a paste, buffer the data
54
+ if (this.isInPaste) {
55
+ // Check if this chunk contains the end marker
56
+ this.pasteBuffer += data;
57
+ const endIndex = this.pasteBuffer.indexOf("\x1b[201~");
58
+ if (endIndex !== -1) {
59
+ // Extract the pasted content
60
+ const pasteContent = this.pasteBuffer.substring(0, endIndex);
61
+ // Process the complete paste
62
+ this.handlePaste(pasteContent);
63
+ // Reset paste state
64
+ this.isInPaste = false;
65
+ // Handle any remaining input after the paste marker
66
+ const remaining = this.pasteBuffer.substring(endIndex + 6); // 6 = length of \x1b[201~
67
+ this.pasteBuffer = "";
68
+ if (remaining) {
69
+ this.handleInput(remaining);
70
+ }
71
+ }
72
+ return;
73
+ }
74
+ const kb = getKeybindings();
75
+ // Escape/Cancel
76
+ if (kb.matches(data, "tui.select.cancel")) {
77
+ if (this.onEscape)
78
+ this.onEscape();
79
+ return;
80
+ }
81
+ // Undo
82
+ if (kb.matches(data, "tui.editor.undo")) {
83
+ this.undo();
84
+ return;
85
+ }
86
+ // Submit
87
+ if (kb.matches(data, "tui.input.submit") || data === "\n") {
88
+ if (this.onSubmit)
89
+ this.onSubmit(this.value);
90
+ return;
91
+ }
92
+ // Deletion
93
+ if (kb.matches(data, "tui.editor.deleteCharBackward")) {
94
+ this.handleBackspace();
95
+ return;
96
+ }
97
+ if (kb.matches(data, "tui.editor.deleteCharForward")) {
98
+ this.handleForwardDelete();
99
+ return;
100
+ }
101
+ if (kb.matches(data, "tui.editor.deleteWordBackward")) {
102
+ this.deleteWordBackwards();
103
+ return;
104
+ }
105
+ if (kb.matches(data, "tui.editor.deleteWordForward")) {
106
+ this.deleteWordForward();
107
+ return;
108
+ }
109
+ if (kb.matches(data, "tui.editor.deleteToLineStart")) {
110
+ this.deleteToLineStart();
111
+ return;
112
+ }
113
+ if (kb.matches(data, "tui.editor.deleteToLineEnd")) {
114
+ this.deleteToLineEnd();
115
+ return;
116
+ }
117
+ // Kill ring actions
118
+ if (kb.matches(data, "tui.editor.yank")) {
119
+ this.yank();
120
+ return;
121
+ }
122
+ if (kb.matches(data, "tui.editor.yankPop")) {
123
+ this.yankPop();
124
+ return;
125
+ }
126
+ // Cursor movement
127
+ if (kb.matches(data, "tui.editor.cursorLeft")) {
128
+ this.lastAction = null;
129
+ if (this.cursor > 0) {
130
+ const beforeCursor = this.value.slice(0, this.cursor);
131
+ const graphemes = [...segmenter.segment(beforeCursor)];
132
+ const lastGrapheme = graphemes[graphemes.length - 1];
133
+ this.cursor -= lastGrapheme ? lastGrapheme.segment.length : 1;
134
+ }
135
+ return;
136
+ }
137
+ if (kb.matches(data, "tui.editor.cursorRight")) {
138
+ this.lastAction = null;
139
+ if (this.cursor < this.value.length) {
140
+ const afterCursor = this.value.slice(this.cursor);
141
+ const graphemes = [...segmenter.segment(afterCursor)];
142
+ const firstGrapheme = graphemes[0];
143
+ this.cursor += firstGrapheme ? firstGrapheme.segment.length : 1;
144
+ }
145
+ return;
146
+ }
147
+ if (kb.matches(data, "tui.editor.cursorLineStart")) {
148
+ this.lastAction = null;
149
+ this.cursor = 0;
150
+ return;
151
+ }
152
+ if (kb.matches(data, "tui.editor.cursorLineEnd")) {
153
+ this.lastAction = null;
154
+ this.cursor = this.value.length;
155
+ return;
156
+ }
157
+ if (kb.matches(data, "tui.editor.cursorWordLeft")) {
158
+ this.moveWordBackwards();
159
+ return;
160
+ }
161
+ if (kb.matches(data, "tui.editor.cursorWordRight")) {
162
+ this.moveWordForwards();
163
+ return;
164
+ }
165
+ // Kitty CSI-u printable character (e.g. \x1b[97u for 'a').
166
+ // Terminals with Kitty protocol flag 1 (disambiguate) send CSI-u for all keys,
167
+ // including plain printable characters. Decode before the control-char check
168
+ // since CSI-u sequences contain \x1b which would be rejected.
169
+ const kittyPrintable = decodeKittyPrintable(data);
170
+ if (kittyPrintable !== undefined) {
171
+ this.insertCharacter(kittyPrintable);
172
+ return;
173
+ }
174
+ // Regular character input - accept printable characters including Unicode,
175
+ // but reject control characters (C0: 0x00-0x1F, DEL: 0x7F, C1: 0x80-0x9F)
176
+ const hasControlChars = [...data].some((ch) => {
177
+ const code = ch.charCodeAt(0);
178
+ return code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f);
179
+ });
180
+ if (!hasControlChars) {
181
+ this.insertCharacter(data);
182
+ }
183
+ }
184
+ handleMouse(event) {
185
+ if (event.type !== "press" || event.button !== "left" || event.y !== 0)
186
+ return undefined;
187
+ const visibleColumn = Math.max(0, event.x - 2);
188
+ const targetColumn = this.renderedStartColumn + visibleColumn;
189
+ let currentColumn = 0;
190
+ this.cursor = this.value.length;
191
+ for (const grapheme of segmenter.segment(this.value)) {
192
+ const nextColumn = currentColumn + visibleWidth(grapheme.segment);
193
+ if (targetColumn < nextColumn) {
194
+ this.cursor = grapheme.index;
195
+ break;
196
+ }
197
+ currentColumn = nextColumn;
198
+ }
199
+ this.lastAction = null;
200
+ return { handled: true, focus: true };
201
+ }
202
+ insertCharacter(char) {
203
+ // Undo coalescing: consecutive word chars coalesce into one undo unit
204
+ if (isWhitespaceChar(char) || this.lastAction !== "type-word") {
205
+ this.pushUndo();
206
+ }
207
+ this.lastAction = "type-word";
208
+ this.value = this.value.slice(0, this.cursor) + char + this.value.slice(this.cursor);
209
+ this.cursor += char.length;
210
+ }
211
+ handleBackspace() {
212
+ this.lastAction = null;
213
+ if (this.cursor > 0) {
214
+ this.pushUndo();
215
+ const beforeCursor = this.value.slice(0, this.cursor);
216
+ const graphemes = [...segmenter.segment(beforeCursor)];
217
+ const lastGrapheme = graphemes[graphemes.length - 1];
218
+ const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
219
+ this.value = this.value.slice(0, this.cursor - graphemeLength) + this.value.slice(this.cursor);
220
+ this.cursor -= graphemeLength;
221
+ }
222
+ }
223
+ handleForwardDelete() {
224
+ this.lastAction = null;
225
+ if (this.cursor < this.value.length) {
226
+ this.pushUndo();
227
+ const afterCursor = this.value.slice(this.cursor);
228
+ const graphemes = [...segmenter.segment(afterCursor)];
229
+ const firstGrapheme = graphemes[0];
230
+ const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
231
+ this.value = this.value.slice(0, this.cursor) + this.value.slice(this.cursor + graphemeLength);
232
+ }
233
+ }
234
+ deleteToLineStart() {
235
+ if (this.cursor === 0)
236
+ return;
237
+ this.pushUndo();
238
+ const deletedText = this.value.slice(0, this.cursor);
239
+ this.killRing.push(deletedText, { prepend: true, accumulate: this.lastAction === "kill" });
240
+ this.lastAction = "kill";
241
+ this.value = this.value.slice(this.cursor);
242
+ this.cursor = 0;
243
+ }
244
+ deleteToLineEnd() {
245
+ if (this.cursor >= this.value.length)
246
+ return;
247
+ this.pushUndo();
248
+ const deletedText = this.value.slice(this.cursor);
249
+ this.killRing.push(deletedText, { prepend: false, accumulate: this.lastAction === "kill" });
250
+ this.lastAction = "kill";
251
+ this.value = this.value.slice(0, this.cursor);
252
+ }
253
+ deleteWordBackwards() {
254
+ if (this.cursor === 0)
255
+ return;
256
+ // Save lastAction before cursor movement (moveWordBackwards resets it)
257
+ const wasKill = this.lastAction === "kill";
258
+ this.pushUndo();
259
+ const oldCursor = this.cursor;
260
+ this.moveWordBackwards();
261
+ const deleteFrom = this.cursor;
262
+ this.cursor = oldCursor;
263
+ const deletedText = this.value.slice(deleteFrom, this.cursor);
264
+ this.killRing.push(deletedText, { prepend: true, accumulate: wasKill });
265
+ this.lastAction = "kill";
266
+ this.value = this.value.slice(0, deleteFrom) + this.value.slice(this.cursor);
267
+ this.cursor = deleteFrom;
268
+ }
269
+ deleteWordForward() {
270
+ if (this.cursor >= this.value.length)
271
+ return;
272
+ // Save lastAction before cursor movement (moveWordForwards resets it)
273
+ const wasKill = this.lastAction === "kill";
274
+ this.pushUndo();
275
+ const oldCursor = this.cursor;
276
+ this.moveWordForwards();
277
+ const deleteTo = this.cursor;
278
+ this.cursor = oldCursor;
279
+ const deletedText = this.value.slice(this.cursor, deleteTo);
280
+ this.killRing.push(deletedText, { prepend: false, accumulate: wasKill });
281
+ this.lastAction = "kill";
282
+ this.value = this.value.slice(0, this.cursor) + this.value.slice(deleteTo);
283
+ }
284
+ yank() {
285
+ const text = this.killRing.peek();
286
+ if (!text)
287
+ return;
288
+ this.pushUndo();
289
+ this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);
290
+ this.cursor += text.length;
291
+ this.lastAction = "yank";
292
+ }
293
+ yankPop() {
294
+ if (this.lastAction !== "yank" || this.killRing.length <= 1)
295
+ return;
296
+ this.pushUndo();
297
+ // Delete the previously yanked text (still at end of ring before rotation)
298
+ const prevText = this.killRing.peek() || "";
299
+ this.value = this.value.slice(0, this.cursor - prevText.length) + this.value.slice(this.cursor);
300
+ this.cursor -= prevText.length;
301
+ // Rotate and insert new entry
302
+ this.killRing.rotate();
303
+ const text = this.killRing.peek() || "";
304
+ this.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);
305
+ this.cursor += text.length;
306
+ this.lastAction = "yank";
307
+ }
308
+ pushUndo() {
309
+ this.undoStack.push({ value: this.value, cursor: this.cursor });
310
+ }
311
+ undo() {
312
+ const snapshot = this.undoStack.pop();
313
+ if (!snapshot)
314
+ return;
315
+ this.value = snapshot.value;
316
+ this.cursor = snapshot.cursor;
317
+ this.lastAction = null;
318
+ }
319
+ moveWordBackwards() {
320
+ if (this.cursor === 0)
321
+ return;
322
+ this.lastAction = null;
323
+ this.cursor = findWordBackward(this.value, this.cursor);
324
+ }
325
+ moveWordForwards() {
326
+ if (this.cursor >= this.value.length)
327
+ return;
328
+ this.lastAction = null;
329
+ this.cursor = findWordForward(this.value, this.cursor);
330
+ }
331
+ handlePaste(pastedText) {
332
+ this.lastAction = null;
333
+ this.pushUndo();
334
+ // Clean the pasted text - remove newlines and carriage returns
335
+ const cleanText = pastedText.replace(/\r\n/g, "").replace(/\r/g, "").replace(/\n/g, "").replace(/\t/g, " ");
336
+ // Insert at cursor position
337
+ this.value = this.value.slice(0, this.cursor) + cleanText + this.value.slice(this.cursor);
338
+ this.cursor += cleanText.length;
339
+ }
340
+ invalidate() {
341
+ // No cached state to invalidate currently
342
+ }
343
+ render(width) {
344
+ // Calculate visible window
345
+ const availableWidth = width - visibleWidth(this.prompt);
346
+ if (availableWidth <= 0) {
347
+ return [truncateToWidth(this.prompt, width, "")];
348
+ }
349
+ if (this.value.length === 0 && this.placeholder) {
350
+ const placeholder = truncateToWidth(this.placeholder, availableWidth, "");
351
+ const graphemes = [...segmenter.segment(placeholder)];
352
+ const atCursor = graphemes[0]?.segment ?? " ";
353
+ const afterCursor = placeholder.slice(atCursor.length);
354
+ const marker = this.focused ? CURSOR_MARKER : "";
355
+ const cursorChar = renderFakeCursor(this.placeholderStyle(atCursor));
356
+ const textWithCursor = marker + cursorChar + this.placeholderStyle(afterCursor);
357
+ const padding = " ".repeat(Math.max(0, availableWidth - visibleWidth(textWithCursor)));
358
+ return [this.prompt + textWithCursor + padding];
359
+ }
360
+ let visibleText = "";
361
+ let cursorDisplay = this.cursor;
362
+ this.renderedStartColumn = 0;
363
+ const totalWidth = visibleWidth(this.value);
364
+ if (totalWidth < availableWidth) {
365
+ // Everything fits (leave room for cursor at end)
366
+ visibleText = this.value;
367
+ }
368
+ else {
369
+ // Need horizontal scrolling
370
+ // Reserve one column for cursor if it's at the end
371
+ const scrollWidth = this.cursor === this.value.length ? availableWidth - 1 : availableWidth;
372
+ const cursorCol = visibleWidth(this.value.slice(0, this.cursor));
373
+ if (scrollWidth > 0) {
374
+ const halfWidth = Math.floor(scrollWidth / 2);
375
+ let startCol = 0;
376
+ if (cursorCol < halfWidth) {
377
+ // Cursor near start
378
+ startCol = 0;
379
+ }
380
+ else if (cursorCol > totalWidth - halfWidth) {
381
+ // Cursor near end
382
+ startCol = Math.max(0, totalWidth - scrollWidth);
383
+ }
384
+ else {
385
+ // Cursor in middle
386
+ startCol = Math.max(0, cursorCol - halfWidth);
387
+ }
388
+ this.renderedStartColumn = startCol;
389
+ visibleText = sliceByColumn(this.value, startCol, scrollWidth, true);
390
+ const beforeCursor = sliceByColumn(this.value, startCol, Math.max(0, cursorCol - startCol), true);
391
+ cursorDisplay = beforeCursor.length;
392
+ }
393
+ else {
394
+ visibleText = "";
395
+ cursorDisplay = 0;
396
+ }
397
+ }
398
+ // Build line with fake cursor
399
+ // Insert cursor character at cursor position
400
+ const graphemes = [...segmenter.segment(visibleText.slice(cursorDisplay))];
401
+ const cursorGrapheme = graphemes[0];
402
+ const beforeCursor = visibleText.slice(0, cursorDisplay);
403
+ const atCursor = cursorGrapheme?.segment ?? " "; // Character at cursor, or space if at end
404
+ const afterCursor = visibleText.slice(cursorDisplay + atCursor.length);
405
+ // Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning)
406
+ const marker = this.focused ? CURSOR_MARKER : "";
407
+ // TUI draws the fake cursor in reverse video, or omits it when the hardware cursor is shown
408
+ const cursorChar = renderFakeCursor(atCursor);
409
+ const textWithCursor = beforeCursor + marker + cursorChar + afterCursor;
410
+ // Calculate visual width
411
+ const visualLength = visibleWidth(textWithCursor);
412
+ const padding = " ".repeat(Math.max(0, availableWidth - visualLength));
413
+ const line = this.prompt + textWithCursor + padding;
414
+ return [line];
415
+ }
416
+ }
417
+ //# sourceMappingURL=input.js.map