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
3 changes: 0 additions & 3 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
"@vercel/otel": "^2.1.2",
"@vercel/speed-insights": "^1.2.0",
"@xterm/addon-canvas": "^0.7.0",
"@xterm/addon-fit": "^0.11.0",
"@xterm/addon-webgl": "^0.19.0",
"@xterm/xterm": "^6.0.0",
"cheerio": "^1.0.0",
Expand Down
49 changes: 24 additions & 25 deletions src/features/dashboard/terminal/dashboard-terminal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import { type CommandHandle, type Sandbox } from 'e2b'
import { useCallback, useEffect, useRef, useState } from 'react'
import type { CommandHandle, Sandbox } from 'e2b'
import { useCallback, useEffect, useEffectEvent, useRef, useState } from 'react'
import type { SandboxManagementAuth } from '@/core/shared/sandbox-management-auth'
import {
DEFAULT_CWD,
Expand Down Expand Up @@ -192,7 +192,7 @@ export default function DashboardTerminal({

const resizePty = useCallback((size: { cols: number; rows: number }) => {
if (sandboxRef.current && pidRef.current) {
void sandboxRef.current.pty.resize(pidRef.current, size)
void sandboxRef.current.pty.resize(pidRef.current, size).catch(() => {})
}
}, [])

Expand Down Expand Up @@ -378,7 +378,7 @@ export default function DashboardTerminal({
})
ptyRef.current = pty
pidRef.current = pty.pid
resizeTerminal()
resizeTerminal({ force: true })
setStatus('ready')
appendOutput(`PTY ${pty.pid} attached.\r\n`)
focusTerminal()
Expand Down Expand Up @@ -555,38 +555,37 @@ export default function DashboardTerminal({
}
}, [autoStart, launchTarget, queueTerminalCommand, status])

useEffect(() => {
const handlePageHide = (event: PageTransitionEvent) => {
if (event.persisted) return
const handlePageHide = useEffectEvent((event: PageTransitionEvent) => {
if (event.persisted) return

abortCurrentStart()
void closeTerminal()
}
abortCurrentStart()
void closeTerminal()
})

const handlePageShow = (event: PageTransitionEvent) => {
if (!event.persisted || !ptyRef.current) return
const handlePageShow = useEffectEvent((event: PageTransitionEvent) => {
if (!event.persisted || !ptyRef.current) return

resizeTerminal()
focusTerminal()
}
resizeTerminal({ force: true })
focusTerminal()
Comment thread
cursor[bot] marked this conversation as resolved.
})

const handleTerminalUnmount = useEffectEvent(() => {
startGenerationRef.current += 1
isStartingRef.current = false
clearPendingInput()
void closeTerminal()
})

useEffect(() => {
window.addEventListener('pagehide', handlePageHide)
window.addEventListener('pageshow', handlePageShow)

return () => {
window.removeEventListener('pagehide', handlePageHide)
window.removeEventListener('pageshow', handlePageShow)
abortCurrentStart()
clearPendingInput()
void closeTerminal()
handleTerminalUnmount()
}
}, [
abortCurrentStart,
clearPendingInput,
closeTerminal,
focusTerminal,
resizeTerminal,
])
}, [])

return (
<>
Expand Down
49 changes: 42 additions & 7 deletions src/features/dashboard/terminal/terminal-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ const MAX_CELL_WIDTH_PX = 16
const MIN_CELL_HEIGHT_PX = 8
const MAX_CELL_HEIGHT_PX = 40

type XTermWithRenderDimensions = XTerm & {
_core?: {
_renderService?: {
dimensions?: {
css?: {
cell?: {
width?: number
height?: number
}
}
}
}
}
}

function getElementSize(element: Element | null) {
if (!element) return undefined

Expand All @@ -21,18 +36,41 @@ function getElementSize(element: Element | null) {
return rect
}

function getRenderCellSize(terminal: XTerm | null) {
const cell = (terminal as XTermWithRenderDimensions | null)?._core
?._renderService?.dimensions?.css?.cell

if (!cell?.width && !cell?.height) return undefined

return {
width: cell.width,
height: cell.height,
}
}

function getMeasuredCellSize(terminal: XTerm | null) {
const renderCellSize = getRenderCellSize(terminal)
const measureElement = terminal?.element?.querySelector(
'.xterm-char-measure-element'
)
const rowElement = terminal?.element?.querySelector('.xterm-rows > div')
const helperTextArea = terminal?.element?.querySelector(
'.xterm-helper-textarea'
)
const measuredCharSize = getElementSize(measureElement ?? null)
const rowSize = getElementSize(rowElement ?? null)
const helperSize = getElementSize(helperTextArea ?? null)

if (!measuredCharSize && !rowSize) return undefined
if (!renderCellSize && !measuredCharSize && !rowSize && !helperSize) {
return undefined
}

const measuredWidth = measuredCharSize?.width
const measuredHeight = rowSize?.height ?? measuredCharSize?.height
const measuredWidth = renderCellSize?.width ?? measuredCharSize?.width
const measuredHeight =
renderCellSize?.height ??
rowSize?.height ??
measuredCharSize?.height ??
helperSize?.height

return {
width:
Expand Down Expand Up @@ -78,9 +116,6 @@ export function calculateTerminalSize(

return {
cols: Math.max(MIN_TERMINAL_COLS, Math.floor(availableWidth / cellWidth)),
rows: Math.max(
MIN_TERMINAL_ROWS,
Math.floor(availableHeight / cellHeight) - 1
),
rows: Math.max(MIN_TERMINAL_ROWS, Math.floor(availableHeight / cellHeight)),
}
}
Loading
Loading