@m4ike1/ion-tui 0.1.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 (196) hide show
  1. package/README.md +114 -0
  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 +75 -0
  7. package/dist/autocomplete.d.ts.map +1 -0
  8. package/dist/autocomplete.js +749 -0
  9. package/dist/autocomplete.js.map +1 -0
  10. package/dist/components/alt-screen-flash.d.ts +13 -0
  11. package/dist/components/alt-screen-flash.d.ts.map +1 -0
  12. package/dist/components/alt-screen-flash.js +37 -0
  13. package/dist/components/alt-screen-flash.js.map +1 -0
  14. package/dist/components/box.d.ts +24 -0
  15. package/dist/components/box.d.ts.map +1 -0
  16. package/dist/components/box.js +133 -0
  17. package/dist/components/box.js.map +1 -0
  18. package/dist/components/cancellable-loader.d.ts +22 -0
  19. package/dist/components/cancellable-loader.d.ts.map +1 -0
  20. package/dist/components/cancellable-loader.js +35 -0
  21. package/dist/components/cancellable-loader.js.map +1 -0
  22. package/dist/components/editor.d.ts +266 -0
  23. package/dist/components/editor.d.ts.map +1 -0
  24. package/dist/components/editor.js +2087 -0
  25. package/dist/components/editor.js.map +1 -0
  26. package/dist/components/h-stack.d.ts +7 -0
  27. package/dist/components/h-stack.d.ts.map +1 -0
  28. package/dist/components/h-stack.js +43 -0
  29. package/dist/components/h-stack.js.map +1 -0
  30. package/dist/components/image.d.ts +28 -0
  31. package/dist/components/image.d.ts.map +1 -0
  32. package/dist/components/image.js +90 -0
  33. package/dist/components/image.js.map +1 -0
  34. package/dist/components/input.d.ts +48 -0
  35. package/dist/components/input.d.ts.map +1 -0
  36. package/dist/components/input.js +417 -0
  37. package/dist/components/input.js.map +1 -0
  38. package/dist/components/loader.d.ts +33 -0
  39. package/dist/components/loader.d.ts.map +1 -0
  40. package/dist/components/loader.js +76 -0
  41. package/dist/components/loader.js.map +1 -0
  42. package/dist/components/markdown.d.ts +103 -0
  43. package/dist/components/markdown.d.ts.map +1 -0
  44. package/dist/components/markdown.js +858 -0
  45. package/dist/components/markdown.js.map +1 -0
  46. package/dist/components/mouse-region.d.ts +12 -0
  47. package/dist/components/mouse-region.d.ts.map +1 -0
  48. package/dist/components/mouse-region.js +21 -0
  49. package/dist/components/mouse-region.js.map +1 -0
  50. package/dist/components/scroll-view.d.ts +68 -0
  51. package/dist/components/scroll-view.d.ts.map +1 -0
  52. package/dist/components/scroll-view.js +199 -0
  53. package/dist/components/scroll-view.js.map +1 -0
  54. package/dist/components/select-list.d.ts +53 -0
  55. package/dist/components/select-list.d.ts.map +1 -0
  56. package/dist/components/select-list.js +206 -0
  57. package/dist/components/select-list.js.map +1 -0
  58. package/dist/components/settings-list.d.ts +60 -0
  59. package/dist/components/settings-list.d.ts.map +1 -0
  60. package/dist/components/settings-list.js +252 -0
  61. package/dist/components/settings-list.js.map +1 -0
  62. package/dist/components/spacer.d.ts +12 -0
  63. package/dist/components/spacer.d.ts.map +1 -0
  64. package/dist/components/spacer.js +23 -0
  65. package/dist/components/spacer.js.map +1 -0
  66. package/dist/components/stack.d.ts +32 -0
  67. package/dist/components/stack.d.ts.map +1 -0
  68. package/dist/components/stack.js +111 -0
  69. package/dist/components/stack.js.map +1 -0
  70. package/dist/components/text.d.ts +19 -0
  71. package/dist/components/text.d.ts.map +1 -0
  72. package/dist/components/text.js +90 -0
  73. package/dist/components/text.js.map +1 -0
  74. package/dist/components/truncated-text.d.ts +13 -0
  75. package/dist/components/truncated-text.d.ts.map +1 -0
  76. package/dist/components/truncated-text.js +51 -0
  77. package/dist/components/truncated-text.js.map +1 -0
  78. package/dist/components/v-stack.d.ts +8 -0
  79. package/dist/components/v-stack.d.ts.map +1 -0
  80. package/dist/components/v-stack.js +26 -0
  81. package/dist/components/v-stack.js.map +1 -0
  82. package/dist/components/virtual-list.d.ts +51 -0
  83. package/dist/components/virtual-list.d.ts.map +1 -0
  84. package/dist/components/virtual-list.js +170 -0
  85. package/dist/components/virtual-list.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 +110 -0
  93. package/dist/fuzzy.js.map +1 -0
  94. package/dist/index.d.ts +34 -0
  95. package/dist/index.d.ts.map +1 -0
  96. package/dist/index.js +46 -0
  97. package/dist/index.js.map +1 -0
  98. package/dist/keybindings.d.ts +366 -0
  99. package/dist/keybindings.d.ts.map +1 -0
  100. package/dist/keybindings.js +331 -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 +1174 -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 +1285 -0
  113. package/dist/latex.js.map +1 -0
  114. package/dist/layout-node.d.ts +59 -0
  115. package/dist/layout-node.d.ts.map +1 -0
  116. package/dist/layout-node.js +12 -0
  117. package/dist/layout-node.js.map +1 -0
  118. package/dist/layout.d.ts +42 -0
  119. package/dist/layout.d.ts.map +1 -0
  120. package/dist/layout.js +404 -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 +18 -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/pty-host.d.ts +224 -0
  135. package/dist/pty-host.d.ts.map +1 -0
  136. package/dist/pty-host.js +347 -0
  137. package/dist/pty-host.js.map +1 -0
  138. package/dist/stdin-buffer.d.ts +56 -0
  139. package/dist/stdin-buffer.d.ts.map +1 -0
  140. package/dist/stdin-buffer.js +366 -0
  141. package/dist/stdin-buffer.js.map +1 -0
  142. package/dist/terminal-colors.d.ts +10 -0
  143. package/dist/terminal-colors.d.ts.map +1 -0
  144. package/dist/terminal-colors.js +59 -0
  145. package/dist/terminal-colors.js.map +1 -0
  146. package/dist/terminal-image.d.ts +118 -0
  147. package/dist/terminal-image.d.ts.map +1 -0
  148. package/dist/terminal-image.js +550 -0
  149. package/dist/terminal-image.js.map +1 -0
  150. package/dist/terminal.d.ts +123 -0
  151. package/dist/terminal.d.ts.map +1 -0
  152. package/dist/terminal.js +442 -0
  153. package/dist/terminal.js.map +1 -0
  154. package/dist/tui-alt-screen.d.ts +165 -0
  155. package/dist/tui-alt-screen.d.ts.map +1 -0
  156. package/dist/tui-alt-screen.js +1515 -0
  157. package/dist/tui-alt-screen.js.map +1 -0
  158. package/dist/tui-main-screen.d.ts +39 -0
  159. package/dist/tui-main-screen.d.ts.map +1 -0
  160. package/dist/tui-main-screen.js +594 -0
  161. package/dist/tui-main-screen.js.map +1 -0
  162. package/dist/tui.d.ts +431 -0
  163. package/dist/tui.d.ts.map +1 -0
  164. package/dist/tui.js +1142 -0
  165. package/dist/tui.js.map +1 -0
  166. package/dist/undo-stack.d.ts +17 -0
  167. package/dist/undo-stack.d.ts.map +1 -0
  168. package/dist/undo-stack.js +25 -0
  169. package/dist/undo-stack.js.map +1 -0
  170. package/dist/utils.d.ts +98 -0
  171. package/dist/utils.d.ts.map +1 -0
  172. package/dist/utils.js +1200 -0
  173. package/dist/utils.js.map +1 -0
  174. package/dist/word-navigation.d.ts +25 -0
  175. package/dist/word-navigation.d.ts.map +1 -0
  176. package/dist/word-navigation.js +96 -0
  177. package/dist/word-navigation.js.map +1 -0
  178. package/native/clipboard.h +170 -0
  179. package/native/darwin/README.md +22 -0
  180. package/native/darwin/build.sh +69 -0
  181. package/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
  182. package/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
  183. package/native/darwin/src/darwin-platform.m +93 -0
  184. package/native/linux/README.md +23 -0
  185. package/native/linux/build.sh +65 -0
  186. package/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  187. package/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  188. package/native/linux/src/clipboard-worker.h +70 -0
  189. package/native/linux/src/linux-platform-x11.c +274 -0
  190. package/native/napi.h +89 -0
  191. package/native/win32/README.md +31 -0
  192. package/native/win32/build.mjs +232 -0
  193. package/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  194. package/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  195. package/native/win32/src/win32-platform.c +241 -0
  196. package/package.json +62 -0
@@ -0,0 +1,90 @@
1
+ import { allocateImageId, getCapabilities, getCellDimensions, getImageDimensions, imageFallback, renderImage, } from "../terminal-image.js";
2
+ import { truncateToWidth } from "../utils.js";
3
+ export class Image {
4
+ base64Data;
5
+ mimeType;
6
+ dimensions;
7
+ theme;
8
+ options;
9
+ imageId;
10
+ cachedLines;
11
+ cachedWidth;
12
+ constructor(base64Data, mimeType, theme, options = {}, dimensions) {
13
+ this.base64Data = base64Data;
14
+ this.mimeType = mimeType;
15
+ this.theme = theme;
16
+ this.options = options;
17
+ this.dimensions = dimensions || getImageDimensions(base64Data, mimeType) || { widthPx: 800, heightPx: 600 };
18
+ this.imageId = options.imageId;
19
+ }
20
+ /** Get the Kitty image ID used by this image (if any). */
21
+ getImageId() {
22
+ return this.imageId;
23
+ }
24
+ invalidate() {
25
+ this.cachedLines = undefined;
26
+ this.cachedWidth = undefined;
27
+ }
28
+ render(width) {
29
+ if (this.cachedLines && this.cachedWidth === width) {
30
+ return this.cachedLines;
31
+ }
32
+ const maxWidth = Math.max(1, Math.min(width - 2, this.options.maxWidthCells ?? 60));
33
+ const cellDimensions = getCellDimensions();
34
+ const defaultMaxHeight = Math.max(1, Math.ceil((maxWidth * cellDimensions.widthPx) / cellDimensions.heightPx));
35
+ const maxHeight = this.options.maxHeightCells ?? defaultMaxHeight;
36
+ const caps = getCapabilities();
37
+ let lines;
38
+ if (caps.images) {
39
+ if (caps.images === "kitty" && this.imageId === undefined) {
40
+ this.imageId = allocateImageId();
41
+ }
42
+ const result = renderImage(this.base64Data, this.dimensions, {
43
+ maxWidthCells: maxWidth,
44
+ maxHeightCells: maxHeight,
45
+ imageId: this.imageId,
46
+ moveCursor: false,
47
+ });
48
+ if (result) {
49
+ // Store the image ID for later cleanup
50
+ if (result.imageId) {
51
+ this.imageId = result.imageId;
52
+ }
53
+ if (caps.images === "kitty") {
54
+ // For Kitty: C=1 prevents cursor movement.
55
+ // Don't need the cursor movement.
56
+ lines = [result.sequence];
57
+ // Return `rows` lines so TUI accounts for image height.
58
+ for (let i = 0; i < result.rows - 1; i++) {
59
+ lines.push("");
60
+ }
61
+ }
62
+ else {
63
+ // Return `rows` lines so TUI accounts for image height.
64
+ // First (rows-1) lines are empty and cleared before the image is drawn.
65
+ // Last line: move cursor back up, draw the image, then move back down
66
+ // so TUI cursor accounting stays inside the scroll area.
67
+ lines = [];
68
+ for (let i = 0; i < result.rows - 1; i++) {
69
+ lines.push("");
70
+ }
71
+ const rowOffset = result.rows - 1;
72
+ const moveUp = rowOffset > 0 ? `\x1b[${rowOffset}A` : "";
73
+ lines.push(moveUp + result.sequence);
74
+ }
75
+ }
76
+ else {
77
+ const fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename);
78
+ lines = [truncateToWidth(this.theme.fallbackColor(fallback), width)];
79
+ }
80
+ }
81
+ else {
82
+ const fallback = imageFallback(this.mimeType, this.dimensions, this.options.filename);
83
+ lines = [truncateToWidth(this.theme.fallbackColor(fallback), width)];
84
+ }
85
+ this.cachedLines = lines;
86
+ this.cachedWidth = width;
87
+ return lines;
88
+ }
89
+ }
90
+ //# sourceMappingURL=image.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"image.js","sourceRoot":"","sources":["../../src/components/image.ts"],"names":[],"mappings":"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","sourcesContent":["import {\n\tallocateImageId,\n\tgetCapabilities,\n\tgetCellDimensions,\n\tgetImageDimensions,\n\ttype ImageDimensions,\n\timageFallback,\n\trenderImage,\n} from \"../terminal-image.ts\";\nimport type { Component } from \"../tui.ts\";\nimport { truncateToWidth } from \"../utils.ts\";\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\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 lines: string[];\n\n\t\tif (caps.images) {\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(this.base64Data, this.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,EAAE,KAAK,SAAS,EAAiB,KAAK,SAAS,EAAE,KAAK,aAAa,EAAE,KAAK,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAYxH,MAAM,WAAW,YAAY;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gBAAgB,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;CAC5C;AAED;;GAEG;AACH,qBAAa,KAAM,YAAW,SAAS,EAAE,SAAS;IACjD,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA2B;IAC5D,OAAO,CAAC,mBAAmB,CAAK;IACzB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;IAE7B,0DAA0D;IAC1D,OAAO,EAAE,OAAO,CAAS;IAGzB,OAAO,CAAC,WAAW,CAAc;IACjC,OAAO,CAAC,SAAS,CAAkB;IAGnC,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,UAAU,CAA8C;IAGhE,OAAO,CAAC,SAAS,CAA+B;IAEhD,YAAY,OAAO,GAAE,YAAiB,EAIrC;IAED,QAAQ,IAAI,MAAM,CAEjB;IAED,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAG5B;IAED,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAmK9B;IAED,WAAW,CAAC,KAAK,EAAE,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","sourcesContent":["import { getKeybindings } from \"../keybindings.ts\";\nimport { decodeKittyPrintable } from \"../keys.ts\";\nimport { KillRing } from \"../kill-ring.ts\";\nimport { type Component, CURSOR_MARKER, type Focusable, type TuiMouseEvent, type TuiMouseEventResult } from \"../tui.ts\";\nimport { UndoStack } from \"../undo-stack.ts\";\nimport { getGraphemeSegmenter, isWhitespaceChar, sliceByColumn, truncateToWidth, visibleWidth } from \"../utils.ts\";\nimport { findWordBackward, findWordForward } from \"../word-navigation.ts\";\n\nconst segmenter = getGraphemeSegmenter();\n\ninterface InputState {\n\tvalue: string;\n\tcursor: number;\n}\n\nexport interface InputOptions {\n\tprompt?: string;\n\tplaceholder?: string;\n\tplaceholderStyle?: (text: string) => string;\n}\n\n/**\n * Input component - single-line text input with horizontal scrolling\n */\nexport class Input implements Component, Focusable {\n\tprivate value: string = \"\";\n\tprivate cursor: number = 0; // Cursor position in the value\n\tprivate readonly prompt: string;\n\tprivate readonly placeholder: string;\n\tprivate readonly placeholderStyle: (text: string) => string;\n\tprivate renderedStartColumn = 0;\n\tpublic onSubmit?: (value: string) => void;\n\tpublic onEscape?: () => void;\n\n\t/** Focusable interface - set by TUI when focus changes */\n\tfocused: boolean = false;\n\n\t// Bracketed paste mode buffering\n\tprivate pasteBuffer: string = \"\";\n\tprivate isInPaste: boolean = false;\n\n\t// Kill ring for Emacs-style kill/yank operations\n\tprivate killRing = new KillRing();\n\tprivate lastAction: \"kill\" | \"yank\" | \"type-word\" | null = null;\n\n\t// Undo support\n\tprivate undoStack = new UndoStack<InputState>();\n\n\tconstructor(options: InputOptions = {}) {\n\t\tthis.prompt = options.prompt ?? \"> \";\n\t\tthis.placeholder = options.placeholder ?? \"\";\n\t\tthis.placeholderStyle = options.placeholderStyle ?? ((text) => text);\n\t}\n\n\tgetValue(): string {\n\t\treturn this.value;\n\t}\n\n\tsetValue(value: string): void {\n\t\tthis.value = value;\n\t\tthis.cursor = Math.min(this.cursor, value.length);\n\t}\n\n\thandleInput(data: string): void {\n\t\t// Handle bracketed paste mode\n\t\t// Start of paste: \\x1b[200~\n\t\t// End of paste: \\x1b[201~\n\n\t\t// Check if we're starting a bracketed paste\n\t\tif (data.includes(\"\\x1b[200~\")) {\n\t\t\tthis.isInPaste = true;\n\t\t\tthis.pasteBuffer = \"\";\n\t\t\tdata = data.replace(\"\\x1b[200~\", \"\");\n\t\t}\n\n\t\t// If we're in a paste, buffer the data\n\t\tif (this.isInPaste) {\n\t\t\t// Check if this chunk contains the end marker\n\t\t\tthis.pasteBuffer += data;\n\n\t\t\tconst endIndex = this.pasteBuffer.indexOf(\"\\x1b[201~\");\n\t\t\tif (endIndex !== -1) {\n\t\t\t\t// Extract the pasted content\n\t\t\t\tconst pasteContent = this.pasteBuffer.substring(0, endIndex);\n\n\t\t\t\t// Process the complete paste\n\t\t\t\tthis.handlePaste(pasteContent);\n\n\t\t\t\t// Reset paste state\n\t\t\t\tthis.isInPaste = false;\n\n\t\t\t\t// Handle any remaining input after the paste marker\n\t\t\t\tconst remaining = this.pasteBuffer.substring(endIndex + 6); // 6 = length of \\x1b[201~\n\t\t\t\tthis.pasteBuffer = \"\";\n\t\t\t\tif (remaining) {\n\t\t\t\t\tthis.handleInput(remaining);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tconst kb = getKeybindings();\n\n\t\t// Escape/Cancel\n\t\tif (kb.matches(data, \"tui.select.cancel\")) {\n\t\t\tif (this.onEscape) this.onEscape();\n\t\t\treturn;\n\t\t}\n\n\t\t// Undo\n\t\tif (kb.matches(data, \"tui.editor.undo\")) {\n\t\t\tthis.undo();\n\t\t\treturn;\n\t\t}\n\n\t\t// Submit\n\t\tif (kb.matches(data, \"tui.input.submit\") || data === \"\\n\") {\n\t\t\tif (this.onSubmit) this.onSubmit(this.value);\n\t\t\treturn;\n\t\t}\n\n\t\t// Deletion\n\t\tif (kb.matches(data, \"tui.editor.deleteCharBackward\")) {\n\t\t\tthis.handleBackspace();\n\t\t\treturn;\n\t\t}\n\n\t\tif (kb.matches(data, \"tui.editor.deleteCharForward\")) {\n\t\t\tthis.handleForwardDelete();\n\t\t\treturn;\n\t\t}\n\n\t\tif (kb.matches(data, \"tui.editor.deleteWordBackward\")) {\n\t\t\tthis.deleteWordBackwards();\n\t\t\treturn;\n\t\t}\n\n\t\tif (kb.matches(data, \"tui.editor.deleteWordForward\")) {\n\t\t\tthis.deleteWordForward();\n\t\t\treturn;\n\t\t}\n\n\t\tif (kb.matches(data, \"tui.editor.deleteToLineStart\")) {\n\t\t\tthis.deleteToLineStart();\n\t\t\treturn;\n\t\t}\n\n\t\tif (kb.matches(data, \"tui.editor.deleteToLineEnd\")) {\n\t\t\tthis.deleteToLineEnd();\n\t\t\treturn;\n\t\t}\n\n\t\t// Kill ring actions\n\t\tif (kb.matches(data, \"tui.editor.yank\")) {\n\t\t\tthis.yank();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.yankPop\")) {\n\t\t\tthis.yankPop();\n\t\t\treturn;\n\t\t}\n\n\t\t// Cursor movement\n\t\tif (kb.matches(data, \"tui.editor.cursorLeft\")) {\n\t\t\tthis.lastAction = null;\n\t\t\tif (this.cursor > 0) {\n\t\t\t\tconst beforeCursor = this.value.slice(0, this.cursor);\n\t\t\t\tconst graphemes = [...segmenter.segment(beforeCursor)];\n\t\t\t\tconst lastGrapheme = graphemes[graphemes.length - 1];\n\t\t\t\tthis.cursor -= lastGrapheme ? lastGrapheme.segment.length : 1;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tif (kb.matches(data, \"tui.editor.cursorRight\")) {\n\t\t\tthis.lastAction = null;\n\t\t\tif (this.cursor < this.value.length) {\n\t\t\t\tconst afterCursor = this.value.slice(this.cursor);\n\t\t\t\tconst graphemes = [...segmenter.segment(afterCursor)];\n\t\t\t\tconst firstGrapheme = graphemes[0];\n\t\t\t\tthis.cursor += firstGrapheme ? firstGrapheme.segment.length : 1;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tif (kb.matches(data, \"tui.editor.cursorLineStart\")) {\n\t\t\tthis.lastAction = null;\n\t\t\tthis.cursor = 0;\n\t\t\treturn;\n\t\t}\n\n\t\tif (kb.matches(data, \"tui.editor.cursorLineEnd\")) {\n\t\t\tthis.lastAction = null;\n\t\t\tthis.cursor = this.value.length;\n\t\t\treturn;\n\t\t}\n\n\t\tif (kb.matches(data, \"tui.editor.cursorWordLeft\")) {\n\t\t\tthis.moveWordBackwards();\n\t\t\treturn;\n\t\t}\n\n\t\tif (kb.matches(data, \"tui.editor.cursorWordRight\")) {\n\t\t\tthis.moveWordForwards();\n\t\t\treturn;\n\t\t}\n\n\t\t// Kitty CSI-u printable character (e.g. \\x1b[97u for 'a').\n\t\t// Terminals with Kitty protocol flag 1 (disambiguate) send CSI-u for all keys,\n\t\t// including plain printable characters. Decode before the control-char check\n\t\t// since CSI-u sequences contain \\x1b which would be rejected.\n\t\tconst kittyPrintable = decodeKittyPrintable(data);\n\t\tif (kittyPrintable !== undefined) {\n\t\t\tthis.insertCharacter(kittyPrintable);\n\t\t\treturn;\n\t\t}\n\n\t\t// Regular character input - accept printable characters including Unicode,\n\t\t// but reject control characters (C0: 0x00-0x1F, DEL: 0x7F, C1: 0x80-0x9F)\n\t\tconst hasControlChars = [...data].some((ch) => {\n\t\t\tconst code = ch.charCodeAt(0);\n\t\t\treturn code < 32 || code === 0x7f || (code >= 0x80 && code <= 0x9f);\n\t\t});\n\t\tif (!hasControlChars) {\n\t\t\tthis.insertCharacter(data);\n\t\t}\n\t}\n\n\thandleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {\n\t\tif (event.type !== \"press\" || event.button !== \"left\" || event.y !== 0) return undefined;\n\t\tconst visibleColumn = Math.max(0, event.x - 2);\n\t\tconst targetColumn = this.renderedStartColumn + visibleColumn;\n\t\tlet currentColumn = 0;\n\t\tthis.cursor = this.value.length;\n\t\tfor (const grapheme of segmenter.segment(this.value)) {\n\t\t\tconst nextColumn = currentColumn + visibleWidth(grapheme.segment);\n\t\t\tif (targetColumn < nextColumn) {\n\t\t\t\tthis.cursor = grapheme.index;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcurrentColumn = nextColumn;\n\t\t}\n\t\tthis.lastAction = null;\n\t\treturn { handled: true, focus: true };\n\t}\n\n\tprivate insertCharacter(char: string): void {\n\t\t// Undo coalescing: consecutive word chars coalesce into one undo unit\n\t\tif (isWhitespaceChar(char) || this.lastAction !== \"type-word\") {\n\t\t\tthis.pushUndo();\n\t\t}\n\t\tthis.lastAction = \"type-word\";\n\n\t\tthis.value = this.value.slice(0, this.cursor) + char + this.value.slice(this.cursor);\n\t\tthis.cursor += char.length;\n\t}\n\n\tprivate handleBackspace(): void {\n\t\tthis.lastAction = null;\n\t\tif (this.cursor > 0) {\n\t\t\tthis.pushUndo();\n\t\t\tconst beforeCursor = this.value.slice(0, this.cursor);\n\t\t\tconst graphemes = [...segmenter.segment(beforeCursor)];\n\t\t\tconst lastGrapheme = graphemes[graphemes.length - 1];\n\t\t\tconst graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;\n\t\t\tthis.value = this.value.slice(0, this.cursor - graphemeLength) + this.value.slice(this.cursor);\n\t\t\tthis.cursor -= graphemeLength;\n\t\t}\n\t}\n\n\tprivate handleForwardDelete(): void {\n\t\tthis.lastAction = null;\n\t\tif (this.cursor < this.value.length) {\n\t\t\tthis.pushUndo();\n\t\t\tconst afterCursor = this.value.slice(this.cursor);\n\t\t\tconst graphemes = [...segmenter.segment(afterCursor)];\n\t\t\tconst firstGrapheme = graphemes[0];\n\t\t\tconst graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;\n\t\t\tthis.value = this.value.slice(0, this.cursor) + this.value.slice(this.cursor + graphemeLength);\n\t\t}\n\t}\n\n\tprivate deleteToLineStart(): void {\n\t\tif (this.cursor === 0) return;\n\t\tthis.pushUndo();\n\t\tconst deletedText = this.value.slice(0, this.cursor);\n\t\tthis.killRing.push(deletedText, { prepend: true, accumulate: this.lastAction === \"kill\" });\n\t\tthis.lastAction = \"kill\";\n\t\tthis.value = this.value.slice(this.cursor);\n\t\tthis.cursor = 0;\n\t}\n\n\tprivate deleteToLineEnd(): void {\n\t\tif (this.cursor >= this.value.length) return;\n\t\tthis.pushUndo();\n\t\tconst deletedText = this.value.slice(this.cursor);\n\t\tthis.killRing.push(deletedText, { prepend: false, accumulate: this.lastAction === \"kill\" });\n\t\tthis.lastAction = \"kill\";\n\t\tthis.value = this.value.slice(0, this.cursor);\n\t}\n\n\tprivate deleteWordBackwards(): void {\n\t\tif (this.cursor === 0) return;\n\n\t\t// Save lastAction before cursor movement (moveWordBackwards resets it)\n\t\tconst wasKill = this.lastAction === \"kill\";\n\n\t\tthis.pushUndo();\n\n\t\tconst oldCursor = this.cursor;\n\t\tthis.moveWordBackwards();\n\t\tconst deleteFrom = this.cursor;\n\t\tthis.cursor = oldCursor;\n\n\t\tconst deletedText = this.value.slice(deleteFrom, this.cursor);\n\t\tthis.killRing.push(deletedText, { prepend: true, accumulate: wasKill });\n\t\tthis.lastAction = \"kill\";\n\n\t\tthis.value = this.value.slice(0, deleteFrom) + this.value.slice(this.cursor);\n\t\tthis.cursor = deleteFrom;\n\t}\n\n\tprivate deleteWordForward(): void {\n\t\tif (this.cursor >= this.value.length) return;\n\n\t\t// Save lastAction before cursor movement (moveWordForwards resets it)\n\t\tconst wasKill = this.lastAction === \"kill\";\n\n\t\tthis.pushUndo();\n\n\t\tconst oldCursor = this.cursor;\n\t\tthis.moveWordForwards();\n\t\tconst deleteTo = this.cursor;\n\t\tthis.cursor = oldCursor;\n\n\t\tconst deletedText = this.value.slice(this.cursor, deleteTo);\n\t\tthis.killRing.push(deletedText, { prepend: false, accumulate: wasKill });\n\t\tthis.lastAction = \"kill\";\n\n\t\tthis.value = this.value.slice(0, this.cursor) + this.value.slice(deleteTo);\n\t}\n\n\tprivate yank(): void {\n\t\tconst text = this.killRing.peek();\n\t\tif (!text) return;\n\n\t\tthis.pushUndo();\n\n\t\tthis.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);\n\t\tthis.cursor += text.length;\n\t\tthis.lastAction = \"yank\";\n\t}\n\n\tprivate yankPop(): void {\n\t\tif (this.lastAction !== \"yank\" || this.killRing.length <= 1) return;\n\n\t\tthis.pushUndo();\n\n\t\t// Delete the previously yanked text (still at end of ring before rotation)\n\t\tconst prevText = this.killRing.peek() || \"\";\n\t\tthis.value = this.value.slice(0, this.cursor - prevText.length) + this.value.slice(this.cursor);\n\t\tthis.cursor -= prevText.length;\n\n\t\t// Rotate and insert new entry\n\t\tthis.killRing.rotate();\n\t\tconst text = this.killRing.peek() || \"\";\n\t\tthis.value = this.value.slice(0, this.cursor) + text + this.value.slice(this.cursor);\n\t\tthis.cursor += text.length;\n\t\tthis.lastAction = \"yank\";\n\t}\n\n\tprivate pushUndo(): void {\n\t\tthis.undoStack.push({ value: this.value, cursor: this.cursor });\n\t}\n\n\tprivate undo(): void {\n\t\tconst snapshot = this.undoStack.pop();\n\t\tif (!snapshot) return;\n\t\tthis.value = snapshot.value;\n\t\tthis.cursor = snapshot.cursor;\n\t\tthis.lastAction = null;\n\t}\n\n\tprivate moveWordBackwards(): void {\n\t\tif (this.cursor === 0) return;\n\t\tthis.lastAction = null;\n\t\tthis.cursor = findWordBackward(this.value, this.cursor);\n\t}\n\n\tprivate moveWordForwards(): void {\n\t\tif (this.cursor >= this.value.length) return;\n\t\tthis.lastAction = null;\n\t\tthis.cursor = findWordForward(this.value, this.cursor);\n\t}\n\n\tprivate handlePaste(pastedText: string): void {\n\t\tthis.lastAction = null;\n\t\tthis.pushUndo();\n\n\t\t// Clean the pasted text - remove newlines and carriage returns\n\t\tconst cleanText = pastedText.replace(/\\r\\n/g, \"\").replace(/\\r/g, \"\").replace(/\\n/g, \"\").replace(/\\t/g, \" \");\n\n\t\t// Insert at cursor position\n\t\tthis.value = this.value.slice(0, this.cursor) + cleanText + this.value.slice(this.cursor);\n\t\tthis.cursor += cleanText.length;\n\t}\n\n\tinvalidate(): void {\n\t\t// No cached state to invalidate currently\n\t}\n\n\trender(width: number): string[] {\n\t\t// Calculate visible window\n\t\tconst availableWidth = width - visibleWidth(this.prompt);\n\n\t\tif (availableWidth <= 0) {\n\t\t\treturn [truncateToWidth(this.prompt, width, \"\")];\n\t\t}\n\n\t\tif (this.value.length === 0 && this.placeholder) {\n\t\t\tconst placeholder = truncateToWidth(this.placeholder, availableWidth, \"\");\n\t\t\tconst graphemes = [...segmenter.segment(placeholder)];\n\t\t\tconst atCursor = graphemes[0]?.segment ?? \" \";\n\t\t\tconst afterCursor = placeholder.slice(atCursor.length);\n\t\t\tconst marker = this.focused ? CURSOR_MARKER : \"\";\n\t\t\tconst cursorChar = `\\x1b[7m${this.placeholderStyle(atCursor)}\\x1b[27m`;\n\t\t\tconst textWithCursor = marker + cursorChar + this.placeholderStyle(afterCursor);\n\t\t\tconst padding = \" \".repeat(Math.max(0, availableWidth - visibleWidth(textWithCursor)));\n\t\t\treturn [this.prompt + textWithCursor + padding];\n\t\t}\n\n\t\tlet visibleText = \"\";\n\t\tlet cursorDisplay = this.cursor;\n\t\tthis.renderedStartColumn = 0;\n\t\tconst totalWidth = visibleWidth(this.value);\n\n\t\tif (totalWidth < availableWidth) {\n\t\t\t// Everything fits (leave room for cursor at end)\n\t\t\tvisibleText = this.value;\n\t\t} else {\n\t\t\t// Need horizontal scrolling\n\t\t\t// Reserve one column for cursor if it's at the end\n\t\t\tconst scrollWidth = this.cursor === this.value.length ? availableWidth - 1 : availableWidth;\n\t\t\tconst cursorCol = visibleWidth(this.value.slice(0, this.cursor));\n\n\t\t\tif (scrollWidth > 0) {\n\t\t\t\tconst halfWidth = Math.floor(scrollWidth / 2);\n\t\t\t\tlet startCol = 0;\n\n\t\t\t\tif (cursorCol < halfWidth) {\n\t\t\t\t\t// Cursor near start\n\t\t\t\t\tstartCol = 0;\n\t\t\t\t} else if (cursorCol > totalWidth - halfWidth) {\n\t\t\t\t\t// Cursor near end\n\t\t\t\t\tstartCol = Math.max(0, totalWidth - scrollWidth);\n\t\t\t\t} else {\n\t\t\t\t\t// Cursor in middle\n\t\t\t\t\tstartCol = Math.max(0, cursorCol - halfWidth);\n\t\t\t\t}\n\n\t\t\t\tthis.renderedStartColumn = startCol;\n\t\t\t\tvisibleText = sliceByColumn(this.value, startCol, scrollWidth, true);\n\t\t\t\tconst beforeCursor = sliceByColumn(this.value, startCol, Math.max(0, cursorCol - startCol), true);\n\t\t\t\tcursorDisplay = beforeCursor.length;\n\t\t\t} else {\n\t\t\t\tvisibleText = \"\";\n\t\t\t\tcursorDisplay = 0;\n\t\t\t}\n\t\t}\n\n\t\t// Build line with fake cursor\n\t\t// Insert cursor character at cursor position\n\t\tconst graphemes = [...segmenter.segment(visibleText.slice(cursorDisplay))];\n\t\tconst cursorGrapheme = graphemes[0];\n\n\t\tconst beforeCursor = visibleText.slice(0, cursorDisplay);\n\t\tconst atCursor = cursorGrapheme?.segment ?? \" \"; // Character at cursor, or space if at end\n\t\tconst afterCursor = visibleText.slice(cursorDisplay + atCursor.length);\n\n\t\t// Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning)\n\t\tconst marker = this.focused ? CURSOR_MARKER : \"\";\n\n\t\t// Use inverse video to show cursor\n\t\tconst cursorChar = `\\x1b[7m${atCursor}\\x1b[27m`; // ESC[7m = reverse video, ESC[27m = normal\n\t\tconst textWithCursor = beforeCursor + marker + cursorChar + afterCursor;\n\n\t\t// Calculate visual width\n\t\tconst visualLength = visibleWidth(textWithCursor);\n\t\tconst padding = \" \".repeat(Math.max(0, availableWidth - visualLength));\n\t\tconst line = this.prompt + textWithCursor + padding;\n\n\t\treturn [line];\n\t}\n}\n"]}
@@ -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 } 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 = `\x1b[7m${this.placeholderStyle(atCursor)}\x1b[27m`;
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
+ // Use inverse video to show cursor
408
+ const cursorChar = `\x1b[7m${atCursor}\x1b[27m`; // ESC[7m = reverse video, ESC[27m = normal
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