Skip to content

Commit ce10e1d

Browse files
author
Sisyphus Agent
committed
fix(tui): apply CodeRabbit suggestions for safe input access in prompt
1 parent 3813de7 commit ce10e1d

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

‎.husky/pre-push‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/bin/sh
2-
32
# Check if bun version matches package.json
43
EXPECTED_VERSION=$(grep '"packageManager"' package.json | sed 's/.*"bun@\([^"]*\)".*/\1/')
54
CURRENT_VERSION=$(bun --version)

‎packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export function Prompt(props: PromptProps) {
202202
category: "Session",
203203
onSelect: (dialog) => {
204204
if (autocomplete.visible) return
205-
if (!input.focused) return
205+
if (!input?.focused) return
206206
// TODO: this should be its own command
207207
if (store.mode === "shell") {
208208
setStore("mode", "normal")
@@ -446,6 +446,7 @@ export function Prompt(props: PromptProps) {
446446
dialog.replace(() => (
447447
<DialogStash
448448
onSelect={(entry) => {
449+
if (!input) return
449450
input.setText(entry.input)
450451
setStore("prompt", { input: entry.input, parts: entry.parts })
451452
restoreExtmarksFromParts(entry.parts)
@@ -806,8 +807,8 @@ export function Prompt(props: PromptProps) {
806807
// If no image, let the default paste behavior continue
807808
}
808809
if (keybind.match("input_clear", e) && store.prompt.input !== "") {
809-
input.clear()
810-
input.extmarks.clear()
810+
input?.clear()
811+
input?.extmarks.clear()
811812
setStore("prompt", {
812813
input: "",
813814
parts: [],
@@ -823,13 +824,13 @@ export function Prompt(props: PromptProps) {
823824
return
824825
}
825826
}
826-
if (e.name === "!" && input.visualCursor.offset === 0) {
827+
if (e.name === "!" && input?.visualCursor.offset === 0) {
827828
setStore("mode", "shell")
828829
e.preventDefault()
829830
return
830831
}
831832
if (store.mode === "shell") {
832-
if ((e.name === "backspace" && input.visualCursor.offset === 0) || e.name === "escape") {
833+
if ((e.name === "backspace" && input?.visualCursor.offset === 0) || e.name === "escape") {
833834
setStore("mode", "normal")
834835
e.preventDefault()
835836
return
@@ -838,9 +839,10 @@ export function Prompt(props: PromptProps) {
838839
if (store.mode === "normal") autocomplete.onKeyDown(e)
839840
if (!autocomplete.visible) {
840841
if (
841-
(keybind.match("history_previous", e) && input.cursorOffset === 0) ||
842-
(keybind.match("history_next", e) && input.cursorOffset === input.plainText.length)
842+
(keybind.match("history_previous", e) && input?.cursorOffset === 0) ||
843+
(keybind.match("history_next", e) && input?.cursorOffset === input?.plainText.length)
843844
) {
845+
if (!input) return
844846
const direction = keybind.match("history_previous", e) ? -1 : 1
845847
const item = history.move(direction, input.plainText)
846848

@@ -856,8 +858,13 @@ export function Prompt(props: PromptProps) {
856858
return
857859
}
858860

859-
if (keybind.match("history_previous", e) && input.visualCursor.visualRow === 0) input.cursorOffset = 0
860-
if (keybind.match("history_next", e) && input.visualCursor.visualRow === input.height - 1)
861+
if (keybind.match("history_previous", e) && input?.visualCursor.visualRow === 0 && input)
862+
input.cursorOffset = 0
863+
if (
864+
keybind.match("history_next", e) &&
865+
input?.visualCursor.visualRow === (input?.height ?? 0) - 1 &&
866+
input
867+
)
861868
input.cursorOffset = input.plainText.length
862869
}
863870
}}

0 commit comments

Comments
 (0)