mirror of
https://github.com/anotherhadi/iknowyou.git
synced 2026-04-12 00:47:26 +02:00
init
This commit is contained in:
28
back/internal/tools/ptyrun.go
Normal file
28
back/internal/tools/ptyrun.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
|
||||
"github.com/creack/pty"
|
||||
)
|
||||
|
||||
// oscRe strips OSC terminal sequences emitted by the PTY (e.g. colour queries).
|
||||
var oscRe = regexp.MustCompile(`\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)`)
|
||||
|
||||
// RunWithPTY runs cmd under a pseudo-terminal (preserving ANSI colours) and
|
||||
// returns the full output once the process exits.
|
||||
func RunWithPTY(ctx context.Context, cmd *exec.Cmd) (string, error) {
|
||||
ptmx, err := pty.StartWithSize(cmd, &pty.Winsize{Rows: 50, Cols: 220})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer func() { _ = ptmx.Close() }()
|
||||
|
||||
output, _ := io.ReadAll(ptmx)
|
||||
_ = cmd.Wait()
|
||||
|
||||
return oscRe.ReplaceAllString(string(output), ""), ctx.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user