|
1 | 1 | import { createStore, produce } from "solid-js/store" |
2 | 2 | import { createSimpleContext } from "@opencode-ai/ui/context" |
3 | | -import { batch, createEffect, createMemo, createRoot, onCleanup } from "solid-js" |
| 3 | +import { batch, createEffect, createMemo, createRoot, on, onCleanup } from "solid-js" |
4 | 4 | import { useParams } from "@solidjs/router" |
5 | 5 | import { useSDK } from "./sdk" |
6 | 6 | import type { Platform } from "./platform" |
@@ -38,6 +38,16 @@ type TerminalCacheEntry = { |
38 | 38 |
|
39 | 39 | const caches = new Set<Map<string, TerminalCacheEntry>>() |
40 | 40 |
|
| 41 | +const trimTerminal = (pty: LocalPTY) => { |
| 42 | + if (!pty.buffer && pty.cursor === undefined && pty.scrollY === undefined) return pty |
| 43 | + return { |
| 44 | + ...pty, |
| 45 | + buffer: undefined, |
| 46 | + cursor: undefined, |
| 47 | + scrollY: undefined, |
| 48 | + } |
| 49 | +} |
| 50 | + |
41 | 51 | export function clearWorkspaceTerminals(dir: string, sessionIDs?: string[], platform?: Platform) { |
42 | 52 | const key = getWorkspaceTerminalCacheKey(dir) |
43 | 53 | for (const cache of caches) { |
@@ -188,6 +198,18 @@ function createWorkspaceTerminalSession(sdk: ReturnType<typeof useSDK>, dir: str |
188 | 198 | console.error("Failed to update terminal", error) |
189 | 199 | }) |
190 | 200 | }, |
| 201 | + trim(id: string) { |
| 202 | + const index = store.all.findIndex((x) => x.id === id) |
| 203 | + if (index === -1) return |
| 204 | + setStore("all", index, (pty) => trimTerminal(pty)) |
| 205 | + }, |
| 206 | + trimAll() { |
| 207 | + setStore("all", (all) => { |
| 208 | + const next = all.map(trimTerminal) |
| 209 | + if (next.every((pty, index) => pty === all[index])) return all |
| 210 | + return next |
| 211 | + }) |
| 212 | + }, |
191 | 213 | async clone(id: string) { |
192 | 214 | const index = store.all.findIndex((x) => x.id === id) |
193 | 215 | const pty = store.all[index] |
@@ -322,12 +344,27 @@ export const { use: useTerminal, provider: TerminalProvider } = createSimpleCont |
322 | 344 |
|
323 | 345 | const workspace = createMemo(() => loadWorkspace(params.dir!, params.id)) |
324 | 346 |
|
| 347 | + createEffect( |
| 348 | + on( |
| 349 | + () => ({ dir: params.dir, id: params.id }), |
| 350 | + (next, prev) => { |
| 351 | + if (!prev?.dir) return |
| 352 | + if (next.dir === prev.dir && next.id === prev.id) return |
| 353 | + if (next.dir === prev.dir && next.id) return |
| 354 | + loadWorkspace(prev.dir, prev.id).trimAll() |
| 355 | + }, |
| 356 | + { defer: true }, |
| 357 | + ), |
| 358 | + ) |
| 359 | + |
325 | 360 | return { |
326 | 361 | ready: () => workspace().ready(), |
327 | 362 | all: () => workspace().all(), |
328 | 363 | active: () => workspace().active(), |
329 | 364 | new: () => workspace().new(), |
330 | 365 | update: (pty: Partial<LocalPTY> & { id: string }) => workspace().update(pty), |
| 366 | + trim: (id: string) => workspace().trim(id), |
| 367 | + trimAll: () => workspace().trimAll(), |
331 | 368 | clone: (id: string) => workspace().clone(id), |
332 | 369 | open: (id: string) => workspace().open(id), |
333 | 370 | close: (id: string) => workspace().close(id), |
|
0 commit comments