fix: terminal wrap error
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"bufio"
|
"bufio"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -158,6 +159,20 @@ func (tc *TerminalController) handlePtyMode(conn *websocket.Conn, userID string)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理调整窗口大小的消息
|
||||||
|
if len(message) > 0 && message[0] == '{' {
|
||||||
|
var resizeMsg struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Rows uint16 `json:"rows"`
|
||||||
|
Cols uint16 `json:"cols"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(message, &resizeMsg); err == nil && resizeMsg.Type == "resize" {
|
||||||
|
pty.Setsize(ptmx, &pty.Winsize{Rows: resizeMsg.Rows, Cols: resizeMsg.Cols})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := ptmx.Write(message); err != nil {
|
if _, err := ptmx.Write(message); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -249,6 +264,16 @@ func (tc *TerminalController) handlePipeMode(conn *websocket.Conn, userID string
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
// 过滤调整窗口大小的消息(Windows Pipe 模式不支持调整尺寸,需过滤掉避免写入 stdin)
|
||||||
|
if len(message) > 0 && message[0] == '{' {
|
||||||
|
var resizeMsg struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(message, &resizeMsg); err == nil && resizeMsg.Type == "resize" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := stdin.Write(message); err != nil {
|
if _, err := stdin.Write(message); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -177,15 +177,19 @@ function connectWebSocket() {
|
|||||||
}, 100)
|
}, 100)
|
||||||
}
|
}
|
||||||
terminal?.focus()
|
terminal?.focus()
|
||||||
|
// 连接成功后立即触发一次尺寸同步
|
||||||
|
handleResize()
|
||||||
}
|
}
|
||||||
|
|
||||||
ws.onmessage = (event) => {
|
ws.onmessage = (event) => {
|
||||||
if (event.data === '__PTY_MODE__') {
|
if (event.data === '__PTY_MODE__') {
|
||||||
isPtyMode = true
|
isPtyMode = true
|
||||||
|
handleResize() // 关键:模式确认后立即同步一次尺寸
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (event.data === '__PIPE_MODE__') {
|
if (event.data === '__PIPE_MODE__') {
|
||||||
isPtyMode = false
|
isPtyMode = false
|
||||||
|
handleResize()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
terminal?.write(event.data)
|
terminal?.write(event.data)
|
||||||
@@ -239,6 +243,14 @@ function dispose() {
|
|||||||
function handleResize() {
|
function handleResize() {
|
||||||
try {
|
try {
|
||||||
fitAddon?.fit()
|
fitAddon?.fit()
|
||||||
|
// 通知后端调整 PTY 尺寸
|
||||||
|
if (isPtyMode && terminal && ws && ws.readyState === WebSocket.OPEN) {
|
||||||
|
ws.send(JSON.stringify({
|
||||||
|
type: 'resize',
|
||||||
|
cols: terminal.cols,
|
||||||
|
rows: terminal.rows
|
||||||
|
}))
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Terminal resize failed:', e)
|
console.warn('Terminal resize failed:', e)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user