From 0b44b263aaa06879aa59e4fd9cae3cbd8af56c38 Mon Sep 17 00:00:00 2001 From: winhost Date: Fri, 8 Nov 2024 15:11:49 +0800 Subject: [PATCH] feat(pty): support windows --- go.mod | 2 +- pkg/pty/pty.go | 28 ++++++++++++++++++++-------- pkg/pty/pty_windows.go | 4 +++- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 32e71399..661e819d 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ replace github.com/tree-sitter/tree-sitter-markdown => ./pkg/tree-sitter-markdow replace github.com/tree-sitter-grammars/tree-sitter-yaml => ./pkg/tree-sitter-yaml/bindings/go replace github.com/tree-sitter-grammars/tree-sitter-toml => ./pkg/tree-sitter-toml/bindings/go - +replace github.com/creack/pty => ./pkg/photostorm/pty require ( github.com/charlievieth/fastwalk v1.0.8 github.com/reinhrst/fzf-lib v0.9.0 diff --git a/pkg/pty/pty.go b/pkg/pty/pty.go index 5b4f5f2e..1cda1d80 100644 --- a/pkg/pty/pty.go +++ b/pkg/pty/pty.go @@ -85,8 +85,9 @@ func setupLogFile(filename string) (*os.File, error) { type Pty struct { Cmd *exec.Cmd - File *os.File + File pty.Pty Ch chan os.Signal + wch chan bool Rows uint16 // ws_row: Number of rows (in cells). Cols uint16 // } @@ -117,7 +118,7 @@ func RunNoStdin(Args []string) *Pty { if err != nil { log.Fatal(err) } - ret := &Pty{Cmd: c, File: f, Ch: make(chan os.Signal, 1)} + ret := &Pty{Cmd: c, File: f, Ch: make(chan os.Signal, 1), wch: make(chan bool, 1)} return ret } @@ -142,14 +143,25 @@ func RunCommand(Args []string) *Pty { } io.Copy(stdin2, os.Stdin) }() - ret := &Pty{File: f, Ch: make(chan os.Signal, 1)} + ret := &Pty{File: f, Ch: make(chan os.Signal, 1),wch: make(chan bool, 1),} ret.Notify() go func() { - for range ret.Ch { - // if err := pty.InheritSize(os.Stdin, ret.File); err != nil { - // } - if err := pty.Setsize(ret.File, &pty.Winsize{Rows: ret.Rows, Cols: ret.Cols}); err != nil { - log.Printf("error resizing pty: %s", err) + for { + select { + case <-ret.wch: + { + if err := pty.Setsize(ret.File, &pty.Winsize{Rows: ret.Rows, Cols: ret.Cols}); err != nil { + debug.DebugLogf("pty","error resizing pty: %s", err) + } + } + case <-ret.Ch: + { + // if err := pty.InheritSize(os.Stdin, ret.File); err != nil { + // } + if err := pty.Setsize(ret.File, &pty.Winsize{Rows: ret.Rows, Cols: ret.Cols}); err != nil { + log.Printf("error resizing pty: %s", err) + } + } } } }() diff --git a/pkg/pty/pty_windows.go b/pkg/pty/pty_windows.go index 46250f59..a980c4bf 100644 --- a/pkg/pty/pty_windows.go +++ b/pkg/pty/pty_windows.go @@ -2,7 +2,9 @@ package pty func (pty *Pty) OsUpdateSize(Rows uint16, Cols uint16) { pty.Rows = Rows pty.Cols = Cols - // pty.Ch <- syscall.SIGWINCH + go func () { + pty.wch <- true + }() } func (ret Pty)Notify(){ // signal.Notify(ret.Ch, syscall.SIGWINCH)