@google/gemini-cli 0.1.1 → 0.1.3-rc.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 (391) hide show
  1. package/README.md +7 -6
  2. package/dist/.last_build +0 -0
  3. package/dist/google-gemini-cli-0.1.3.tgz +0 -0
  4. package/dist/index.d.ts +7 -0
  5. package/dist/index.js +20 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/package.json +77 -0
  8. package/dist/src/config/auth.d.ts +6 -0
  9. package/dist/src/config/auth.js +38 -0
  10. package/dist/src/config/auth.js.map +1 -0
  11. package/dist/src/config/config.d.ts +14 -0
  12. package/dist/src/config/config.js +225 -0
  13. package/dist/src/config/config.js.map +1 -0
  14. package/dist/src/config/extension.d.ts +19 -0
  15. package/dist/src/config/extension.js +81 -0
  16. package/dist/src/config/extension.js.map +1 -0
  17. package/dist/src/config/sandboxConfig.d.ts +13 -0
  18. package/dist/src/config/sandboxConfig.js +75 -0
  19. package/dist/src/config/sandboxConfig.js.map +1 -0
  20. package/dist/src/config/settings.d.ts +69 -0
  21. package/dist/src/config/settings.js +165 -0
  22. package/dist/src/config/settings.js.map +1 -0
  23. package/dist/src/gemini.d.ts +6 -0
  24. package/dist/src/gemini.js +212 -0
  25. package/dist/src/gemini.js.map +1 -0
  26. package/dist/src/gemini.test.d.ts +6 -0
  27. package/dist/src/gemini.test.js +122 -0
  28. package/dist/src/gemini.test.js.map +1 -0
  29. package/dist/src/generated/git-commit.d.ts +6 -0
  30. package/dist/src/generated/git-commit.js +9 -0
  31. package/dist/src/generated/git-commit.js.map +1 -0
  32. package/dist/src/nonInteractiveCli.d.ts +7 -0
  33. package/dist/src/nonInteractiveCli.js +112 -0
  34. package/dist/src/nonInteractiveCli.js.map +1 -0
  35. package/dist/src/ui/App.d.ts +14 -0
  36. package/dist/src/ui/App.js +398 -0
  37. package/dist/src/ui/App.js.map +1 -0
  38. package/dist/src/ui/App.test.d.ts +6 -0
  39. package/dist/src/ui/App.test.js +280 -0
  40. package/dist/src/ui/App.test.js.map +1 -0
  41. package/dist/src/ui/colors.d.ts +7 -0
  42. package/dist/src/ui/colors.js +48 -0
  43. package/dist/src/ui/colors.js.map +1 -0
  44. package/dist/src/ui/components/AboutBox.d.ts +14 -0
  45. package/dist/src/ui/components/AboutBox.js +6 -0
  46. package/dist/src/ui/components/AboutBox.js.map +1 -0
  47. package/dist/src/ui/components/AsciiArt.d.ts +7 -0
  48. package/dist/src/ui/components/AsciiArt.js +26 -0
  49. package/dist/src/ui/components/AsciiArt.js.map +1 -0
  50. package/dist/src/ui/components/AuthDialog.d.ts +15 -0
  51. package/dist/src/ui/components/AuthDialog.js +67 -0
  52. package/dist/src/ui/components/AuthDialog.js.map +1 -0
  53. package/dist/src/ui/components/AuthDialog.test.d.ts +6 -0
  54. package/dist/src/ui/components/AuthDialog.test.js +69 -0
  55. package/dist/src/ui/components/AuthDialog.test.js.map +1 -0
  56. package/dist/src/ui/components/AuthInProgress.d.ts +11 -0
  57. package/dist/src/ui/components/AuthInProgress.js +27 -0
  58. package/dist/src/ui/components/AuthInProgress.js.map +1 -0
  59. package/dist/src/ui/components/AutoAcceptIndicator.d.ts +12 -0
  60. package/dist/src/ui/components/AutoAcceptIndicator.js +26 -0
  61. package/dist/src/ui/components/AutoAcceptIndicator.js.map +1 -0
  62. package/dist/src/ui/components/ConsolePatcher.d.ts +12 -0
  63. package/dist/src/ui/components/ConsolePatcher.js +40 -0
  64. package/dist/src/ui/components/ConsolePatcher.js.map +1 -0
  65. package/dist/src/ui/components/ConsoleSummaryDisplay.d.ts +11 -0
  66. package/dist/src/ui/components/ConsoleSummaryDisplay.js +11 -0
  67. package/dist/src/ui/components/ConsoleSummaryDisplay.js.map +1 -0
  68. package/dist/src/ui/components/ContextSummaryDisplay.d.ts +15 -0
  69. package/dist/src/ui/components/ContextSummaryDisplay.js +41 -0
  70. package/dist/src/ui/components/ContextSummaryDisplay.js.map +1 -0
  71. package/dist/src/ui/components/DetailedMessagesDisplay.d.ts +14 -0
  72. package/dist/src/ui/components/DetailedMessagesDisplay.js +34 -0
  73. package/dist/src/ui/components/DetailedMessagesDisplay.js.map +1 -0
  74. package/dist/src/ui/components/EditorSettingsDialog.d.ts +15 -0
  75. package/dist/src/ui/components/EditorSettingsDialog.js +73 -0
  76. package/dist/src/ui/components/EditorSettingsDialog.js.map +1 -0
  77. package/dist/src/ui/components/Footer.d.ts +22 -0
  78. package/dist/src/ui/components/Footer.js +13 -0
  79. package/dist/src/ui/components/Footer.js.map +1 -0
  80. package/dist/src/ui/components/GeminiRespondingSpinner.d.ts +17 -0
  81. package/dist/src/ui/components/GeminiRespondingSpinner.js +16 -0
  82. package/dist/src/ui/components/GeminiRespondingSpinner.js.map +1 -0
  83. package/dist/src/ui/components/Header.d.ts +12 -0
  84. package/dist/src/ui/components/Header.js +20 -0
  85. package/dist/src/ui/components/Header.js.map +1 -0
  86. package/dist/src/ui/components/Help.d.ts +12 -0
  87. package/dist/src/ui/components/Help.js +7 -0
  88. package/dist/src/ui/components/Help.js.map +1 -0
  89. package/dist/src/ui/components/HistoryItemDisplay.d.ts +18 -0
  90. package/dist/src/ui/components/HistoryItemDisplay.js +15 -0
  91. package/dist/src/ui/components/HistoryItemDisplay.js.map +1 -0
  92. package/dist/src/ui/components/HistoryItemDisplay.test.d.ts +6 -0
  93. package/dist/src/ui/components/HistoryItemDisplay.test.js +85 -0
  94. package/dist/src/ui/components/HistoryItemDisplay.test.js.map +1 -0
  95. package/dist/src/ui/components/InputPrompt.d.ts +24 -0
  96. package/dist/src/ui/components/InputPrompt.js +313 -0
  97. package/dist/src/ui/components/InputPrompt.js.map +1 -0
  98. package/dist/src/ui/components/InputPrompt.test.d.ts +6 -0
  99. package/dist/src/ui/components/InputPrompt.test.js +154 -0
  100. package/dist/src/ui/components/InputPrompt.test.js.map +1 -0
  101. package/dist/src/ui/components/LoadingIndicator.d.ts +15 -0
  102. package/dist/src/ui/components/LoadingIndicator.js +19 -0
  103. package/dist/src/ui/components/LoadingIndicator.js.map +1 -0
  104. package/dist/src/ui/components/LoadingIndicator.test.d.ts +6 -0
  105. package/dist/src/ui/components/LoadingIndicator.test.js +141 -0
  106. package/dist/src/ui/components/LoadingIndicator.test.js.map +1 -0
  107. package/dist/src/ui/components/MemoryUsageDisplay.d.ts +7 -0
  108. package/dist/src/ui/components/MemoryUsageDisplay.js +27 -0
  109. package/dist/src/ui/components/MemoryUsageDisplay.js.map +1 -0
  110. package/dist/src/ui/components/SessionSummaryDisplay.d.ts +13 -0
  111. package/dist/src/ui/components/SessionSummaryDisplay.js +20 -0
  112. package/dist/src/ui/components/SessionSummaryDisplay.js.map +1 -0
  113. package/dist/src/ui/components/SessionSummaryDisplay.test.d.ts +6 -0
  114. package/dist/src/ui/components/SessionSummaryDisplay.test.js +41 -0
  115. package/dist/src/ui/components/SessionSummaryDisplay.test.js.map +1 -0
  116. package/dist/src/ui/components/ShellModeIndicator.d.ts +7 -0
  117. package/dist/src/ui/components/ShellModeIndicator.js +5 -0
  118. package/dist/src/ui/components/ShellModeIndicator.js.map +1 -0
  119. package/dist/src/ui/components/ShowMoreLines.d.ts +10 -0
  120. package/dist/src/ui/components/ShowMoreLines.js +24 -0
  121. package/dist/src/ui/components/ShowMoreLines.js.map +1 -0
  122. package/dist/src/ui/components/Stats.d.ts +39 -0
  123. package/dist/src/ui/components/Stats.js +23 -0
  124. package/dist/src/ui/components/Stats.js.map +1 -0
  125. package/dist/src/ui/components/Stats.test.d.ts +6 -0
  126. package/dist/src/ui/components/Stats.test.js +57 -0
  127. package/dist/src/ui/components/Stats.test.js.map +1 -0
  128. package/dist/src/ui/components/StatsDisplay.d.ts +14 -0
  129. package/dist/src/ui/components/StatsDisplay.js +28 -0
  130. package/dist/src/ui/components/StatsDisplay.js.map +1 -0
  131. package/dist/src/ui/components/StatsDisplay.test.d.ts +6 -0
  132. package/dist/src/ui/components/StatsDisplay.test.js +51 -0
  133. package/dist/src/ui/components/StatsDisplay.test.js.map +1 -0
  134. package/dist/src/ui/components/SuggestionsDisplay.d.ts +21 -0
  135. package/dist/src/ui/components/SuggestionsDisplay.js +32 -0
  136. package/dist/src/ui/components/SuggestionsDisplay.js.map +1 -0
  137. package/dist/src/ui/components/ThemeDialog.d.ts +19 -0
  138. package/dist/src/ui/components/ThemeDialog.js +126 -0
  139. package/dist/src/ui/components/ThemeDialog.js.map +1 -0
  140. package/dist/src/ui/components/Tips.d.ts +12 -0
  141. package/dist/src/ui/components/Tips.js +8 -0
  142. package/dist/src/ui/components/Tips.js.map +1 -0
  143. package/dist/src/ui/components/UpdateNotification.d.ts +10 -0
  144. package/dist/src/ui/components/UpdateNotification.js +10 -0
  145. package/dist/src/ui/components/UpdateNotification.js.map +1 -0
  146. package/dist/src/ui/components/messages/CompressionMessage.d.ts +11 -0
  147. package/dist/src/ui/components/messages/CompressionMessage.js +16 -0
  148. package/dist/src/ui/components/messages/CompressionMessage.js.map +1 -0
  149. package/dist/src/ui/components/messages/DiffRenderer.d.ts +15 -0
  150. package/dist/src/ui/components/messages/DiffRenderer.js +212 -0
  151. package/dist/src/ui/components/messages/DiffRenderer.js.map +1 -0
  152. package/dist/src/ui/components/messages/DiffRenderer.test.d.ts +6 -0
  153. package/dist/src/ui/components/messages/DiffRenderer.test.js +239 -0
  154. package/dist/src/ui/components/messages/DiffRenderer.test.js.map +1 -0
  155. package/dist/src/ui/components/messages/ErrorMessage.d.ts +11 -0
  156. package/dist/src/ui/components/messages/ErrorMessage.js +9 -0
  157. package/dist/src/ui/components/messages/ErrorMessage.js.map +1 -0
  158. package/dist/src/ui/components/messages/GeminiMessage.d.ts +14 -0
  159. package/dist/src/ui/components/messages/GeminiMessage.js +10 -0
  160. package/dist/src/ui/components/messages/GeminiMessage.js.map +1 -0
  161. package/dist/src/ui/components/messages/GeminiMessageContent.d.ts +14 -0
  162. package/dist/src/ui/components/messages/GeminiMessageContent.js +15 -0
  163. package/dist/src/ui/components/messages/GeminiMessageContent.js.map +1 -0
  164. package/dist/src/ui/components/messages/InfoMessage.d.ts +11 -0
  165. package/dist/src/ui/components/messages/InfoMessage.js +9 -0
  166. package/dist/src/ui/components/messages/InfoMessage.js.map +1 -0
  167. package/dist/src/ui/components/messages/ToolConfirmationMessage.d.ts +15 -0
  168. package/dist/src/ui/components/messages/ToolConfirmationMessage.js +111 -0
  169. package/dist/src/ui/components/messages/ToolConfirmationMessage.js.map +1 -0
  170. package/dist/src/ui/components/messages/ToolConfirmationMessage.test.d.ts +6 -0
  171. package/dist/src/ui/components/messages/ToolConfirmationMessage.test.js +37 -0
  172. package/dist/src/ui/components/messages/ToolConfirmationMessage.test.js.map +1 -0
  173. package/dist/src/ui/components/messages/ToolGroupMessage.d.ts +18 -0
  174. package/dist/src/ui/components/messages/ToolGroupMessage.js +53 -0
  175. package/dist/src/ui/components/messages/ToolGroupMessage.js.map +1 -0
  176. package/dist/src/ui/components/messages/ToolMessage.d.ts +15 -0
  177. package/dist/src/ui/components/messages/ToolMessage.js +61 -0
  178. package/dist/src/ui/components/messages/ToolMessage.js.map +1 -0
  179. package/dist/src/ui/components/messages/ToolMessage.test.d.ts +6 -0
  180. package/dist/src/ui/components/messages/ToolMessage.test.js +116 -0
  181. package/dist/src/ui/components/messages/ToolMessage.test.js.map +1 -0
  182. package/dist/src/ui/components/messages/UserMessage.d.ts +11 -0
  183. package/dist/src/ui/components/messages/UserMessage.js +9 -0
  184. package/dist/src/ui/components/messages/UserMessage.js.map +1 -0
  185. package/dist/src/ui/components/messages/UserShellMessage.d.ts +11 -0
  186. package/dist/src/ui/components/messages/UserShellMessage.js +9 -0
  187. package/dist/src/ui/components/messages/UserShellMessage.js.map +1 -0
  188. package/dist/src/ui/components/shared/MaxSizedBox.d.ts +61 -0
  189. package/dist/src/ui/components/shared/MaxSizedBox.js +383 -0
  190. package/dist/src/ui/components/shared/MaxSizedBox.js.map +1 -0
  191. package/dist/src/ui/components/shared/MaxSizedBox.test.d.ts +6 -0
  192. package/dist/src/ui/components/shared/MaxSizedBox.test.js +134 -0
  193. package/dist/src/ui/components/shared/MaxSizedBox.test.js.map +1 -0
  194. package/dist/src/ui/components/shared/RadioButtonSelect.d.ts +41 -0
  195. package/dist/src/ui/components/shared/RadioButtonSelect.js +52 -0
  196. package/dist/src/ui/components/shared/RadioButtonSelect.js.map +1 -0
  197. package/dist/src/ui/components/shared/text-buffer.d.ts +121 -0
  198. package/dist/src/ui/components/shared/text-buffer.js +1147 -0
  199. package/dist/src/ui/components/shared/text-buffer.js.map +1 -0
  200. package/dist/src/ui/constants.d.ts +8 -0
  201. package/dist/src/ui/constants.js +12 -0
  202. package/dist/src/ui/constants.js.map +1 -0
  203. package/dist/src/ui/contexts/OverflowContext.d.ts +19 -0
  204. package/dist/src/ui/contexts/OverflowContext.js +43 -0
  205. package/dist/src/ui/contexts/OverflowContext.js.map +1 -0
  206. package/dist/src/ui/contexts/SessionContext.d.ts +35 -0
  207. package/dist/src/ui/contexts/SessionContext.js +132 -0
  208. package/dist/src/ui/contexts/SessionContext.js.map +1 -0
  209. package/dist/src/ui/contexts/SessionContext.test.d.ts +6 -0
  210. package/dist/src/ui/contexts/SessionContext.test.js +151 -0
  211. package/dist/src/ui/contexts/SessionContext.test.js.map +1 -0
  212. package/dist/src/ui/contexts/StreamingContext.d.ts +9 -0
  213. package/dist/src/ui/contexts/StreamingContext.js +15 -0
  214. package/dist/src/ui/contexts/StreamingContext.js.map +1 -0
  215. package/dist/src/ui/editors/editorSettingsManager.d.ts +19 -0
  216. package/dist/src/ui/editors/editorSettingsManager.js +50 -0
  217. package/dist/src/ui/editors/editorSettingsManager.js.map +1 -0
  218. package/dist/src/ui/hooks/atCommandProcessor.d.ts +31 -0
  219. package/dist/src/ui/hooks/atCommandProcessor.js +312 -0
  220. package/dist/src/ui/hooks/atCommandProcessor.js.map +1 -0
  221. package/dist/src/ui/hooks/shellCommandProcessor.d.ts +16 -0
  222. package/dist/src/ui/hooks/shellCommandProcessor.js +261 -0
  223. package/dist/src/ui/hooks/shellCommandProcessor.js.map +1 -0
  224. package/dist/src/ui/hooks/slashCommandProcessor.d.ts +31 -0
  225. package/dist/src/ui/hooks/slashCommandProcessor.js +922 -0
  226. package/dist/src/ui/hooks/slashCommandProcessor.js.map +1 -0
  227. package/dist/src/ui/hooks/useAuthCommand.d.ts +15 -0
  228. package/dist/src/ui/hooks/useAuthCommand.js +65 -0
  229. package/dist/src/ui/hooks/useAuthCommand.js.map +1 -0
  230. package/dist/src/ui/hooks/useAutoAcceptIndicator.d.ts +10 -0
  231. package/dist/src/ui/hooks/useAutoAcceptIndicator.js +37 -0
  232. package/dist/src/ui/hooks/useAutoAcceptIndicator.js.map +1 -0
  233. package/dist/src/ui/hooks/useCompletion.d.ts +21 -0
  234. package/dist/src/ui/hooks/useCompletion.js +321 -0
  235. package/dist/src/ui/hooks/useCompletion.js.map +1 -0
  236. package/dist/src/ui/hooks/useConsoleMessages.d.ts +12 -0
  237. package/dist/src/ui/hooks/useConsoleMessages.js +59 -0
  238. package/dist/src/ui/hooks/useConsoleMessages.js.map +1 -0
  239. package/dist/src/ui/hooks/useEditorSettings.d.ts +16 -0
  240. package/dist/src/ui/hooks/useEditorSettings.js +43 -0
  241. package/dist/src/ui/hooks/useEditorSettings.js.map +1 -0
  242. package/dist/src/ui/hooks/useGeminiStream.d.ts +23 -0
  243. package/dist/src/ui/hooks/useGeminiStream.js +552 -0
  244. package/dist/src/ui/hooks/useGeminiStream.js.map +1 -0
  245. package/dist/src/ui/hooks/useGeminiStream.test.d.ts +6 -0
  246. package/dist/src/ui/hooks/useGeminiStream.test.js +823 -0
  247. package/dist/src/ui/hooks/useGeminiStream.test.js.map +1 -0
  248. package/dist/src/ui/hooks/useGitBranchName.d.ts +6 -0
  249. package/dist/src/ui/hooks/useGitBranchName.js +61 -0
  250. package/dist/src/ui/hooks/useGitBranchName.js.map +1 -0
  251. package/dist/src/ui/hooks/useHistoryManager.d.ts +22 -0
  252. package/dist/src/ui/hooks/useHistoryManager.js +72 -0
  253. package/dist/src/ui/hooks/useHistoryManager.js.map +1 -0
  254. package/dist/src/ui/hooks/useInputHistory.d.ts +19 -0
  255. package/dist/src/ui/hooks/useInputHistory.js +84 -0
  256. package/dist/src/ui/hooks/useInputHistory.js.map +1 -0
  257. package/dist/src/ui/hooks/useLoadingIndicator.d.ts +10 -0
  258. package/dist/src/ui/hooks/useLoadingIndicator.js +44 -0
  259. package/dist/src/ui/hooks/useLoadingIndicator.js.map +1 -0
  260. package/dist/src/ui/hooks/useLogger.d.ts +10 -0
  261. package/dist/src/ui/hooks/useLogger.js +29 -0
  262. package/dist/src/ui/hooks/useLogger.js.map +1 -0
  263. package/dist/src/ui/hooks/usePhraseCycler.d.ts +14 -0
  264. package/dist/src/ui/hooks/usePhraseCycler.js +189 -0
  265. package/dist/src/ui/hooks/usePhraseCycler.js.map +1 -0
  266. package/dist/src/ui/hooks/useReactToolScheduler.d.ts +33 -0
  267. package/dist/src/ui/hooks/useReactToolScheduler.js +186 -0
  268. package/dist/src/ui/hooks/useReactToolScheduler.js.map +1 -0
  269. package/dist/src/ui/hooks/useRefreshMemoryCommand.d.ts +6 -0
  270. package/dist/src/ui/hooks/useRefreshMemoryCommand.js +7 -0
  271. package/dist/src/ui/hooks/useRefreshMemoryCommand.js.map +1 -0
  272. package/dist/src/ui/hooks/useShellHistory.d.ts +11 -0
  273. package/dist/src/ui/hooks/useShellHistory.js +89 -0
  274. package/dist/src/ui/hooks/useShellHistory.js.map +1 -0
  275. package/dist/src/ui/hooks/useShowMemoryCommand.d.ts +9 -0
  276. package/dist/src/ui/hooks/useShowMemoryCommand.js +58 -0
  277. package/dist/src/ui/hooks/useShowMemoryCommand.js.map +1 -0
  278. package/dist/src/ui/hooks/useStateAndRef.d.ts +7 -0
  279. package/dist/src/ui/hooks/useStateAndRef.js +26 -0
  280. package/dist/src/ui/hooks/useStateAndRef.js.map +1 -0
  281. package/dist/src/ui/hooks/useTerminalSize.d.ts +9 -0
  282. package/dist/src/ui/hooks/useTerminalSize.js +27 -0
  283. package/dist/src/ui/hooks/useTerminalSize.js.map +1 -0
  284. package/dist/src/ui/hooks/useThemeCommand.d.ts +15 -0
  285. package/dist/src/ui/hooks/useThemeCommand.js +78 -0
  286. package/dist/src/ui/hooks/useThemeCommand.js.map +1 -0
  287. package/dist/src/ui/hooks/useTimer.d.ts +12 -0
  288. package/dist/src/ui/hooks/useTimer.js +58 -0
  289. package/dist/src/ui/hooks/useTimer.js.map +1 -0
  290. package/dist/src/ui/themes/ansi-light.d.ts +7 -0
  291. package/dist/src/ui/themes/ansi-light.js +139 -0
  292. package/dist/src/ui/themes/ansi-light.js.map +1 -0
  293. package/dist/src/ui/themes/ansi.d.ts +7 -0
  294. package/dist/src/ui/themes/ansi.js +149 -0
  295. package/dist/src/ui/themes/ansi.js.map +1 -0
  296. package/dist/src/ui/themes/atom-one-dark.d.ts +7 -0
  297. package/dist/src/ui/themes/atom-one-dark.js +136 -0
  298. package/dist/src/ui/themes/atom-one-dark.js.map +1 -0
  299. package/dist/src/ui/themes/ayu-light.d.ts +7 -0
  300. package/dist/src/ui/themes/ayu-light.js +128 -0
  301. package/dist/src/ui/themes/ayu-light.js.map +1 -0
  302. package/dist/src/ui/themes/ayu.d.ts +7 -0
  303. package/dist/src/ui/themes/ayu.js +102 -0
  304. package/dist/src/ui/themes/ayu.js.map +1 -0
  305. package/dist/src/ui/themes/default-light.d.ts +7 -0
  306. package/dist/src/ui/themes/default-light.js +100 -0
  307. package/dist/src/ui/themes/default-light.js.map +1 -0
  308. package/dist/src/ui/themes/default.d.ts +7 -0
  309. package/dist/src/ui/themes/default.js +143 -0
  310. package/dist/src/ui/themes/default.js.map +1 -0
  311. package/dist/src/ui/themes/dracula.d.ts +7 -0
  312. package/dist/src/ui/themes/dracula.js +113 -0
  313. package/dist/src/ui/themes/dracula.js.map +1 -0
  314. package/dist/src/ui/themes/github-dark.d.ts +7 -0
  315. package/dist/src/ui/themes/github-dark.js +136 -0
  316. package/dist/src/ui/themes/github-dark.js.map +1 -0
  317. package/dist/src/ui/themes/github-light.d.ts +7 -0
  318. package/dist/src/ui/themes/github-light.js +138 -0
  319. package/dist/src/ui/themes/github-light.js.map +1 -0
  320. package/dist/src/ui/themes/googlecode.d.ts +7 -0
  321. package/dist/src/ui/themes/googlecode.js +135 -0
  322. package/dist/src/ui/themes/googlecode.js.map +1 -0
  323. package/dist/src/ui/themes/no-color.d.ts +7 -0
  324. package/dist/src/ui/themes/no-color.js +84 -0
  325. package/dist/src/ui/themes/no-color.js.map +1 -0
  326. package/dist/src/ui/themes/theme-manager.d.ts +33 -0
  327. package/dist/src/ui/themes/theme-manager.js +107 -0
  328. package/dist/src/ui/themes/theme-manager.js.map +1 -0
  329. package/dist/src/ui/themes/theme.d.ts +67 -0
  330. package/dist/src/ui/themes/theme.js +304 -0
  331. package/dist/src/ui/themes/theme.js.map +1 -0
  332. package/dist/src/ui/themes/xcode.d.ts +7 -0
  333. package/dist/src/ui/themes/xcode.js +143 -0
  334. package/dist/src/ui/themes/xcode.js.map +1 -0
  335. package/dist/src/ui/types.d.ts +149 -0
  336. package/dist/src/ui/types.js +41 -0
  337. package/dist/src/ui/types.js.map +1 -0
  338. package/dist/src/ui/utils/CodeColorizer.d.ts +14 -0
  339. package/dist/src/ui/utils/CodeColorizer.js +97 -0
  340. package/dist/src/ui/utils/CodeColorizer.js.map +1 -0
  341. package/dist/src/ui/utils/MarkdownDisplay.d.ts +14 -0
  342. package/dist/src/ui/utils/MarkdownDisplay.js +221 -0
  343. package/dist/src/ui/utils/MarkdownDisplay.js.map +1 -0
  344. package/dist/src/ui/utils/commandUtils.d.ts +22 -0
  345. package/dist/src/ui/utils/commandUtils.js +25 -0
  346. package/dist/src/ui/utils/commandUtils.js.map +1 -0
  347. package/dist/src/ui/utils/errorParsing.d.ts +15 -0
  348. package/dist/src/ui/utils/errorParsing.js +80 -0
  349. package/dist/src/ui/utils/errorParsing.js.map +1 -0
  350. package/dist/src/ui/utils/formatters.d.ts +13 -0
  351. package/dist/src/ui/utils/formatters.js +56 -0
  352. package/dist/src/ui/utils/formatters.js.map +1 -0
  353. package/dist/src/ui/utils/markdownUtilities.d.ts +6 -0
  354. package/dist/src/ui/utils/markdownUtilities.js +110 -0
  355. package/dist/src/ui/utils/markdownUtilities.js.map +1 -0
  356. package/dist/src/ui/utils/textUtils.d.ts +22 -0
  357. package/dist/src/ui/utils/textUtils.js +58 -0
  358. package/dist/src/ui/utils/textUtils.js.map +1 -0
  359. package/dist/src/ui/utils/updateCheck.d.ts +6 -0
  360. package/dist/src/ui/utils/updateCheck.js +34 -0
  361. package/dist/src/ui/utils/updateCheck.js.map +1 -0
  362. package/dist/src/utils/cleanup.d.ts +6 -0
  363. package/dist/src/utils/cleanup.js +19 -0
  364. package/dist/src/utils/cleanup.js.map +1 -0
  365. package/dist/src/utils/package.d.ts +12 -0
  366. package/dist/src/utils/package.js +24 -0
  367. package/dist/src/utils/package.js.map +1 -0
  368. package/dist/src/utils/readStdin.d.ts +6 -0
  369. package/dist/src/utils/readStdin.js +34 -0
  370. package/dist/src/utils/readStdin.js.map +1 -0
  371. package/dist/src/utils/sandbox.d.ts +7 -0
  372. package/dist/src/utils/sandbox.js +680 -0
  373. package/dist/src/utils/sandbox.js.map +1 -0
  374. package/dist/src/utils/startupWarnings.d.ts +6 -0
  375. package/dist/src/utils/startupWarnings.js +40 -0
  376. package/dist/src/utils/startupWarnings.js.map +1 -0
  377. package/dist/src/utils/version.d.ts +6 -0
  378. package/dist/src/utils/version.js +11 -0
  379. package/dist/src/utils/version.js.map +1 -0
  380. package/dist/tsconfig.tsbuildinfo +1 -0
  381. package/package.json +63 -71
  382. package/LICENSE +0 -202
  383. package/bundle/gemini.js +0 -257368
  384. package/bundle/shell.json +0 -18
  385. package/bundle/shell.md +0 -16
  386. /package/{bundle → dist/src/utils}/sandbox-macos-permissive-closed.sb +0 -0
  387. /package/{bundle → dist/src/utils}/sandbox-macos-permissive-open.sb +0 -0
  388. /package/{bundle → dist/src/utils}/sandbox-macos-permissive-proxied.sb +0 -0
  389. /package/{bundle → dist/src/utils}/sandbox-macos-restrictive-closed.sb +0 -0
  390. /package/{bundle → dist/src/utils}/sandbox-macos-restrictive-open.sb +0 -0
  391. /package/{bundle → dist/src/utils}/sandbox-macos-restrictive-proxied.sb +0 -0
@@ -0,0 +1,1147 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import stripAnsi from 'strip-ansi';
7
+ import { spawnSync } from 'child_process';
8
+ import fs from 'fs';
9
+ import os from 'os';
10
+ import pathMod from 'path';
11
+ import { useState, useCallback, useEffect, useMemo } from 'react';
12
+ import stringWidth from 'string-width';
13
+ import { unescapePath } from '@google/gemini-cli-core';
14
+ import { toCodePoints, cpLen, cpSlice } from '../../utils/textUtils.js';
15
+ // Simple helper for word‑wise ops.
16
+ function isWordChar(ch) {
17
+ if (ch === undefined) {
18
+ return false;
19
+ }
20
+ return !/[\s,.;!?]/.test(ch);
21
+ }
22
+ /**
23
+ * Strip characters that can break terminal rendering.
24
+ *
25
+ * Strip ANSI escape codes and control characters except for line breaks.
26
+ * Control characters such as delete break terminal UI rendering.
27
+ */
28
+ function stripUnsafeCharacters(str) {
29
+ const stripped = stripAnsi(str);
30
+ return toCodePoints(stripAnsi(stripped))
31
+ .filter((char) => {
32
+ if (char.length > 1)
33
+ return false;
34
+ const code = char.codePointAt(0);
35
+ if (code === undefined) {
36
+ return false;
37
+ }
38
+ const isUnsafe = code === 127 || (code <= 31 && code !== 13 && code !== 10);
39
+ return !isUnsafe;
40
+ })
41
+ .join('');
42
+ }
43
+ function clamp(v, min, max) {
44
+ return v < min ? min : v > max ? max : v;
45
+ }
46
+ /* -------------------------------------------------------------------------
47
+ * Debug helper – enable verbose logging by setting env var TEXTBUFFER_DEBUG=1
48
+ * ---------------------------------------------------------------------- */
49
+ // Enable verbose logging only when requested via env var.
50
+ const DEBUG = process.env['TEXTBUFFER_DEBUG'] === '1' ||
51
+ process.env['TEXTBUFFER_DEBUG'] === 'true';
52
+ function dbg(...args) {
53
+ if (DEBUG) {
54
+ console.log('[TextBuffer]', ...args);
55
+ }
56
+ }
57
+ function calculateInitialCursorPosition(initialLines, offset) {
58
+ let remainingChars = offset;
59
+ let row = 0;
60
+ while (row < initialLines.length) {
61
+ const lineLength = cpLen(initialLines[row]);
62
+ // Add 1 for the newline character (except for the last line)
63
+ const totalCharsInLineAndNewline = lineLength + (row < initialLines.length - 1 ? 1 : 0);
64
+ if (remainingChars <= lineLength) {
65
+ // Cursor is on this line
66
+ return [row, remainingChars];
67
+ }
68
+ remainingChars -= totalCharsInLineAndNewline;
69
+ row++;
70
+ }
71
+ // Offset is beyond the text, place cursor at the end of the last line
72
+ if (initialLines.length > 0) {
73
+ const lastRow = initialLines.length - 1;
74
+ return [lastRow, cpLen(initialLines[lastRow])];
75
+ }
76
+ return [0, 0]; // Default for empty text
77
+ }
78
+ export function offsetToLogicalPos(text, offset) {
79
+ let row = 0;
80
+ let col = 0;
81
+ let currentOffset = 0;
82
+ if (offset === 0)
83
+ return [0, 0];
84
+ const lines = text.split('\n');
85
+ for (let i = 0; i < lines.length; i++) {
86
+ const line = lines[i];
87
+ const lineLength = cpLen(line);
88
+ const lineLengthWithNewline = lineLength + (i < lines.length - 1 ? 1 : 0);
89
+ if (offset <= currentOffset + lineLength) {
90
+ // Check against lineLength first
91
+ row = i;
92
+ col = offset - currentOffset;
93
+ return [row, col];
94
+ }
95
+ else if (offset <= currentOffset + lineLengthWithNewline) {
96
+ // Check if offset is the newline itself
97
+ row = i;
98
+ col = lineLength; // Position cursor at the end of the current line content
99
+ // If the offset IS the newline, and it's not the last line, advance to next line, col 0
100
+ if (offset === currentOffset + lineLengthWithNewline &&
101
+ i < lines.length - 1) {
102
+ return [i + 1, 0];
103
+ }
104
+ return [row, col]; // Otherwise, it's at the end of the current line content
105
+ }
106
+ currentOffset += lineLengthWithNewline;
107
+ }
108
+ // If offset is beyond the text length, place cursor at the end of the last line
109
+ // or [0,0] if text is empty
110
+ if (lines.length > 0) {
111
+ row = lines.length - 1;
112
+ col = cpLen(lines[row]);
113
+ }
114
+ else {
115
+ row = 0;
116
+ col = 0;
117
+ }
118
+ return [row, col];
119
+ }
120
+ // Helper to calculate visual lines and map cursor positions
121
+ function calculateVisualLayout(logicalLines, logicalCursor, viewportWidth) {
122
+ const visualLines = [];
123
+ const logicalToVisualMap = [];
124
+ const visualToLogicalMap = [];
125
+ let currentVisualCursor = [0, 0];
126
+ logicalLines.forEach((logLine, logIndex) => {
127
+ logicalToVisualMap[logIndex] = [];
128
+ if (logLine.length === 0) {
129
+ // Handle empty logical line
130
+ logicalToVisualMap[logIndex].push([visualLines.length, 0]);
131
+ visualToLogicalMap.push([logIndex, 0]);
132
+ visualLines.push('');
133
+ if (logIndex === logicalCursor[0] && logicalCursor[1] === 0) {
134
+ currentVisualCursor = [visualLines.length - 1, 0];
135
+ }
136
+ }
137
+ else {
138
+ // Non-empty logical line
139
+ let currentPosInLogLine = 0; // Tracks position within the current logical line (code point index)
140
+ const codePointsInLogLine = toCodePoints(logLine);
141
+ while (currentPosInLogLine < codePointsInLogLine.length) {
142
+ let currentChunk = '';
143
+ let currentChunkVisualWidth = 0;
144
+ let numCodePointsInChunk = 0;
145
+ let lastWordBreakPoint = -1; // Index in codePointsInLogLine for word break
146
+ let numCodePointsAtLastWordBreak = 0;
147
+ // Iterate through code points to build the current visual line (chunk)
148
+ for (let i = currentPosInLogLine; i < codePointsInLogLine.length; i++) {
149
+ const char = codePointsInLogLine[i];
150
+ const charVisualWidth = stringWidth(char);
151
+ if (currentChunkVisualWidth + charVisualWidth > viewportWidth) {
152
+ // Character would exceed viewport width
153
+ if (lastWordBreakPoint !== -1 &&
154
+ numCodePointsAtLastWordBreak > 0 &&
155
+ currentPosInLogLine + numCodePointsAtLastWordBreak < i) {
156
+ // We have a valid word break point to use, and it's not the start of the current segment
157
+ currentChunk = codePointsInLogLine
158
+ .slice(currentPosInLogLine, currentPosInLogLine + numCodePointsAtLastWordBreak)
159
+ .join('');
160
+ numCodePointsInChunk = numCodePointsAtLastWordBreak;
161
+ }
162
+ else {
163
+ // No word break, or word break is at the start of this potential chunk, or word break leads to empty chunk.
164
+ // Hard break: take characters up to viewportWidth, or just the current char if it alone is too wide.
165
+ if (numCodePointsInChunk === 0 &&
166
+ charVisualWidth > viewportWidth) {
167
+ // Single character is wider than viewport, take it anyway
168
+ currentChunk = char;
169
+ numCodePointsInChunk = 1;
170
+ }
171
+ else if (numCodePointsInChunk === 0 &&
172
+ charVisualWidth <= viewportWidth) {
173
+ // This case should ideally be caught by the next iteration if the char fits.
174
+ // If it doesn't fit (because currentChunkVisualWidth was already > 0 from a previous char that filled the line),
175
+ // then numCodePointsInChunk would not be 0.
176
+ // This branch means the current char *itself* doesn't fit an empty line, which is handled by the above.
177
+ // If we are here, it means the loop should break and the current chunk (which is empty) is finalized.
178
+ }
179
+ }
180
+ break; // Break from inner loop to finalize this chunk
181
+ }
182
+ currentChunk += char;
183
+ currentChunkVisualWidth += charVisualWidth;
184
+ numCodePointsInChunk++;
185
+ // Check for word break opportunity (space)
186
+ if (char === ' ') {
187
+ lastWordBreakPoint = i; // Store code point index of the space
188
+ // Store the state *before* adding the space, if we decide to break here.
189
+ numCodePointsAtLastWordBreak = numCodePointsInChunk - 1; // Chars *before* the space
190
+ }
191
+ }
192
+ // If the inner loop completed without breaking (i.e., remaining text fits)
193
+ // or if the loop broke but numCodePointsInChunk is still 0 (e.g. first char too wide for empty line)
194
+ if (numCodePointsInChunk === 0 &&
195
+ currentPosInLogLine < codePointsInLogLine.length) {
196
+ // This can happen if the very first character considered for a new visual line is wider than the viewport.
197
+ // In this case, we take that single character.
198
+ const firstChar = codePointsInLogLine[currentPosInLogLine];
199
+ currentChunk = firstChar;
200
+ numCodePointsInChunk = 1; // Ensure we advance
201
+ }
202
+ // If after everything, numCodePointsInChunk is still 0 but we haven't processed the whole logical line,
203
+ // it implies an issue, like viewportWidth being 0 or less. Avoid infinite loop.
204
+ if (numCodePointsInChunk === 0 &&
205
+ currentPosInLogLine < codePointsInLogLine.length) {
206
+ // Force advance by one character to prevent infinite loop if something went wrong
207
+ currentChunk = codePointsInLogLine[currentPosInLogLine];
208
+ numCodePointsInChunk = 1;
209
+ }
210
+ logicalToVisualMap[logIndex].push([
211
+ visualLines.length,
212
+ currentPosInLogLine,
213
+ ]);
214
+ visualToLogicalMap.push([logIndex, currentPosInLogLine]);
215
+ visualLines.push(currentChunk);
216
+ // Cursor mapping logic
217
+ // Note: currentPosInLogLine here is the start of the currentChunk within the logical line.
218
+ if (logIndex === logicalCursor[0]) {
219
+ const cursorLogCol = logicalCursor[1]; // This is a code point index
220
+ if (cursorLogCol >= currentPosInLogLine &&
221
+ cursorLogCol < currentPosInLogLine + numCodePointsInChunk // Cursor is within this chunk
222
+ ) {
223
+ currentVisualCursor = [
224
+ visualLines.length - 1,
225
+ cursorLogCol - currentPosInLogLine, // Visual col is also code point index within visual line
226
+ ];
227
+ }
228
+ else if (cursorLogCol === currentPosInLogLine + numCodePointsInChunk &&
229
+ numCodePointsInChunk > 0) {
230
+ // Cursor is exactly at the end of this non-empty chunk
231
+ currentVisualCursor = [
232
+ visualLines.length - 1,
233
+ numCodePointsInChunk,
234
+ ];
235
+ }
236
+ }
237
+ const logicalStartOfThisChunk = currentPosInLogLine;
238
+ currentPosInLogLine += numCodePointsInChunk;
239
+ // If the chunk processed did not consume the entire logical line,
240
+ // and the character immediately following the chunk is a space,
241
+ // advance past this space as it acted as a delimiter for word wrapping.
242
+ if (logicalStartOfThisChunk + numCodePointsInChunk <
243
+ codePointsInLogLine.length &&
244
+ currentPosInLogLine < codePointsInLogLine.length && // Redundant if previous is true, but safe
245
+ codePointsInLogLine[currentPosInLogLine] === ' ') {
246
+ currentPosInLogLine++;
247
+ }
248
+ }
249
+ // After all chunks of a non-empty logical line are processed,
250
+ // if the cursor is at the very end of this logical line, update visual cursor.
251
+ if (logIndex === logicalCursor[0] &&
252
+ logicalCursor[1] === codePointsInLogLine.length // Cursor at end of logical line
253
+ ) {
254
+ const lastVisualLineIdx = visualLines.length - 1;
255
+ if (lastVisualLineIdx >= 0 &&
256
+ visualLines[lastVisualLineIdx] !== undefined) {
257
+ currentVisualCursor = [
258
+ lastVisualLineIdx,
259
+ cpLen(visualLines[lastVisualLineIdx]), // Cursor at end of last visual line for this logical line
260
+ ];
261
+ }
262
+ }
263
+ }
264
+ });
265
+ // If the entire logical text was empty, ensure there's one empty visual line.
266
+ if (logicalLines.length === 0 ||
267
+ (logicalLines.length === 1 && logicalLines[0] === '')) {
268
+ if (visualLines.length === 0) {
269
+ visualLines.push('');
270
+ if (!logicalToVisualMap[0])
271
+ logicalToVisualMap[0] = [];
272
+ logicalToVisualMap[0].push([0, 0]);
273
+ visualToLogicalMap.push([0, 0]);
274
+ }
275
+ currentVisualCursor = [0, 0];
276
+ }
277
+ // Handle cursor at the very end of the text (after all processing)
278
+ // This case might be covered by the loop end condition now, but kept for safety.
279
+ else if (logicalCursor[0] === logicalLines.length - 1 &&
280
+ logicalCursor[1] === cpLen(logicalLines[logicalLines.length - 1]) &&
281
+ visualLines.length > 0) {
282
+ const lastVisLineIdx = visualLines.length - 1;
283
+ currentVisualCursor = [lastVisLineIdx, cpLen(visualLines[lastVisLineIdx])];
284
+ }
285
+ return {
286
+ visualLines,
287
+ visualCursor: currentVisualCursor,
288
+ logicalToVisualMap,
289
+ visualToLogicalMap,
290
+ };
291
+ }
292
+ export function useTextBuffer({ initialText = '', initialCursorOffset = 0, viewport, stdin, setRawMode, onChange, isValidPath, }) {
293
+ const [lines, setLines] = useState(() => {
294
+ const l = initialText.split('\n');
295
+ return l.length === 0 ? [''] : l;
296
+ });
297
+ const [[initialCursorRow, initialCursorCol]] = useState(() => calculateInitialCursorPosition(lines, initialCursorOffset));
298
+ const [cursorRow, setCursorRow] = useState(initialCursorRow);
299
+ const [cursorCol, setCursorCol] = useState(initialCursorCol);
300
+ const [preferredCol, setPreferredCol] = useState(null); // Visual preferred col
301
+ const [undoStack, setUndoStack] = useState([]);
302
+ const [redoStack, setRedoStack] = useState([]);
303
+ const historyLimit = 100;
304
+ const [clipboard, setClipboard] = useState(null);
305
+ const [selectionAnchor, setSelectionAnchor] = useState(null); // Logical selection
306
+ // Visual state
307
+ const [visualLines, setVisualLines] = useState(['']);
308
+ const [visualCursor, setVisualCursor] = useState([0, 0]);
309
+ const [visualScrollRow, setVisualScrollRow] = useState(0);
310
+ const [logicalToVisualMap, setLogicalToVisualMap] = useState([]);
311
+ const [visualToLogicalMap, setVisualToLogicalMap] = useState([]);
312
+ const currentLine = useCallback((r) => lines[r] ?? '', [lines]);
313
+ const currentLineLen = useCallback((r) => cpLen(currentLine(r)), [currentLine]);
314
+ // Recalculate visual layout whenever logical lines or viewport width changes
315
+ useEffect(() => {
316
+ const layout = calculateVisualLayout(lines, [cursorRow, cursorCol], viewport.width);
317
+ setVisualLines(layout.visualLines);
318
+ setVisualCursor(layout.visualCursor);
319
+ setLogicalToVisualMap(layout.logicalToVisualMap);
320
+ setVisualToLogicalMap(layout.visualToLogicalMap);
321
+ }, [lines, cursorRow, cursorCol, viewport.width]);
322
+ // Update visual scroll (vertical)
323
+ useEffect(() => {
324
+ const { height } = viewport;
325
+ let newVisualScrollRow = visualScrollRow;
326
+ if (visualCursor[0] < visualScrollRow) {
327
+ newVisualScrollRow = visualCursor[0];
328
+ }
329
+ else if (visualCursor[0] >= visualScrollRow + height) {
330
+ newVisualScrollRow = visualCursor[0] - height + 1;
331
+ }
332
+ if (newVisualScrollRow !== visualScrollRow) {
333
+ setVisualScrollRow(newVisualScrollRow);
334
+ }
335
+ }, [visualCursor, visualScrollRow, viewport]);
336
+ const pushUndo = useCallback(() => {
337
+ dbg('pushUndo', { cursor: [cursorRow, cursorCol], text: lines.join('\n') });
338
+ const snapshot = { lines: [...lines], cursorRow, cursorCol };
339
+ setUndoStack((prev) => {
340
+ const newStack = [...prev, snapshot];
341
+ if (newStack.length > historyLimit) {
342
+ newStack.shift();
343
+ }
344
+ return newStack;
345
+ });
346
+ setRedoStack([]);
347
+ }, [lines, cursorRow, cursorCol, historyLimit]);
348
+ const _restoreState = useCallback((state) => {
349
+ if (!state)
350
+ return false;
351
+ setLines(state.lines);
352
+ setCursorRow(state.cursorRow);
353
+ setCursorCol(state.cursorCol);
354
+ return true;
355
+ }, []);
356
+ const text = lines.join('\n');
357
+ useEffect(() => {
358
+ if (onChange) {
359
+ onChange(text);
360
+ }
361
+ }, [text, onChange]);
362
+ const undo = useCallback(() => {
363
+ const state = undoStack[undoStack.length - 1];
364
+ if (!state)
365
+ return false;
366
+ setUndoStack((prev) => prev.slice(0, -1));
367
+ const currentSnapshot = { lines: [...lines], cursorRow, cursorCol };
368
+ setRedoStack((prev) => [...prev, currentSnapshot]);
369
+ return _restoreState(state);
370
+ }, [undoStack, lines, cursorRow, cursorCol, _restoreState]);
371
+ const redo = useCallback(() => {
372
+ const state = redoStack[redoStack.length - 1];
373
+ if (!state)
374
+ return false;
375
+ setRedoStack((prev) => prev.slice(0, -1));
376
+ const currentSnapshot = { lines: [...lines], cursorRow, cursorCol };
377
+ setUndoStack((prev) => [...prev, currentSnapshot]);
378
+ return _restoreState(state);
379
+ }, [redoStack, lines, cursorRow, cursorCol, _restoreState]);
380
+ const insertStr = useCallback((str) => {
381
+ dbg('insertStr', { str, beforeCursor: [cursorRow, cursorCol] });
382
+ if (str === '')
383
+ return false;
384
+ pushUndo();
385
+ let normalised = str.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
386
+ normalised = stripUnsafeCharacters(normalised);
387
+ const parts = normalised.split('\n');
388
+ const newLines = [...lines];
389
+ const lineContent = currentLine(cursorRow);
390
+ const before = cpSlice(lineContent, 0, cursorCol);
391
+ const after = cpSlice(lineContent, cursorCol);
392
+ newLines[cursorRow] = before + parts[0];
393
+ if (parts.length > 1) {
394
+ // Adjusted condition for inserting multiple lines
395
+ const remainingParts = parts.slice(1);
396
+ const lastPartOriginal = remainingParts.pop() ?? '';
397
+ newLines.splice(cursorRow + 1, 0, ...remainingParts);
398
+ newLines.splice(cursorRow + parts.length - 1, 0, lastPartOriginal + after);
399
+ setCursorRow(cursorRow + parts.length - 1);
400
+ setCursorCol(cpLen(lastPartOriginal));
401
+ }
402
+ else {
403
+ setCursorCol(cpLen(before) + cpLen(parts[0]));
404
+ }
405
+ setLines(newLines);
406
+ setPreferredCol(null);
407
+ return true;
408
+ }, [pushUndo, cursorRow, cursorCol, lines, currentLine, setPreferredCol]);
409
+ const applyOperations = useCallback((ops) => {
410
+ if (ops.length === 0)
411
+ return;
412
+ const expandedOps = [];
413
+ for (const op of ops) {
414
+ if (op.type === 'insert') {
415
+ let currentText = '';
416
+ for (const char of toCodePoints(op.payload)) {
417
+ if (char.codePointAt(0) === 127) {
418
+ // \x7f
419
+ if (currentText.length > 0) {
420
+ expandedOps.push({ type: 'insert', payload: currentText });
421
+ currentText = '';
422
+ }
423
+ expandedOps.push({ type: 'backspace' });
424
+ }
425
+ else {
426
+ currentText += char;
427
+ }
428
+ }
429
+ if (currentText.length > 0) {
430
+ expandedOps.push({ type: 'insert', payload: currentText });
431
+ }
432
+ }
433
+ else {
434
+ expandedOps.push(op);
435
+ }
436
+ }
437
+ if (expandedOps.length === 0) {
438
+ return;
439
+ }
440
+ pushUndo(); // Snapshot before applying batch of updates
441
+ const newLines = [...lines];
442
+ let newCursorRow = cursorRow;
443
+ let newCursorCol = cursorCol;
444
+ const currentLine = (r) => newLines[r] ?? '';
445
+ for (const op of expandedOps) {
446
+ if (op.type === 'insert') {
447
+ const str = stripUnsafeCharacters(op.payload.replace(/\r\n/g, '\n').replace(/\r/g, '\n'));
448
+ const parts = str.split('\n');
449
+ const lineContent = currentLine(newCursorRow);
450
+ const before = cpSlice(lineContent, 0, newCursorCol);
451
+ const after = cpSlice(lineContent, newCursorCol);
452
+ if (parts.length > 1) {
453
+ newLines[newCursorRow] = before + parts[0];
454
+ const remainingParts = parts.slice(1);
455
+ const lastPartOriginal = remainingParts.pop() ?? '';
456
+ newLines.splice(newCursorRow + 1, 0, ...remainingParts);
457
+ newLines.splice(newCursorRow + parts.length - 1, 0, lastPartOriginal + after);
458
+ newCursorRow = newCursorRow + parts.length - 1;
459
+ newCursorCol = cpLen(lastPartOriginal);
460
+ }
461
+ else {
462
+ newLines[newCursorRow] = before + parts[0] + after;
463
+ newCursorCol = cpLen(before) + cpLen(parts[0]);
464
+ }
465
+ }
466
+ else if (op.type === 'backspace') {
467
+ if (newCursorCol === 0 && newCursorRow === 0)
468
+ continue;
469
+ if (newCursorCol > 0) {
470
+ const lineContent = currentLine(newCursorRow);
471
+ newLines[newCursorRow] =
472
+ cpSlice(lineContent, 0, newCursorCol - 1) +
473
+ cpSlice(lineContent, newCursorCol);
474
+ newCursorCol--;
475
+ }
476
+ else if (newCursorRow > 0) {
477
+ const prevLineContent = currentLine(newCursorRow - 1);
478
+ const currentLineContentVal = currentLine(newCursorRow);
479
+ const newCol = cpLen(prevLineContent);
480
+ newLines[newCursorRow - 1] =
481
+ prevLineContent + currentLineContentVal;
482
+ newLines.splice(newCursorRow, 1);
483
+ newCursorRow--;
484
+ newCursorCol = newCol;
485
+ }
486
+ }
487
+ }
488
+ setLines(newLines);
489
+ setCursorRow(newCursorRow);
490
+ setCursorCol(newCursorCol);
491
+ setPreferredCol(null);
492
+ }, [lines, cursorRow, cursorCol, pushUndo, setPreferredCol]);
493
+ const insert = useCallback((ch) => {
494
+ if (/[\n\r]/.test(ch)) {
495
+ insertStr(ch);
496
+ return;
497
+ }
498
+ dbg('insert', { ch, beforeCursor: [cursorRow, cursorCol] });
499
+ ch = stripUnsafeCharacters(ch);
500
+ // Arbitrary threshold to avoid false positives on normal key presses
501
+ // while still detecting virtually all reasonable length file paths.
502
+ const minLengthToInferAsDragDrop = 3;
503
+ if (ch.length >= minLengthToInferAsDragDrop) {
504
+ // Possible drag and drop of a file path.
505
+ let potentialPath = ch;
506
+ if (potentialPath.length > 2 &&
507
+ potentialPath.startsWith("'") &&
508
+ potentialPath.endsWith("'")) {
509
+ potentialPath = ch.slice(1, -1);
510
+ }
511
+ potentialPath = potentialPath.trim();
512
+ // Be conservative and only add an @ if the path is valid.
513
+ if (isValidPath(unescapePath(potentialPath))) {
514
+ ch = `@${potentialPath}`;
515
+ }
516
+ }
517
+ applyOperations([{ type: 'insert', payload: ch }]);
518
+ }, [applyOperations, cursorRow, cursorCol, isValidPath, insertStr]);
519
+ const newline = useCallback(() => {
520
+ dbg('newline', { beforeCursor: [cursorRow, cursorCol] });
521
+ applyOperations([{ type: 'insert', payload: '\n' }]);
522
+ }, [applyOperations, cursorRow, cursorCol]);
523
+ const backspace = useCallback(() => {
524
+ dbg('backspace', { beforeCursor: [cursorRow, cursorCol] });
525
+ if (cursorCol === 0 && cursorRow === 0)
526
+ return;
527
+ applyOperations([{ type: 'backspace' }]);
528
+ }, [applyOperations, cursorRow, cursorCol]);
529
+ const del = useCallback(() => {
530
+ dbg('delete', { beforeCursor: [cursorRow, cursorCol] });
531
+ const lineContent = currentLine(cursorRow);
532
+ if (cursorCol < currentLineLen(cursorRow)) {
533
+ pushUndo();
534
+ setLines((prevLines) => {
535
+ const newLines = [...prevLines];
536
+ newLines[cursorRow] =
537
+ cpSlice(lineContent, 0, cursorCol) +
538
+ cpSlice(lineContent, cursorCol + 1);
539
+ return newLines;
540
+ });
541
+ }
542
+ else if (cursorRow < lines.length - 1) {
543
+ pushUndo();
544
+ const nextLineContent = currentLine(cursorRow + 1);
545
+ setLines((prevLines) => {
546
+ const newLines = [...prevLines];
547
+ newLines[cursorRow] = lineContent + nextLineContent;
548
+ newLines.splice(cursorRow + 1, 1);
549
+ return newLines;
550
+ });
551
+ }
552
+ // cursor position does not change for del
553
+ setPreferredCol(null);
554
+ }, [
555
+ pushUndo,
556
+ cursorRow,
557
+ cursorCol,
558
+ currentLine,
559
+ currentLineLen,
560
+ lines.length,
561
+ setPreferredCol,
562
+ ]);
563
+ const setText = useCallback((newText) => {
564
+ dbg('setText', { text: newText });
565
+ pushUndo();
566
+ const newContentLines = newText.replace(/\r\n?/g, '\n').split('\n');
567
+ setLines(newContentLines.length === 0 ? [''] : newContentLines);
568
+ // Set logical cursor to the end of the new text
569
+ const lastNewLineIndex = newContentLines.length - 1;
570
+ setCursorRow(lastNewLineIndex);
571
+ setCursorCol(cpLen(newContentLines[lastNewLineIndex] ?? ''));
572
+ setPreferredCol(null);
573
+ }, [pushUndo, setPreferredCol]);
574
+ const replaceRange = useCallback((startRow, startCol, endRow, endCol, replacementText) => {
575
+ if (startRow > endRow ||
576
+ (startRow === endRow && startCol > endCol) ||
577
+ startRow < 0 ||
578
+ startCol < 0 ||
579
+ endRow >= lines.length ||
580
+ (endRow < lines.length && endCol > currentLineLen(endRow))) {
581
+ console.error('Invalid range provided to replaceRange', {
582
+ startRow,
583
+ startCol,
584
+ endRow,
585
+ endCol,
586
+ linesLength: lines.length,
587
+ endRowLineLength: currentLineLen(endRow),
588
+ });
589
+ return false;
590
+ }
591
+ dbg('replaceRange', {
592
+ start: [startRow, startCol],
593
+ end: [endRow, endCol],
594
+ text: replacementText,
595
+ });
596
+ pushUndo();
597
+ const sCol = clamp(startCol, 0, currentLineLen(startRow));
598
+ const eCol = clamp(endCol, 0, currentLineLen(endRow));
599
+ const prefix = cpSlice(currentLine(startRow), 0, sCol);
600
+ const suffix = cpSlice(currentLine(endRow), eCol);
601
+ const normalisedReplacement = replacementText
602
+ .replace(/\r\n/g, '\n')
603
+ .replace(/\r/g, '\n');
604
+ const replacementParts = normalisedReplacement.split('\n');
605
+ setLines((prevLines) => {
606
+ const newLines = [...prevLines];
607
+ // Remove lines between startRow and endRow (exclusive of startRow, inclusive of endRow if different)
608
+ if (startRow < endRow) {
609
+ newLines.splice(startRow + 1, endRow - startRow);
610
+ }
611
+ // Construct the new content for the startRow
612
+ newLines[startRow] = prefix + replacementParts[0];
613
+ // If replacementText has multiple lines, insert them
614
+ if (replacementParts.length > 1) {
615
+ const lastReplacementPart = replacementParts.pop() ?? ''; // parts are already split by \n
616
+ // Insert middle parts (if any)
617
+ if (replacementParts.length > 1) {
618
+ // parts[0] is already used
619
+ newLines.splice(startRow + 1, 0, ...replacementParts.slice(1));
620
+ }
621
+ // The line where the last part of the replacement will go
622
+ const targetRowForLastPart = startRow + (replacementParts.length - 1); // -1 because parts[0] is on startRow
623
+ // If the last part is not the first part (multi-line replacement)
624
+ if (targetRowForLastPart > startRow ||
625
+ (replacementParts.length === 1 && lastReplacementPart !== '')) {
626
+ // If the target row for the last part doesn't exist (because it's a new line created by replacement)
627
+ // ensure it's created before trying to append suffix.
628
+ // This case should be handled by splice if replacementParts.length > 1
629
+ // For single line replacement that becomes multi-line due to parts.length > 1 logic, this is tricky.
630
+ // Let's assume newLines[targetRowForLastPart] exists due to previous splice or it's newLines[startRow]
631
+ if (newLines[targetRowForLastPart] === undefined &&
632
+ targetRowForLastPart === startRow + 1 &&
633
+ replacementParts.length === 1) {
634
+ // This implies a single line replacement that became two lines.
635
+ // e.g. "abc" replace "b" with "B\nC" -> "aB", "C", "c"
636
+ // Here, lastReplacementPart is "C", targetRowForLastPart is startRow + 1
637
+ newLines.splice(targetRowForLastPart, 0, lastReplacementPart + suffix);
638
+ }
639
+ else {
640
+ newLines[targetRowForLastPart] =
641
+ (newLines[targetRowForLastPart] || '') +
642
+ lastReplacementPart +
643
+ suffix;
644
+ }
645
+ }
646
+ else {
647
+ // Single line in replacementParts, but it was the only part
648
+ newLines[startRow] += suffix;
649
+ }
650
+ setCursorRow(targetRowForLastPart);
651
+ setCursorCol(cpLen(newLines[targetRowForLastPart]) - cpLen(suffix));
652
+ }
653
+ else {
654
+ // Single line replacement (replacementParts has only one item)
655
+ newLines[startRow] += suffix;
656
+ setCursorRow(startRow);
657
+ setCursorCol(cpLen(prefix) + cpLen(replacementParts[0]));
658
+ }
659
+ return newLines;
660
+ });
661
+ setPreferredCol(null);
662
+ return true;
663
+ }, [pushUndo, lines, currentLine, currentLineLen, setPreferredCol]);
664
+ const deleteWordLeft = useCallback(() => {
665
+ dbg('deleteWordLeft', { beforeCursor: [cursorRow, cursorCol] });
666
+ if (cursorCol === 0 && cursorRow === 0)
667
+ return;
668
+ if (cursorCol === 0) {
669
+ backspace();
670
+ return;
671
+ }
672
+ pushUndo();
673
+ const lineContent = currentLine(cursorRow);
674
+ const arr = toCodePoints(lineContent);
675
+ let start = cursorCol;
676
+ let onlySpaces = true;
677
+ for (let i = 0; i < start; i++) {
678
+ if (isWordChar(arr[i])) {
679
+ onlySpaces = false;
680
+ break;
681
+ }
682
+ }
683
+ if (onlySpaces && start > 0) {
684
+ start--;
685
+ }
686
+ else {
687
+ while (start > 0 && !isWordChar(arr[start - 1]))
688
+ start--;
689
+ while (start > 0 && isWordChar(arr[start - 1]))
690
+ start--;
691
+ }
692
+ setLines((prevLines) => {
693
+ const newLines = [...prevLines];
694
+ newLines[cursorRow] =
695
+ cpSlice(lineContent, 0, start) + cpSlice(lineContent, cursorCol);
696
+ return newLines;
697
+ });
698
+ setCursorCol(start);
699
+ setPreferredCol(null);
700
+ }, [pushUndo, cursorRow, cursorCol, currentLine, backspace, setPreferredCol]);
701
+ const deleteWordRight = useCallback(() => {
702
+ dbg('deleteWordRight', { beforeCursor: [cursorRow, cursorCol] });
703
+ const lineContent = currentLine(cursorRow);
704
+ const arr = toCodePoints(lineContent);
705
+ if (cursorCol >= arr.length && cursorRow === lines.length - 1)
706
+ return;
707
+ if (cursorCol >= arr.length) {
708
+ del();
709
+ return;
710
+ }
711
+ pushUndo();
712
+ let end = cursorCol;
713
+ while (end < arr.length && !isWordChar(arr[end]))
714
+ end++;
715
+ while (end < arr.length && isWordChar(arr[end]))
716
+ end++;
717
+ setLines((prevLines) => {
718
+ const newLines = [...prevLines];
719
+ newLines[cursorRow] =
720
+ cpSlice(lineContent, 0, cursorCol) + cpSlice(lineContent, end);
721
+ return newLines;
722
+ });
723
+ setPreferredCol(null);
724
+ }, [
725
+ pushUndo,
726
+ cursorRow,
727
+ cursorCol,
728
+ currentLine,
729
+ del,
730
+ lines.length,
731
+ setPreferredCol,
732
+ ]);
733
+ const killLineRight = useCallback(() => {
734
+ const lineContent = currentLine(cursorRow);
735
+ if (cursorCol < currentLineLen(cursorRow)) {
736
+ // Cursor is before the end of the line's content, delete text to the right
737
+ pushUndo();
738
+ setLines((prevLines) => {
739
+ const newLines = [...prevLines];
740
+ newLines[cursorRow] = cpSlice(lineContent, 0, cursorCol);
741
+ return newLines;
742
+ });
743
+ // Cursor position and preferredCol do not change in this case
744
+ }
745
+ else if (cursorCol === currentLineLen(cursorRow) &&
746
+ cursorRow < lines.length - 1) {
747
+ // Cursor is at the end of the line's content (or line is empty),
748
+ // and it's not the last line. Delete the newline.
749
+ // `del()` handles pushUndo and setPreferredCol.
750
+ del();
751
+ }
752
+ // If cursor is at the end of the line and it's the last line, do nothing.
753
+ }, [
754
+ pushUndo,
755
+ cursorRow,
756
+ cursorCol,
757
+ currentLine,
758
+ currentLineLen,
759
+ lines.length,
760
+ del,
761
+ ]);
762
+ const killLineLeft = useCallback(() => {
763
+ const lineContent = currentLine(cursorRow);
764
+ // Only act if the cursor is not at the beginning of the line
765
+ if (cursorCol > 0) {
766
+ pushUndo();
767
+ setLines((prevLines) => {
768
+ const newLines = [...prevLines];
769
+ newLines[cursorRow] = cpSlice(lineContent, cursorCol);
770
+ return newLines;
771
+ });
772
+ setCursorCol(0);
773
+ setPreferredCol(null);
774
+ }
775
+ }, [pushUndo, cursorRow, cursorCol, currentLine, setPreferredCol]);
776
+ const move = useCallback((dir) => {
777
+ let newVisualRow = visualCursor[0];
778
+ let newVisualCol = visualCursor[1];
779
+ let newPreferredCol = preferredCol;
780
+ const currentVisLineLen = cpLen(visualLines[newVisualRow] ?? '');
781
+ switch (dir) {
782
+ case 'left':
783
+ newPreferredCol = null;
784
+ if (newVisualCol > 0) {
785
+ newVisualCol--;
786
+ }
787
+ else if (newVisualRow > 0) {
788
+ newVisualRow--;
789
+ newVisualCol = cpLen(visualLines[newVisualRow] ?? '');
790
+ }
791
+ break;
792
+ case 'right':
793
+ newPreferredCol = null;
794
+ if (newVisualCol < currentVisLineLen) {
795
+ newVisualCol++;
796
+ }
797
+ else if (newVisualRow < visualLines.length - 1) {
798
+ newVisualRow++;
799
+ newVisualCol = 0;
800
+ }
801
+ break;
802
+ case 'up':
803
+ if (newVisualRow > 0) {
804
+ if (newPreferredCol === null)
805
+ newPreferredCol = newVisualCol;
806
+ newVisualRow--;
807
+ newVisualCol = clamp(newPreferredCol, 0, cpLen(visualLines[newVisualRow] ?? ''));
808
+ }
809
+ break;
810
+ case 'down':
811
+ if (newVisualRow < visualLines.length - 1) {
812
+ if (newPreferredCol === null)
813
+ newPreferredCol = newVisualCol;
814
+ newVisualRow++;
815
+ newVisualCol = clamp(newPreferredCol, 0, cpLen(visualLines[newVisualRow] ?? ''));
816
+ }
817
+ break;
818
+ case 'home':
819
+ newPreferredCol = null;
820
+ newVisualCol = 0;
821
+ break;
822
+ case 'end':
823
+ newPreferredCol = null;
824
+ newVisualCol = currentVisLineLen;
825
+ break;
826
+ // wordLeft and wordRight might need more sophisticated visual handling
827
+ // For now, they operate on the logical line derived from the visual cursor
828
+ case 'wordLeft': {
829
+ newPreferredCol = null;
830
+ if (visualToLogicalMap.length === 0 ||
831
+ logicalToVisualMap.length === 0)
832
+ break;
833
+ const [logRow, logColInitial] = visualToLogicalMap[newVisualRow] ?? [
834
+ 0, 0,
835
+ ];
836
+ const currentLogCol = logColInitial + newVisualCol;
837
+ const lineText = lines[logRow];
838
+ const sliceToCursor = cpSlice(lineText, 0, currentLogCol).replace(/[\s,.;!?]+$/, '');
839
+ let lastIdx = 0;
840
+ const regex = /[\s,.;!?]+/g;
841
+ let m;
842
+ while ((m = regex.exec(sliceToCursor)) != null)
843
+ lastIdx = m.index;
844
+ const newLogicalCol = lastIdx === 0 ? 0 : cpLen(sliceToCursor.slice(0, lastIdx)) + 1;
845
+ // Map newLogicalCol back to visual
846
+ const targetLogicalMapEntries = logicalToVisualMap[logRow];
847
+ if (!targetLogicalMapEntries)
848
+ break;
849
+ for (let i = targetLogicalMapEntries.length - 1; i >= 0; i--) {
850
+ const [visRow, logStartCol] = targetLogicalMapEntries[i];
851
+ if (newLogicalCol >= logStartCol) {
852
+ newVisualRow = visRow;
853
+ newVisualCol = newLogicalCol - logStartCol;
854
+ break;
855
+ }
856
+ }
857
+ break;
858
+ }
859
+ case 'wordRight': {
860
+ newPreferredCol = null;
861
+ if (visualToLogicalMap.length === 0 ||
862
+ logicalToVisualMap.length === 0)
863
+ break;
864
+ const [logRow, logColInitial] = visualToLogicalMap[newVisualRow] ?? [
865
+ 0, 0,
866
+ ];
867
+ const currentLogCol = logColInitial + newVisualCol;
868
+ const lineText = lines[logRow];
869
+ const regex = /[\s,.;!?]+/g;
870
+ let moved = false;
871
+ let m;
872
+ let newLogicalCol = currentLineLen(logRow); // Default to end of logical line
873
+ while ((m = regex.exec(lineText)) != null) {
874
+ const cpIdx = cpLen(lineText.slice(0, m.index));
875
+ if (cpIdx > currentLogCol) {
876
+ newLogicalCol = cpIdx;
877
+ moved = true;
878
+ break;
879
+ }
880
+ }
881
+ if (!moved && currentLogCol < currentLineLen(logRow)) {
882
+ // If no word break found after cursor, move to end
883
+ newLogicalCol = currentLineLen(logRow);
884
+ }
885
+ // Map newLogicalCol back to visual
886
+ const targetLogicalMapEntries = logicalToVisualMap[logRow];
887
+ if (!targetLogicalMapEntries)
888
+ break;
889
+ for (let i = 0; i < targetLogicalMapEntries.length; i++) {
890
+ const [visRow, logStartCol] = targetLogicalMapEntries[i];
891
+ const nextLogStartCol = i + 1 < targetLogicalMapEntries.length
892
+ ? targetLogicalMapEntries[i + 1][1]
893
+ : Infinity;
894
+ if (newLogicalCol >= logStartCol &&
895
+ newLogicalCol < nextLogStartCol) {
896
+ newVisualRow = visRow;
897
+ newVisualCol = newLogicalCol - logStartCol;
898
+ break;
899
+ }
900
+ if (newLogicalCol === logStartCol &&
901
+ i === targetLogicalMapEntries.length - 1 &&
902
+ cpLen(visualLines[visRow] ?? '') === 0) {
903
+ // Special case: moving to an empty visual line at the end of a logical line
904
+ newVisualRow = visRow;
905
+ newVisualCol = 0;
906
+ break;
907
+ }
908
+ }
909
+ break;
910
+ }
911
+ default:
912
+ break;
913
+ }
914
+ setVisualCursor([newVisualRow, newVisualCol]);
915
+ setPreferredCol(newPreferredCol);
916
+ // Update logical cursor based on new visual cursor
917
+ if (visualToLogicalMap[newVisualRow]) {
918
+ const [logRow, logStartCol] = visualToLogicalMap[newVisualRow];
919
+ setCursorRow(logRow);
920
+ setCursorCol(clamp(logStartCol + newVisualCol, 0, currentLineLen(logRow)));
921
+ }
922
+ dbg('move', {
923
+ dir,
924
+ visualBefore: visualCursor,
925
+ visualAfter: [newVisualRow, newVisualCol],
926
+ logicalAfter: [cursorRow, cursorCol],
927
+ });
928
+ }, [
929
+ visualCursor,
930
+ visualLines,
931
+ preferredCol,
932
+ lines,
933
+ currentLineLen,
934
+ visualToLogicalMap,
935
+ logicalToVisualMap,
936
+ cursorCol,
937
+ cursorRow,
938
+ ]);
939
+ const openInExternalEditor = useCallback(async (opts = {}) => {
940
+ const editor = opts.editor ??
941
+ process.env['VISUAL'] ??
942
+ process.env['EDITOR'] ??
943
+ (process.platform === 'win32' ? 'notepad' : 'vi');
944
+ const tmpDir = fs.mkdtempSync(pathMod.join(os.tmpdir(), 'gemini-edit-'));
945
+ const filePath = pathMod.join(tmpDir, 'buffer.txt');
946
+ fs.writeFileSync(filePath, text, 'utf8');
947
+ pushUndo(); // Snapshot before external edit
948
+ const wasRaw = stdin?.isRaw ?? false;
949
+ try {
950
+ setRawMode?.(false);
951
+ const { status, error } = spawnSync(editor, [filePath], {
952
+ stdio: 'inherit',
953
+ });
954
+ if (error)
955
+ throw error;
956
+ if (typeof status === 'number' && status !== 0)
957
+ throw new Error(`External editor exited with status ${status}`);
958
+ let newText = fs.readFileSync(filePath, 'utf8');
959
+ newText = newText.replace(/\r\n?/g, '\n');
960
+ setText(newText);
961
+ }
962
+ catch (err) {
963
+ console.error('[useTextBuffer] external editor error', err);
964
+ // TODO(jacobr): potentially revert or handle error state.
965
+ }
966
+ finally {
967
+ if (wasRaw)
968
+ setRawMode?.(true);
969
+ try {
970
+ fs.unlinkSync(filePath);
971
+ }
972
+ catch {
973
+ /* ignore */
974
+ }
975
+ try {
976
+ fs.rmdirSync(tmpDir);
977
+ }
978
+ catch {
979
+ /* ignore */
980
+ }
981
+ }
982
+ }, [text, pushUndo, stdin, setRawMode, setText]);
983
+ const handleInput = useCallback((input, key) => {
984
+ dbg('handleInput', {
985
+ input,
986
+ key,
987
+ cursor: [cursorRow, cursorCol],
988
+ visualCursor,
989
+ });
990
+ const beforeText = text;
991
+ const beforeLogicalCursor = [cursorRow, cursorCol];
992
+ const beforeVisualCursor = [...visualCursor];
993
+ if (key['escape'])
994
+ return false;
995
+ if (key['return'] ||
996
+ input === '\r' ||
997
+ input === '\n' ||
998
+ input === '\\\r' // VSCode terminal represents shift + enter this way
999
+ )
1000
+ newline();
1001
+ else if (key['leftArrow'] && !key['meta'] && !key['ctrl'] && !key['alt'])
1002
+ move('left');
1003
+ else if (key['ctrl'] && input === 'b')
1004
+ move('left');
1005
+ else if (key['rightArrow'] && !key['meta'] && !key['ctrl'] && !key['alt'])
1006
+ move('right');
1007
+ else if (key['ctrl'] && input === 'f')
1008
+ move('right');
1009
+ else if (key['upArrow'])
1010
+ move('up');
1011
+ else if (key['downArrow'])
1012
+ move('down');
1013
+ else if ((key['ctrl'] || key['alt']) && key['leftArrow'])
1014
+ move('wordLeft');
1015
+ else if (key['meta'] && input === 'b')
1016
+ move('wordLeft');
1017
+ else if ((key['ctrl'] || key['alt']) && key['rightArrow'])
1018
+ move('wordRight');
1019
+ else if (key['meta'] && input === 'f')
1020
+ move('wordRight');
1021
+ else if (key['home'])
1022
+ move('home');
1023
+ else if (key['ctrl'] && input === 'a')
1024
+ move('home');
1025
+ else if (key['end'])
1026
+ move('end');
1027
+ else if (key['ctrl'] && input === 'e')
1028
+ move('end');
1029
+ else if (key['ctrl'] && input === 'w')
1030
+ deleteWordLeft();
1031
+ else if ((key['meta'] || key['ctrl'] || key['alt']) &&
1032
+ (key['backspace'] || input === '\x7f'))
1033
+ deleteWordLeft();
1034
+ else if ((key['meta'] || key['ctrl'] || key['alt']) && key['delete'])
1035
+ deleteWordRight();
1036
+ else if (key['backspace'] ||
1037
+ input === '\x7f' ||
1038
+ (key['ctrl'] && input === 'h') ||
1039
+ (key['delete'] && !key['shift']))
1040
+ backspace();
1041
+ else if (key['delete'] || (key['ctrl'] && input === 'd'))
1042
+ del();
1043
+ else if (input && !key['ctrl'] && !key['meta']) {
1044
+ insert(input);
1045
+ }
1046
+ const textChanged = text !== beforeText;
1047
+ // After operations, visualCursor might not be immediately updated if the change
1048
+ // was to `lines`, `cursorRow`, or `cursorCol` which then triggers the useEffect.
1049
+ // So, for return value, we check logical cursor change.
1050
+ const cursorChanged = cursorRow !== beforeLogicalCursor[0] ||
1051
+ cursorCol !== beforeLogicalCursor[1] ||
1052
+ visualCursor[0] !== beforeVisualCursor[0] ||
1053
+ visualCursor[1] !== beforeVisualCursor[1];
1054
+ dbg('handleInput:after', {
1055
+ cursor: [cursorRow, cursorCol],
1056
+ visualCursor,
1057
+ text,
1058
+ });
1059
+ return textChanged || cursorChanged;
1060
+ }, [
1061
+ text,
1062
+ cursorRow,
1063
+ cursorCol,
1064
+ visualCursor,
1065
+ newline,
1066
+ move,
1067
+ deleteWordLeft,
1068
+ deleteWordRight,
1069
+ backspace,
1070
+ del,
1071
+ insert,
1072
+ ]);
1073
+ const renderedVisualLines = useMemo(() => visualLines.slice(visualScrollRow, visualScrollRow + viewport.height), [visualLines, visualScrollRow, viewport.height]);
1074
+ const replaceRangeByOffset = useCallback((startOffset, endOffset, replacementText) => {
1075
+ dbg('replaceRangeByOffset', { startOffset, endOffset, replacementText });
1076
+ const [startRow, startCol] = offsetToLogicalPos(text, startOffset);
1077
+ const [endRow, endCol] = offsetToLogicalPos(text, endOffset);
1078
+ return replaceRange(startRow, startCol, endRow, endCol, replacementText);
1079
+ }, [text, replaceRange]);
1080
+ const moveToOffset = useCallback((offset) => {
1081
+ const [newRow, newCol] = offsetToLogicalPos(text, offset);
1082
+ setCursorRow(newRow);
1083
+ setCursorCol(newCol);
1084
+ setPreferredCol(null);
1085
+ dbg('moveToOffset', { offset, newCursor: [newRow, newCol] });
1086
+ }, [text, setPreferredCol]);
1087
+ const returnValue = {
1088
+ lines,
1089
+ text,
1090
+ cursor: [cursorRow, cursorCol],
1091
+ preferredCol,
1092
+ selectionAnchor,
1093
+ allVisualLines: visualLines,
1094
+ viewportVisualLines: renderedVisualLines,
1095
+ visualCursor,
1096
+ visualScrollRow,
1097
+ setText,
1098
+ insert,
1099
+ newline,
1100
+ backspace,
1101
+ del,
1102
+ move,
1103
+ undo,
1104
+ redo,
1105
+ replaceRange,
1106
+ replaceRangeByOffset,
1107
+ moveToOffset, // Added here
1108
+ deleteWordLeft,
1109
+ deleteWordRight,
1110
+ killLineRight,
1111
+ killLineLeft,
1112
+ handleInput,
1113
+ openInExternalEditor,
1114
+ applyOperations,
1115
+ copy: useCallback(() => {
1116
+ if (!selectionAnchor)
1117
+ return null;
1118
+ const [ar, ac] = selectionAnchor;
1119
+ const [br, bc] = [cursorRow, cursorCol];
1120
+ if (ar === br && ac === bc)
1121
+ return null;
1122
+ const topBefore = ar < br || (ar === br && ac < bc);
1123
+ const [sr, sc, er, ec] = topBefore ? [ar, ac, br, bc] : [br, bc, ar, ac];
1124
+ let selectedTextVal;
1125
+ if (sr === er) {
1126
+ selectedTextVal = cpSlice(currentLine(sr), sc, ec);
1127
+ }
1128
+ else {
1129
+ const parts = [cpSlice(currentLine(sr), sc)];
1130
+ for (let r = sr + 1; r < er; r++)
1131
+ parts.push(currentLine(r));
1132
+ parts.push(cpSlice(currentLine(er), 0, ec));
1133
+ selectedTextVal = parts.join('\n');
1134
+ }
1135
+ setClipboard(selectedTextVal);
1136
+ return selectedTextVal;
1137
+ }, [selectionAnchor, cursorRow, cursorCol, currentLine, setClipboard]),
1138
+ paste: useCallback(() => {
1139
+ if (clipboard === null)
1140
+ return false;
1141
+ return insertStr(clipboard);
1142
+ }, [clipboard, insertStr]),
1143
+ startSelection: useCallback(() => setSelectionAnchor([cursorRow, cursorCol]), [cursorRow, cursorCol, setSelectionAnchor]),
1144
+ };
1145
+ return returnValue;
1146
+ }
1147
+ //# sourceMappingURL=text-buffer.js.map