Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions cli/src/components/freebuff-model-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import { useFreebuffModelStore } from '../state/freebuff-model-store'
import { useFreebuffSessionStore } from '../state/freebuff-session-store'
import { useTerminalDimensions } from '../hooks/use-terminal-dimensions'
import { useTheme } from '../hooks/use-theme'
import { nextFreebuffModelId } from '../utils/freebuff-model-navigation'
import {
freebuffModelNavigationDirectionForKey,
nextFreebuffModelId,
} from '../utils/freebuff-model-navigation'

import type { FreebuffModelOption } from '@codebuff/common/constants/freebuff-models'
import type { KeyEvent } from '@opentui/core'
Expand All @@ -32,6 +35,9 @@ const FREEBUFF_MODEL_SELECTOR_MODELS: readonly FreebuffModelOption[] = [
...FREEBUFF_MODELS.filter((model) => model.id === DEFAULT_FREEBUFF_MODEL_ID),
...FREEBUFF_MODELS.filter((model) => model.id !== DEFAULT_FREEBUFF_MODEL_ID),
]
const FREEBUFF_MODEL_SELECTOR_MODEL_IDS = FREEBUFF_MODEL_SELECTOR_MODELS.map(
(model) => model.id,
)

function formatSessionUnits(units: number): string {
return Number.isInteger(units) ? String(units) : units.toFixed(1)
Expand Down Expand Up @@ -213,27 +219,26 @@ export const FreebuffModelSelector: React.FC = () => {
(key: KeyEvent) => {
if (pending) return
const name = key.name ?? ''
const isForward =
name === 'right' || name === 'down' || (name === 'tab' && !key.shift)
const isBackward =
name === 'left' || name === 'up' || (name === 'tab' && key.shift)
const direction = freebuffModelNavigationDirectionForKey(key)
const isCommit =
name === 'return' || name === 'enter' || name === 'space'
if (!isForward && !isBackward && !isCommit) return
if (isCommit) {
if (isJoinable(focusedId) && focusedId !== committedModelId) {
key.preventDefault?.()
key.stopPropagation?.()
pick(focusedId)
}
return
}
if (!direction) return
const targetId = nextFreebuffModelId({
modelIds: FREEBUFF_MODEL_SELECTOR_MODELS.map((model) => model.id),
modelIds: FREEBUFF_MODEL_SELECTOR_MODEL_IDS,
focusedId,
direction: isForward ? 'forward' : 'backward',
direction,
})
if (targetId) {
key.preventDefault?.()
key.stopPropagation?.()
setFocusedId(targetId)
}
},
Expand Down
53 changes: 52 additions & 1 deletion cli/src/utils/__tests__/freebuff-model-navigation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { describe, expect, test } from 'bun:test'

import { nextFreebuffModelId } from '../freebuff-model-navigation'
import {
freebuffModelNavigationDirectionForKey,
nextFreebuffModelId,
} from '../freebuff-model-navigation'

describe('nextFreebuffModelId', () => {
test('moves to the next model when moving forward', () => {
Expand Down Expand Up @@ -49,3 +52,51 @@ describe('nextFreebuffModelId', () => {
).toBeNull()
})
})

describe('freebuffModelNavigationDirectionForKey', () => {
test('maps arrow keys to model navigation directions', () => {
expect(freebuffModelNavigationDirectionForKey({ name: 'down' })).toBe(
'forward',
)
expect(freebuffModelNavigationDirectionForKey({ name: 'right' })).toBe(
'forward',
)
expect(freebuffModelNavigationDirectionForKey({ name: 'up' })).toBe(
'backward',
)
expect(freebuffModelNavigationDirectionForKey({ name: 'left' })).toBe(
'backward',
)
})

test('maps tab and shift-tab to model navigation directions', () => {
expect(freebuffModelNavigationDirectionForKey({ name: 'tab' })).toBe(
'forward',
)
expect(
freebuffModelNavigationDirectionForKey({ name: 'tab', shift: true }),
).toBe('backward')
})

test('maps terminal tab sequences to model navigation directions', () => {
expect(freebuffModelNavigationDirectionForKey({ sequence: '\t' })).toBe(
'forward',
)
expect(
freebuffModelNavigationDirectionForKey({ sequence: '\x1b[9u' }),
).toBe('forward')
expect(
freebuffModelNavigationDirectionForKey({ sequence: '\x1b[Z' }),
).toBe('backward')
expect(
freebuffModelNavigationDirectionForKey({ sequence: '\x1b[9;2u' }),
).toBe('backward')
expect(
freebuffModelNavigationDirectionForKey({ sequence: '\x1b[27;2;9~' }),
).toBe('backward')
})

test('ignores non-navigation keys', () => {
expect(freebuffModelNavigationDirectionForKey({ name: 'enter' })).toBeNull()
})
})
38 changes: 37 additions & 1 deletion cli/src/utils/freebuff-model-navigation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
export type FreebuffModelNavigationDirection = 'forward' | 'backward'

const FORWARD_KEY_NAMES = new Set(['right', 'down'])
const BACKWARD_KEY_NAMES = new Set(['left', 'up'])
const FORWARD_TAB_SEQUENCES = new Set(['\t', '\x1b[9u'])
const BACKWARD_TAB_SEQUENCES = new Set([
'\x1b[Z',
'\x1b[9;2u',
'\x1b[27;2;9~',
])

export function nextFreebuffModelId(params: {
modelIds: readonly string[]
focusedId: string
direction: 'forward' | 'backward'
direction: FreebuffModelNavigationDirection
}): string | null {
const { modelIds, focusedId, direction } = params
if (modelIds.length === 0) return null
Expand All @@ -12,3 +23,28 @@ export function nextFreebuffModelId(params: {
const step = direction === 'forward' ? 1 : -1
return modelIds[(currentIdx + step + modelIds.length) % modelIds.length]
}

export function freebuffModelNavigationDirectionForKey(key: {
name?: string
shift?: boolean
sequence?: string
raw?: string
}): FreebuffModelNavigationDirection | null {
const name = (key.name ?? '').toLowerCase()
const sequence = key.sequence ?? key.raw ?? ''

if (FORWARD_KEY_NAMES.has(name)) return 'forward'
if (BACKWARD_KEY_NAMES.has(name)) return 'backward'

if (
(name === 'tab' && Boolean(key.shift)) ||
BACKWARD_TAB_SEQUENCES.has(sequence)
) {
return 'backward'
}
if (name === 'tab' || FORWARD_TAB_SEQUENCES.has(sequence)) {
return 'forward'
}

return null
}
Loading