2026-02-17 01:36:01 +09:00
|
|
|
import type { TmuxConfig } from "../../config/schema"
|
|
|
|
|
import type { applyLayout, closeTmuxPane, enforceMainPaneWidth, replaceTmuxPane, spawnTmuxPane } from "../../shared/tmux"
|
|
|
|
|
import type { PaneAction, WindowState } from "./types"
|
|
|
|
|
|
|
|
|
|
export interface ActionResult {
|
|
|
|
|
success: boolean
|
|
|
|
|
paneId?: string
|
|
|
|
|
error?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ExecuteContext {
|
|
|
|
|
config: TmuxConfig
|
|
|
|
|
serverUrl: string
|
|
|
|
|
windowState: WindowState
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ActionExecutorDeps {
|
|
|
|
|
spawnTmuxPane: typeof spawnTmuxPane
|
|
|
|
|
closeTmuxPane: typeof closeTmuxPane
|
|
|
|
|
replaceTmuxPane: typeof replaceTmuxPane
|
|
|
|
|
applyLayout: typeof applyLayout
|
|
|
|
|
enforceMainPaneWidth: typeof enforceMainPaneWidth
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 03:40:46 +09:00
|
|
|
async function enforceMainPane(
|
|
|
|
|
windowState: WindowState,
|
|
|
|
|
config: TmuxConfig,
|
|
|
|
|
deps: ActionExecutorDeps,
|
|
|
|
|
): Promise<void> {
|
2026-02-17 01:36:01 +09:00
|
|
|
if (!windowState.mainPane) return
|
2026-02-17 03:40:46 +09:00
|
|
|
await deps.enforceMainPaneWidth(
|
|
|
|
|
windowState.mainPane.paneId,
|
|
|
|
|
windowState.windowWidth,
|
|
|
|
|
config.main_pane_size,
|
|
|
|
|
)
|
2026-02-17 01:36:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function executeActionWithDeps(
|
|
|
|
|
action: PaneAction,
|
|
|
|
|
ctx: ExecuteContext,
|
|
|
|
|
deps: ActionExecutorDeps,
|
|
|
|
|
): Promise<ActionResult> {
|
|
|
|
|
if (action.type === "close") {
|
|
|
|
|
const success = await deps.closeTmuxPane(action.paneId)
|
|
|
|
|
if (success) {
|
2026-02-17 03:40:46 +09:00
|
|
|
await enforceMainPane(ctx.windowState, ctx.config, deps)
|
2026-02-17 01:36:01 +09:00
|
|
|
}
|
|
|
|
|
return { success }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (action.type === "replace") {
|
|
|
|
|
const result = await deps.replaceTmuxPane(
|
|
|
|
|
action.paneId,
|
|
|
|
|
action.newSessionId,
|
|
|
|
|
action.description,
|
|
|
|
|
ctx.config,
|
|
|
|
|
ctx.serverUrl,
|
|
|
|
|
)
|
|
|
|
|
return {
|
|
|
|
|
success: result.success,
|
|
|
|
|
paneId: result.paneId,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await deps.spawnTmuxPane(
|
|
|
|
|
action.sessionId,
|
|
|
|
|
action.description,
|
|
|
|
|
ctx.config,
|
|
|
|
|
ctx.serverUrl,
|
|
|
|
|
action.targetPaneId,
|
|
|
|
|
action.splitDirection,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (result.success) {
|
2026-02-17 03:40:46 +09:00
|
|
|
await enforceMainPane(ctx.windowState, ctx.config, deps)
|
2026-02-17 01:36:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
success: result.success,
|
|
|
|
|
paneId: result.paneId,
|
|
|
|
|
}
|
|
|
|
|
}
|