Skip to content

Commit 98402d1

Browse files
committed
feat(tui): add Ctrl+R reverse search for prompt history
Add reverse-i-search to the prompt history. Press Ctrl+R to enter search mode, type to filter history entries by substring match. Ctrl+R again cycles through matches, Enter accepts, Escape cancels. Closes anomalyco#5062
1 parent 0befa1e commit 98402d1

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,54 @@ export const { use: usePromptHistory, provider: PromptHistoryProvider } = create
5858
const [store, setStore] = createStore({
5959
index: 0,
6060
history: [] as PromptInfo[],
61+
searching: false,
62+
query: "",
63+
matches: [] as number[],
64+
cursor: 0,
6165
})
6266

67+
function findMatches(q: string): number[] {
68+
if (!q) return []
69+
const lower = q.toLowerCase()
70+
return store.history
71+
.map((entry, i) => (entry.input.toLowerCase().includes(lower) ? i : -1))
72+
.filter((i) => i !== -1)
73+
.reverse()
74+
}
75+
6376
return {
77+
get searching() {
78+
return store.searching
79+
},
80+
get query() {
81+
return store.query
82+
},
83+
startSearch() {
84+
setStore("searching", true)
85+
setStore("query", "")
86+
setStore("matches", [])
87+
setStore("cursor", 0)
88+
},
89+
stopSearch() {
90+
setStore("searching", false)
91+
setStore("query", "")
92+
setStore("matches", [])
93+
setStore("cursor", 0)
94+
},
95+
updateQuery(q: string): PromptInfo | undefined {
96+
setStore("query", q)
97+
const found = findMatches(q)
98+
setStore("matches", found)
99+
setStore("cursor", 0)
100+
if (!found.length) return undefined
101+
return store.history[found[0]]
102+
},
103+
nextMatch(): PromptInfo | undefined {
104+
if (!store.matches.length) return undefined
105+
const next = (store.cursor + 1) % store.matches.length
106+
setStore("cursor", next)
107+
return store.history[store.matches[next]]
108+
},
64109
move(direction: 1 | -1, input: string) {
65110
if (!store.history.length) return undefined
66111
const current = store.history.at(store.index)

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,61 @@ export function Prompt(props: PromptProps) {
887887
return
888888
}
889889
}
890+
// History search mode (Ctrl+R)
891+
if (keybind.match("history_search", e)) {
892+
if (history.searching) {
893+
const item = history.nextMatch()
894+
if (item) {
895+
input.setText(item.input)
896+
setStore("prompt", item)
897+
restoreExtmarksFromParts(item.parts)
898+
}
899+
} else {
900+
history.startSearch()
901+
}
902+
e.preventDefault()
903+
return
904+
}
905+
if (history.searching) {
906+
if (e.name === "escape") {
907+
history.stopSearch()
908+
e.preventDefault()
909+
return
910+
}
911+
if (e.name === "return") {
912+
history.stopSearch()
913+
e.preventDefault()
914+
return
915+
}
916+
if (e.name === "backspace") {
917+
const q = history.query.slice(0, -1)
918+
if (!q) {
919+
history.stopSearch()
920+
e.preventDefault()
921+
return
922+
}
923+
const item = history.updateQuery(q)
924+
if (item) {
925+
input.setText(item.input)
926+
setStore("prompt", item)
927+
restoreExtmarksFromParts(item.parts)
928+
}
929+
e.preventDefault()
930+
return
931+
}
932+
if (e.name.length === 1 && !e.ctrl && !e.meta) {
933+
const item = history.updateQuery(history.query + e.name)
934+
if (item) {
935+
input.setText(item.input)
936+
setStore("prompt", item)
937+
restoreExtmarksFromParts(item.parts)
938+
}
939+
e.preventDefault()
940+
return
941+
}
942+
e.preventDefault()
943+
return
944+
}
890945
if (e.name === "!" && input.visualCursor.offset === 0) {
891946
setStore("placeholder", Math.floor(Math.random() * SHELL_PLACEHOLDERS.length))
892947
setStore("mode", "shell")
@@ -1143,6 +1198,18 @@ export function Prompt(props: PromptProps) {
11431198
<Show when={status().type !== "retry"}>
11441199
<box gap={2} flexDirection="row">
11451200
<Switch>
1201+
<Match when={history.searching}>
1202+
<text fg={theme.primary}>
1203+
(search) <span style={{ fg: theme.text }}>{history.query}</span>
1204+
<span style={{ fg: theme.textMuted }}>{history.query ? "" : "type to search"}</span>
1205+
</text>
1206+
<text fg={theme.text}>
1207+
{keybind.print("history_search")} <span style={{ fg: theme.textMuted }}>next</span>
1208+
</text>
1209+
<text fg={theme.text}>
1210+
esc <span style={{ fg: theme.textMuted }}>cancel</span>
1211+
</text>
1212+
</Match>
11461213
<Match when={store.mode === "normal"}>
11471214
<Show when={local.model.variant.list().length > 0}>
11481215
<text fg={theme.text}>

‎packages/opencode/src/config/config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ export namespace Config {
943943
.describe("Delete word backward in input"),
944944
history_previous: z.string().optional().default("up").describe("Previous history item"),
945945
history_next: z.string().optional().default("down").describe("Next history item"),
946+
history_search: z.string().optional().default("ctrl+r").describe("Reverse search prompt history"),
946947
session_child_first: z.string().optional().default("<leader>down").describe("Go to first child session"),
947948
session_child_cycle: z.string().optional().default("right").describe("Go to next child session"),
948949
session_child_cycle_reverse: z.string().optional().default("left").describe("Go to previous child session"),

0 commit comments

Comments
 (0)