Skip to content

Commit 512e3e9

Browse files
committed
enable nav up/down within an input
1 parent 48ef159 commit 512e3e9

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

cli/src/chat.tsx

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
22
import { useShallow } from 'zustand/react/shallow'
33

4-
import type { ContentBlock } from './types/chat'
5-
64
import { routeUserPrompt } from './commands/router'
75
import { AgentModeToggle } from './components/agent-mode-toggle'
86
import { BuildModeButtons } from './components/build-mode-buttons'
@@ -39,6 +37,7 @@ import { createMarkdownPalette } from './utils/theme-system'
3937
import { BORDER_CHARS } from './utils/ui-constants'
4038

4139
import type { SendMessageTimerEvent } from './hooks/use-send-message'
40+
import type { ContentBlock } from './types/chat'
4241
import type { SendMessageFn } from './types/contracts/send-message'
4342
import type { ScrollBoxRenderable } from '@opentui/core'
4443

@@ -303,31 +302,56 @@ export const Chat = ({
303302
setSuggestionMenuDisabled(!lastEditDueToNav)
304303
}, [setSuggestionMenuDisabled, lastEditDueToNav])
305304

306-
// Disable history navigation during suggesion menu
307-
const [historyNavEnabled, setHistoryNavEnabled] = useState(true)
305+
const [historyNavUpEnabled, setHistoryNavUpEnabled] = useState(true)
308306
useEffect(() => {
309307
if (lastEditDueToNav) {
310-
setHistoryNavEnabled(true)
308+
setHistoryNavUpEnabled(true)
311309
return
312310
}
313311

314312
if (slashContext.active) {
315-
setHistoryNavEnabled(false)
313+
setHistoryNavUpEnabled(false)
316314
return
317315
}
318316
if (mentionContext.active) {
319-
setHistoryNavEnabled(false)
317+
setHistoryNavUpEnabled(false)
320318
return
321319
}
322320

323-
setHistoryNavEnabled(true)
321+
setHistoryNavUpEnabled(cursorPosition === 0)
324322
}, [
325-
setHistoryNavEnabled,
323+
setHistoryNavUpEnabled,
326324
lastEditDueToNav,
327325
slashContext.active,
328326
mentionContext.active,
327+
cursorPosition,
329328
])
330329

330+
const [historyNavDownEnabled, setHistoryNavDownEnabled] = useState(true)
331+
useEffect(() => {
332+
if (lastEditDueToNav) {
333+
setHistoryNavDownEnabled(true)
334+
return
335+
}
336+
337+
if (slashContext.active) {
338+
setHistoryNavDownEnabled(false)
339+
return
340+
}
341+
if (mentionContext.active) {
342+
setHistoryNavDownEnabled(false)
343+
return
344+
}
345+
346+
setHistoryNavDownEnabled(inputValue.length === cursorPosition)
347+
}, [
348+
setHistoryNavDownEnabled,
349+
lastEditDueToNav,
350+
slashContext.active,
351+
mentionContext.active,
352+
cursorPosition,
353+
inputValue.length,
354+
])
331355
useEffect(() => {
332356
if (!slashContext.active) {
333357
setSlashSelectedIndex(0)
@@ -711,7 +735,8 @@ export const Chat = ({
711735
navigateDown,
712736
toggleAgentMode,
713737
onCtrlC: handleCtrlC,
714-
historyNavEnabled,
738+
historyNavUpEnabled,
739+
historyNavDownEnabled,
715740
})
716741

717742
const { tree: messageTree, topLevelMessages } = useMemo(

cli/src/components/multiline-input.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,18 @@ export const MultilineInput = forwardRef<
574574
return
575575
}
576576

577+
// Up arrow (no modifiers)
578+
if (key.name === 'up' && !key.ctrl && !key.meta && !key.alt) {
579+
preventKeyDefault(key)
580+
setCursorPosition(cursorPosition - getEffectiveCols())
581+
}
582+
583+
// Down arrow (no modifiers)
584+
if (key.name === 'down' && !key.ctrl && !key.meta && !key.alt) {
585+
preventKeyDefault(key)
586+
setCursorPosition(cursorPosition + getEffectiveCols())
587+
}
588+
577589
// Regular character input
578590
if (
579591
key.sequence &&
@@ -680,7 +692,7 @@ export const MultilineInput = forwardRef<
680692
},
681693
}}
682694
>
683-
<text style={{ ...textStyle, wrapMode: 'word' }}>
695+
<text style={{ ...textStyle, wrapMode: 'char' }}>
684696
{showCursor ? (
685697
<>
686698
{beforeCursor}

cli/src/hooks/use-keyboard-handlers.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ interface KeyboardHandlersConfig {
1616
navigateDown: () => void
1717
toggleAgentMode: () => void
1818
onCtrlC: () => boolean
19-
historyNavEnabled: boolean
19+
historyNavUpEnabled: boolean
20+
historyNavDownEnabled: boolean
2021
}
2122

2223
export const useKeyboardHandlers = ({
@@ -32,7 +33,8 @@ export const useKeyboardHandlers = ({
3233
navigateDown,
3334
toggleAgentMode,
3435
onCtrlC,
35-
historyNavEnabled,
36+
historyNavUpEnabled,
37+
historyNavDownEnabled,
3638
}: KeyboardHandlersConfig) => {
3739
useKeyboard(
3840
useCallback(
@@ -147,8 +149,6 @@ export const useKeyboardHandlers = ({
147149
useKeyboard(
148150
useCallback(
149151
(key) => {
150-
if (!historyNavEnabled) return
151-
152152
const isUpArrow =
153153
key.name === 'up' && !key.ctrl && !key.meta && !key.shift
154154
const isDownArrow =
@@ -164,12 +164,14 @@ export const useKeyboardHandlers = ({
164164
}
165165

166166
if (isUpArrow) {
167+
if (!historyNavUpEnabled) return
167168
navigateUp()
168169
} else {
170+
if (!historyNavDownEnabled) return
169171
navigateDown()
170172
}
171173
},
172-
[historyNavEnabled, navigateUp, navigateDown],
174+
[historyNavUpEnabled, historyNavDownEnabled, navigateUp, navigateDown],
173175
),
174176
)
175177

0 commit comments

Comments
 (0)