Update lxc-terminal-modal.tsx

This commit is contained in:
MacRimi
2026-01-31 18:29:19 +01:00
parent 5bfc911e1b
commit 58a4e475ad

View File

@@ -56,19 +56,13 @@ export function LxcTerminalModal({
const [isTablet, setIsTablet] = useState(false) const [isTablet, setIsTablet] = useState(false)
const isInsideLxcRef = useRef(false) const isInsideLxcRef = useRef(false)
const outputBufferRef = useRef<string>("") const outputBufferRef = useRef<string>("")
const hostPromptPatternRef = useRef<string>("")
const [modalHeight, setModalHeight] = useState(500) const [modalHeight, setModalHeight] = useState(500)
const [isResizing, setIsResizing] = useState(false) const [isResizing, setIsResizing] = useState(false)
const resizeBarRef = useRef<HTMLDivElement>(null) const resizeBarRef = useRef<HTMLDivElement>(null)
const modalHeightRef = useRef(500) const modalHeightRef = useRef(500)
const [showLoginModal, setShowLoginModal] = useState(false)
const [loginUsername, setLoginUsername] = useState("")
const [loginPassword, setLoginPassword] = useState("")
const [loginError, setLoginError] = useState("")
const [isLoggingIn, setIsLoggingIn] = useState(false)
const waitingForPasswordRef = useRef(false)
// Detect mobile/tablet // Detect mobile/tablet
useEffect(() => { useEffect(() => {
const checkDevice = () => { const checkDevice = () => {
@@ -99,6 +93,7 @@ export function LxcTerminalModal({
setConnectionStatus("connecting") setConnectionStatus("connecting")
isInsideLxcRef.current = false isInsideLxcRef.current = false
outputBufferRef.current = "" outputBufferRef.current = ""
hostPromptPatternRef.current = ""
} }
}, [isOpen]) }, [isOpen])
@@ -234,40 +229,46 @@ export function LxcTerminalModal({
// pct enter always enters directly without login prompt when run as root // pct enter always enters directly without login prompt when run as root
if (!isInsideLxcRef.current) { if (!isInsideLxcRef.current) {
outputBufferRef.current += event.data outputBufferRef.current += event.data
// Detect when we're inside the LXC container
// The LXC prompt will NOT contain "constructor" (the host name)
// It will be something like "root@plex:/#" or "user@containername:~$"
const buffer = outputBufferRef.current const buffer = outputBufferRef.current
// Look for a prompt that: // First, capture the host prompt pattern (before pct enter)
// 1. Comes after pct enter command // This will be something like "[root@hostname" or "user@hostname"
// 2. Has @ followed by container name (not host name) if (!hostPromptPatternRef.current) {
// 3. Ends with # or $ const hostPromptMatch = buffer.match(/\[?(\w+@[\w-]+)/)
if (hostPromptMatch) {
hostPromptPatternRef.current = hostPromptMatch[1]
}
}
// Look for pct enter command completion
const pctEnterMatch = buffer.match(/pct enter \d+\r?\n/) const pctEnterMatch = buffer.match(/pct enter \d+\r?\n/)
if (pctEnterMatch) { if (pctEnterMatch && hostPromptPatternRef.current) {
const afterPctEnter = buffer.substring(buffer.indexOf(pctEnterMatch[0]) + pctEnterMatch[0].length) const afterPctEnter = buffer.substring(buffer.indexOf(pctEnterMatch[0]) + pctEnterMatch[0].length)
// Find the LXC prompt - it should be a line ending with :~# :~$ :/# or similar // Find the LXC prompt - a prompt that does NOT match the host prompt
// and NOT containing the host name "constructor" // Extract hostname from host prompt (e.g., "root@constructor" -> "constructor")
const lxcPromptMatch = afterPctEnter.match(/\r?\n?([^\r\n]*@(?!constructor)[^\r\n]*[#$]\s*)$/) const hostName = hostPromptPatternRef.current.split('@')[1]
// Create regex that matches prompts NOT containing the host name
const lxcPromptRegex = new RegExp(`\\r?\\n?([^\\r\\n]*@(?!${hostName})[^\\r\\n]*[#$]\\s*)$`)
const lxcPromptMatch = afterPctEnter.match(lxcPromptRegex)
if (lxcPromptMatch) { if (lxcPromptMatch) {
// Successfully inside LXC - only show from the LXC prompt onwards // Successfully inside LXC - only show from the LXC prompt onwards
isInsideLxcRef.current = true isInsideLxcRef.current = true
term.write(lxcPromptMatch[1])
// Find where the LXC prompt line starts
const promptStart = afterPctEnter.lastIndexOf(lxcPromptMatch[1])
if (promptStart !== -1) {
// Only show the LXC prompt itself
term.write(lxcPromptMatch[1])
}
return return
} }
} }
} else { } else {
// Already inside LXC, write directly // Already inside LXC, write directly
term.write(event.data) term.write(event.data)
// Detect if user exited the LXC (returned to host prompt)
if (hostPromptPatternRef.current && event.data.includes(hostPromptPatternRef.current)) {
// User exited the LXC, close the modal
onClose()
}
} }
} }
} }