From f49ca37dc631cfcb6b130c37c7676d8c262127c8 Mon Sep 17 00:00:00 2001 From: Florian Rey Date: Wed, 29 May 2024 16:32:23 +0200 Subject: [PATCH] fix: wait for goland terminal size --- signals_unix.go | 2 +- tea.go | 4 ++-- tty.go | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/signals_unix.go b/signals_unix.go index 409540385f..aec83546e8 100644 --- a/signals_unix.go +++ b/signals_unix.go @@ -28,6 +28,6 @@ func (p *Program) listenForResize(done chan struct{}) { case <-sig: } - p.checkResize() + p.checkResize(false) } } diff --git a/tea.go b/tea.go index 0915d1adbd..88f3138e5c 100644 --- a/tea.go +++ b/tea.go @@ -247,7 +247,7 @@ func (p *Program) handleResize() chan struct{} { if p.ttyOutput != nil { // Get the initial terminal size and send it to the program. - go p.checkResize() + go p.checkResize(true) // Listen for window resizes. go p.listenForResize(ch) @@ -686,7 +686,7 @@ func (p *Program) RestoreTerminal() error { // process was at the foreground, in which case we may not have received // SIGWINCH. Detect any size change now and propagate the new size as // needed. - go p.checkResize() + go p.checkResize(false) return nil } diff --git a/tty.go b/tty.go index 2fdd36b4e4..5422068e63 100644 --- a/tty.go +++ b/tty.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io" + "os" "time" "github.com/charmbracelet/x/term" @@ -92,12 +93,42 @@ func (p *Program) waitForReadLoop() { // checkResize detects the current size of the output and informs the program // via a WindowSizeMsg. -func (p *Program) checkResize() { +func (p *Program) checkResize(first bool) { if p.ttyOutput == nil { // can't query window size return } + if first { + // JetBrains terminal (JediTerm) don't answer real size until approx. 400ms. + // In other words, we have no choice but to try hard again and again... + if _, ok := os.LookupEnv("IDEA_INITIAL_DIRECTORY"); ok { + var w, h int + var err error + for i := 0; i < 60; i++ { + time.Sleep(time.Millisecond * 10) + w, h, err = term.GetSize(p.ttyOutput.Fd()) + if err != nil { + select { + case <-p.ctx.Done(): + case p.errs <- err: + } + + return + } + if w != 0 || h != 0 { + break + } + } + p.Send(WindowSizeMsg{ + Width: w, + Height: h, + }) + + return + } + } + w, h, err := term.GetSize(p.ttyOutput.Fd()) if err != nil { select {