redirect cursor hide/show to stderr (#456)

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2021-06-30 13:10:00 -04:00 committed by GitHub
parent ecf4e5546a
commit fee35dd175
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 8 deletions

1
go.mod
View file

@ -21,7 +21,6 @@ require (
github.com/gookit/color v1.2.7 github.com/gookit/color v1.2.7
github.com/hashicorp/go-multierror v1.1.0 github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/go-version v1.2.0 github.com/hashicorp/go-version v1.2.0
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.3.1 github.com/mitchellh/mapstructure v1.3.1
github.com/olekukonko/tablewriter v0.0.4 github.com/olekukonko/tablewriter v0.0.4

1
go.sum
View file

@ -466,7 +466,6 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213 h1:qGQQKEcAR99REcMpsXCp3lJ03zYT1PkRd3kQGPn9GVg=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=

View file

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io"
"os" "os"
"sync" "sync"
@ -11,7 +12,6 @@ import (
"github.com/anchore/syft/internal/logger" "github.com/anchore/syft/internal/logger"
syftEvent "github.com/anchore/syft/syft/event" syftEvent "github.com/anchore/syft/syft/event"
"github.com/anchore/syft/ui" "github.com/anchore/syft/ui"
"github.com/k0kubun/go-ansi"
"github.com/wagoodman/go-partybus" "github.com/wagoodman/go-partybus"
"github.com/wagoodman/jotframe/pkg/frame" "github.com/wagoodman/jotframe/pkg/frame"
) )
@ -38,18 +38,20 @@ type ephemeralTerminalUI struct {
waitGroup *sync.WaitGroup waitGroup *sync.WaitGroup
frame *frame.Frame frame *frame.Frame
logBuffer *bytes.Buffer logBuffer *bytes.Buffer
output *os.File
} }
func NewEphemeralTerminalUI() UI { func NewEphemeralTerminalUI() UI {
return &ephemeralTerminalUI{ return &ephemeralTerminalUI{
handler: ui.NewHandler(), handler: ui.NewHandler(),
waitGroup: &sync.WaitGroup{}, waitGroup: &sync.WaitGroup{},
output: os.Stderr,
} }
} }
func (h *ephemeralTerminalUI) Setup(unsubscribe func() error) error { func (h *ephemeralTerminalUI) Setup(unsubscribe func() error) error {
h.unsubscribe = unsubscribe h.unsubscribe = unsubscribe
ansi.CursorHide() hideCursor(h.output)
// prep the logger to not clobber the screen from now on (logrus only) // prep the logger to not clobber the screen from now on (logrus only)
h.logBuffer = bytes.NewBufferString("") h.logBuffer = bytes.NewBufferString("")
@ -93,7 +95,7 @@ func (h *ephemeralTerminalUI) openScreen() error {
config := frame.Config{ config := frame.Config{
PositionPolicy: frame.PolicyFloatForward, PositionPolicy: frame.PolicyFloatForward,
// only report output to stderr, reserve report output for stdout // only report output to stderr, reserve report output for stdout
Output: os.Stderr, Output: h.output,
} }
fr, err := frame.New(config) fr, err := frame.New(config)
@ -126,14 +128,22 @@ func (h *ephemeralTerminalUI) flushLog() {
logWrapper, ok := log.Log.(*logger.LogrusLogger) logWrapper, ok := log.Log.(*logger.LogrusLogger)
if ok { if ok {
fmt.Fprint(logWrapper.Output, h.logBuffer.String()) fmt.Fprint(logWrapper.Output, h.logBuffer.String())
logWrapper.Logger.SetOutput(os.Stderr) logWrapper.Logger.SetOutput(h.output)
} else { } else {
fmt.Fprint(os.Stderr, h.logBuffer.String()) fmt.Fprint(h.output, h.logBuffer.String())
} }
} }
func (h *ephemeralTerminalUI) Teardown(force bool) error { func (h *ephemeralTerminalUI) Teardown(force bool) error {
h.closeScreen(force) h.closeScreen(force)
ansi.CursorShow() showCursor(h.output)
return nil return nil
} }
func hideCursor(output io.Writer) {
fmt.Fprint(output, "\x1b[?25l")
}
func showCursor(output io.Writer) {
fmt.Fprint(output, "\x1b[?25h")
}