bots-garden/sidekickspublic Fork
⑂ v1.6.0
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/sidekicks.git
git clone ssh://git@rickub.com/bots-garden/sidekicks.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
terminal.go · 103 lines · 2.4 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
// Copyright (c) 2026 Philippe Charrière — Sidekicks Source-Available License 1.0, see LICENSE

package main

import (
	"errors"
	"os"
	"os/exec"
	"sync"

	"github.com/creack/pty"
	"github.com/wailsapp/wails/v3/pkg/application"
)

const eventTerminalData = "terminal:data"

// TerminalService runs one login shell in a PTY and streams its output to the
// frontend as "terminal:data" events. The frontend sends keystrokes back with
// Write and the panel size with Resize.
type TerminalService struct {
	mu   sync.Mutex
	ptmx *os.File
	cmd  *exec.Cmd
}

// NewTerminalService returns a service with no shell running yet.
func NewTerminalService() *TerminalService {
	return &TerminalService{}
}

// Start launches $SHELL (or /bin/sh) in dir with the given size. Calling it
// again while a shell runs does nothing.
func (t *TerminalService) Start(dir string, cols, rows int) error {
	t.mu.Lock()
	defer t.mu.Unlock()
	if t.ptmx != nil {
		return nil
	}

	shell := os.Getenv("SHELL")
	if shell == "" {
		shell = "/bin/sh"
	}
	cmd := exec.Command(shell, "-l")
	cmd.Dir = dir
	cmd.Env = append(os.Environ(), "TERM=xterm-256color")

	ptmx, err := pty.StartWithSize(cmd, &pty.Winsize{Cols: uint16(cols), Rows: uint16(rows)})
	if err != nil {
		return err
	}
	t.ptmx, t.cmd = ptmx, cmd
	go t.pump(ptmx)
	return nil
}

// Write sends keystrokes typed in the terminal panel to the shell.
func (t *TerminalService) Write(data string) error {
	t.mu.Lock()
	defer t.mu.Unlock()
	if t.ptmx == nil {
		return errors.New("terminal not started")
	}
	_, err := t.ptmx.WriteString(data)
	return err
}

// Resize follows the size of the terminal panel (sends SIGWINCH to the shell).
func (t *TerminalService) Resize(cols, rows int) error {
	t.mu.Lock()
	defer t.mu.Unlock()
	if t.ptmx == nil {
		return nil
	}
	return pty.Setsize(t.ptmx, &pty.Winsize{Cols: uint16(cols), Rows: uint16(rows)})
}

// ServiceShutdown kills the shell when the application quits.
func (t *TerminalService) ServiceShutdown() error {
	t.mu.Lock()
	defer t.mu.Unlock()
	if t.ptmx == nil {
		return nil
	}
	_ = t.cmd.Process.Kill()
	return t.ptmx.Close()
}

func (t *TerminalService) pump(ptmx *os.File) {
	buffer := make([]byte, 32*1024)
	for {
		n, err := ptmx.Read(buffer)
		if n > 0 {
			application.Get().Event.Emit(eventTerminalData, string(buffer[:n]))
		}
		if err != nil {
			t.mu.Lock()
			t.ptmx = nil
			t.mu.Unlock()
			return
		}
	}
}