The PTY was hardcoded to 80x24 at start, so full-screen programs
(htop, vim, less, tmux, top) drew themselves for 80x24 regardless
of the actual xterm window. Add a path for the frontend to push the
real cols/rows to the backend, which calls pty.Setsize on the pty
file.
Backend:
- internal/shell/manager.go: Resize(workspaceID, cols, rows) added to
the Manager interface and implemented on LocalManager. Looks up the
session, type-asserts sess.Stdin.(*os.File), calls pty.Setsize.
Validates cols/rows in [1, 10000] and returns CodeBadRequest on
bad input, CodeNotFound when no session.
- internal/service/shell_service.go: ShellService.Resize wrapper that
checks workspace existence first.
- internal/api/shell_handler.go: resize handler, 204 on success.
- internal/api/router.go: register POST /workspaces/:id/shell/resize.
- internal/model/shell.go: ShellResizeRequest{Cols, Rows}.
- internal/shell/manager_test.go: 3 new tests (valid resize via
pty.Getsize round-trip, invalid size, missing session).
Frontend:
- web/src/lib/api/process.ts: useShellResize mutation hook.
- web/src/components/terminal/TerminalPanel.tsx: terminal.onResize
subscription with 100ms debounce; filters 0x0; only fires when
workspaceId is set; cleanup clears timeout + disposes listener.
E2E: 'stty size' after POST /shell/resize {cols:200, rows:50} returns
'50 200'. 0x40 → 400, missing workspace → 404, valid → 204.
Conversation: 019f360a-5eba-7c81-94d3-e5d58ad3c026
112 lines
3.4 KiB
Go
112 lines
3.4 KiB
Go
package service
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"io"
|
|
|
|
"codespace/internal/shell"
|
|
"codespace/internal/workspace"
|
|
)
|
|
|
|
// ShellService manages interactive shells for workspaces.
|
|
type ShellService struct {
|
|
workspaces workspace.Manager
|
|
shells shell.Manager
|
|
logger *slog.Logger
|
|
}
|
|
|
|
// NewShellService creates a ShellService.
|
|
// If lg is nil, slog.Default() is used.
|
|
func NewShellService(workspaces workspace.Manager, shells shell.Manager, lg *slog.Logger) *ShellService {
|
|
if lg == nil {
|
|
lg = slog.Default()
|
|
}
|
|
return &ShellService{workspaces: workspaces, shells: shells, logger: lg}
|
|
}
|
|
|
|
// Start starts an interactive shell for the given workspace.
|
|
func (s *ShellService) Start(workspaceID string) error {
|
|
ws, err := s.workspaces.Get(workspaceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := s.shells.Start(workspaceID, ws.Root); err != nil {
|
|
s.logger.Error("shell start failed", "workspace_id", workspaceID, "error", err)
|
|
return err
|
|
}
|
|
status := s.shells.Status(workspaceID)
|
|
s.logger.Info("shell started", "workspace_id", workspaceID, "pid", status.PID)
|
|
return nil
|
|
}
|
|
|
|
// Stop stops the interactive shell for the given workspace.
|
|
func (s *ShellService) Stop(workspaceID string) error {
|
|
if err := s.shells.Stop(workspaceID); err != nil {
|
|
s.logger.Error("shell stop failed", "workspace_id", workspaceID, "error", err)
|
|
return err
|
|
}
|
|
s.logger.Info("shell stopped", "workspace_id", workspaceID)
|
|
return nil
|
|
}
|
|
|
|
// Restart restarts the interactive shell for the given workspace.
|
|
func (s *ShellService) Restart(workspaceID string) error {
|
|
ws, err := s.workspaces.Get(workspaceID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := s.shells.Restart(workspaceID, ws.Root); err != nil {
|
|
s.logger.Error("shell restart failed", "workspace_id", workspaceID, "error", err)
|
|
return err
|
|
}
|
|
status := s.shells.Status(workspaceID)
|
|
s.logger.Info("shell restarted", "workspace_id", workspaceID, "pid", status.PID)
|
|
return nil
|
|
}
|
|
|
|
// Resize resizes the PTY for the given workspace.
|
|
func (s *ShellService) Resize(workspaceID string, cols, rows int) error {
|
|
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
|
return err
|
|
}
|
|
if err := s.shells.Resize(workspaceID, cols, rows); err != nil {
|
|
s.logger.Error("shell resize failed", "workspace_id", workspaceID, "error", err)
|
|
return err
|
|
}
|
|
s.logger.Info("shell resized", "workspace_id", workspaceID, "cols", cols, "rows", rows)
|
|
return nil
|
|
}
|
|
|
|
// Status returns the shell status for the given workspace.
|
|
func (s *ShellService) Status(workspaceID string) (shell.Status, error) {
|
|
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
|
return shell.Status{}, err
|
|
}
|
|
return s.shells.Status(workspaceID), nil
|
|
}
|
|
|
|
// Subscribe subscribes to output events for the workspace shell.
|
|
func (s *ShellService) Subscribe(workspaceID string) (shell.Subscription, error) {
|
|
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
|
return nil, err
|
|
}
|
|
return s.shells.Subscribe(workspaceID)
|
|
}
|
|
|
|
// Input returns the stdin writer for the workspace shell.
|
|
func (s *ShellService) Input(workspaceID string) (io.WriteCloser, error) {
|
|
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
|
return nil, err
|
|
}
|
|
return s.shells.Stdin(workspaceID)
|
|
}
|
|
|
|
// ExitStatus returns the exit status for the workspace shell.
|
|
func (s *ShellService) ExitStatus(workspaceID string) (shell.ExitInfo, error) {
|
|
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
|
return shell.ExitInfo{}, err
|
|
}
|
|
return s.shells.ExitStatus(workspaceID)
|
|
}
|