Skip to content

Commit

Permalink
Fix lint issues and small rewrite around ui/console
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Dec 12, 2023
1 parent fc67ab6 commit 1c4e9f5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
16 changes: 8 additions & 8 deletions cmd/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ func NewGlobalState(ctx context.Context) *GlobalState {
stderrTTY := !isDumbTerm && (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd()))
outMutex := &sync.Mutex{}
stdout := &console.Writer{
RawOut: os.Stdout,
Mutex: outMutex,
Writer: colorable.NewColorable(os.Stdout),
IsTTY: stdoutTTY,
RawOutFd: int(os.Stdout.Fd()),
Mutex: outMutex,
Writer: colorable.NewColorable(os.Stdout),
IsTTY: stdoutTTY,
}
stderr := &console.Writer{
RawOut: os.Stderr,
Mutex: outMutex,
Writer: colorable.NewColorable(os.Stderr),
IsTTY: stderrTTY,
RawOutFd: int(os.Stderr.Fd()),
Mutex: outMutex,
Writer: colorable.NewColorable(os.Stderr),
IsTTY: stderrTTY,
}

env := BuildEnvMap(os.Environ())
Expand Down
4 changes: 2 additions & 2 deletions cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func showProgress(ctx context.Context, gs *state.GlobalState, pbs []*pb.Progress
var errTermGetSize bool
termWidth := defaultTermWidth
if gs.Stdout.IsTTY {
tw, _, err := term.GetSize(int(gs.Stdout.RawOut.Fd()))
tw, _, err := term.GetSize(gs.Stdout.RawOutFd)
if !(tw > 0) || err != nil {
errTermGetSize = true
logger.WithError(err).Warn("error getting terminal size")
Expand Down Expand Up @@ -314,7 +314,7 @@ func showProgress(ctx context.Context, gs *state.GlobalState, pbs []*pb.Progress
updateFreq := 1 * time.Second
var stdoutFD int
if gs.Stdout.IsTTY {
stdoutFD = int(gs.Stdout.RawOut.Fd())
stdoutFD = gs.Stdout.RawOutFd
updateFreq = 100 * time.Millisecond
gs.OutMutex.Lock()
gs.Stdout.PersistentText = printProgressBars
Expand Down
9 changes: 4 additions & 5 deletions ui/console/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package console
import (
"bytes"
"io"
"os"
"sync"
)

// Writer syncs writes with a mutex and, if the output is a TTY, clears before
// newlines.
type Writer struct {
RawOut *os.File
Mutex *sync.Mutex
Writer io.Writer
IsTTY bool
RawOutFd int
Mutex *sync.Mutex
Writer io.Writer
IsTTY bool

// Used for flicker-free persistent objects like the progressbars
PersistentText func()
Expand Down

0 comments on commit 1c4e9f5

Please sign in to comment.