MCPcopy Create free account
hub / github.com/anomalyco/opencode / movePromptHistory

Function movePromptHistory

packages/opencode/src/cli/cmd/run/prompt.shared.ts:96–153  ·  view source on GitHub ↗
(state: PromptHistoryState, dir: -1 | 1, text: string, cursor: number)

Source from the content-addressed store, hash-verified

94}
95
96export function movePromptHistory(state: PromptHistoryState, dir: -1 | 1, text: string, cursor: number): PromptMove {
97 if (state.items.length === 0) {
98 return { state, apply: false }
99 }
100
101 if (dir === -1 && cursor !== 0) {
102 return { state, apply: false }
103 }
104
105 if (dir === 1 && cursor !== Bun.stringWidth(text)) {
106 return { state, apply: false }
107 }
108
109 if (state.index === null) {
110 if (dir === 1) {
111 return { state, apply: false }
112 }
113
114 const idx = state.items.length - 1
115 return {
116 state: {
117 ...state,
118 index: idx,
119 draft: text,
120 },
121 text: state.items[idx].text,
122 cursor: 0,
123 apply: true,
124 }
125 }
126
127 const idx = state.index + dir
128 if (idx < 0) {
129 return { state, apply: false }
130 }
131
132 if (idx >= state.items.length) {
133 return {
134 state: {
135 ...state,
136 index: null,
137 },
138 text: state.draft,
139 cursor: Bun.stringWidth(state.draft),
140 apply: true,
141 }
142 }
143
144 return {
145 state: {
146 ...state,
147 index: idx,
148 },
149 text: state.items[idx].text,
150 cursor: dir === -1 ? 0 : Bun.stringWidth(state.items[idx].text),
151 apply: true,
152 }
153}

Callers 2

moveFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected