Use SIGWINCH to update terminal size
Instead of reading the terminal size on every status update, register for SIGWINCH to read and store the size when it changes. This reapplies I555ad21a31a2c924ab0ca681e0c8f00df42a370a with a fix for a race condition in TestSmartStatusOutputWidthChange. Test: status_test.go Change-Id: If342cb4cc8e4ed57af9e3bb417758348c9c41247
This commit is contained in:
parent
e77a57336f
commit
4355ee64a9
|
@ -17,8 +17,11 @@ package terminal
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
"android/soong/ui/status"
|
"android/soong/ui/status"
|
||||||
)
|
)
|
||||||
|
@ -30,18 +33,30 @@ type smartStatusOutput struct {
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
|
||||||
haveBlankLine bool
|
haveBlankLine bool
|
||||||
|
|
||||||
|
termWidth int
|
||||||
|
sigwinch chan os.Signal
|
||||||
|
sigwinchHandled chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSmartStatusOutput returns a StatusOutput that represents the
|
// NewSmartStatusOutput returns a StatusOutput that represents the
|
||||||
// current build status similarly to Ninja's built-in terminal
|
// current build status similarly to Ninja's built-in terminal
|
||||||
// output.
|
// output.
|
||||||
func NewSmartStatusOutput(w io.Writer, formatter formatter) status.StatusOutput {
|
func NewSmartStatusOutput(w io.Writer, formatter formatter) status.StatusOutput {
|
||||||
return &smartStatusOutput{
|
s := &smartStatusOutput{
|
||||||
writer: w,
|
writer: w,
|
||||||
formatter: formatter,
|
formatter: formatter,
|
||||||
|
|
||||||
haveBlankLine: true,
|
haveBlankLine: true,
|
||||||
|
|
||||||
|
sigwinch: make(chan os.Signal),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.updateTermSize()
|
||||||
|
|
||||||
|
s.startSigwinch()
|
||||||
|
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *smartStatusOutput) Message(level status.MsgLevel, message string) {
|
func (s *smartStatusOutput) Message(level status.MsgLevel, message string) {
|
||||||
|
@ -101,6 +116,8 @@ func (s *smartStatusOutput) Flush() {
|
||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
defer s.lock.Unlock()
|
defer s.lock.Unlock()
|
||||||
|
|
||||||
|
s.stopSigwinch()
|
||||||
|
|
||||||
s.requestLine()
|
s.requestLine()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,16 +154,8 @@ func (s *smartStatusOutput) statusLine(str string) {
|
||||||
|
|
||||||
// Limit line width to the terminal width, otherwise we'll wrap onto
|
// Limit line width to the terminal width, otherwise we'll wrap onto
|
||||||
// another line and we won't delete the previous line.
|
// another line and we won't delete the previous line.
|
||||||
//
|
if s.termWidth > 0 {
|
||||||
// Run this on every line in case the window has been resized while
|
str = s.elide(str)
|
||||||
// we're printing. This could be optimized to only re-run when we get
|
|
||||||
// SIGWINCH if it ever becomes too time consuming.
|
|
||||||
if max, ok := termWidth(s.writer); ok {
|
|
||||||
if len(str) > max {
|
|
||||||
// TODO: Just do a max. Ninja elides the middle, but that's
|
|
||||||
// more complicated and these lines aren't that important.
|
|
||||||
str = str[:max]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move to the beginning on the line, turn on bold, print the output,
|
// Move to the beginning on the line, turn on bold, print the output,
|
||||||
|
@ -156,3 +165,38 @@ func (s *smartStatusOutput) statusLine(str string) {
|
||||||
fmt.Fprint(s.writer, start, str, end)
|
fmt.Fprint(s.writer, start, str, end)
|
||||||
s.haveBlankLine = false
|
s.haveBlankLine = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *smartStatusOutput) elide(str string) string {
|
||||||
|
if len(str) > s.termWidth {
|
||||||
|
// TODO: Just do a max. Ninja elides the middle, but that's
|
||||||
|
// more complicated and these lines aren't that important.
|
||||||
|
str = str[:s.termWidth]
|
||||||
|
}
|
||||||
|
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *smartStatusOutput) startSigwinch() {
|
||||||
|
signal.Notify(s.sigwinch, syscall.SIGWINCH)
|
||||||
|
go func() {
|
||||||
|
for _ = range s.sigwinch {
|
||||||
|
s.lock.Lock()
|
||||||
|
s.updateTermSize()
|
||||||
|
s.lock.Unlock()
|
||||||
|
if s.sigwinchHandled != nil {
|
||||||
|
s.sigwinchHandled <- true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *smartStatusOutput) stopSigwinch() {
|
||||||
|
signal.Stop(s.sigwinch)
|
||||||
|
close(s.sigwinch)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *smartStatusOutput) updateTermSize() {
|
||||||
|
if w, ok := termWidth(s.writer); ok {
|
||||||
|
s.termWidth = w
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ package terminal
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"android/soong/ui/status"
|
"android/soong/ui/status"
|
||||||
|
@ -252,6 +253,8 @@ func actionWithOuptutWithAnsiCodes(stat status.StatusOutput) {
|
||||||
func TestSmartStatusOutputWidthChange(t *testing.T) {
|
func TestSmartStatusOutputWidthChange(t *testing.T) {
|
||||||
smart := &fakeSmartTerminal{termWidth: 40}
|
smart := &fakeSmartTerminal{termWidth: 40}
|
||||||
stat := NewStatusOutput(smart, "", false)
|
stat := NewStatusOutput(smart, "", false)
|
||||||
|
smartStat := stat.(*smartStatusOutput)
|
||||||
|
smartStat.sigwinchHandled = make(chan bool)
|
||||||
|
|
||||||
runner := newRunner(stat, 2)
|
runner := newRunner(stat, 2)
|
||||||
|
|
||||||
|
@ -260,6 +263,9 @@ func TestSmartStatusOutputWidthChange(t *testing.T) {
|
||||||
|
|
||||||
runner.startAction(action)
|
runner.startAction(action)
|
||||||
smart.termWidth = 30
|
smart.termWidth = 30
|
||||||
|
// Fake a SIGWINCH
|
||||||
|
smartStat.sigwinch <- syscall.SIGWINCH
|
||||||
|
<-smartStat.sigwinchHandled
|
||||||
runner.finishAction(result)
|
runner.finishAction(result)
|
||||||
|
|
||||||
stat.Flush()
|
stat.Flush()
|
||||||
|
|
Loading…
Reference in New Issue