Skip to content

Commit

Permalink
Fix docker stats in streaming mode
Browse files Browse the repository at this point in the history
  • Loading branch information
devemlight committed Mar 1, 2025
1 parent fedfea6 commit de359e1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ build:

.PHONY: publish
publish: clean
# Mac
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 $(GO) build -ldflags="-s -w" -o $(BIN)/$(APP)-darwin-amd64 ./cmd/cli
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 $(GO) build -ldflags="-s -w" -o $(BIN)/$(APP)-darwin-arm64 ./cmd/cli
# Linux
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 $(GO) build -ldflags="-s -w" -o $(BIN)/$(APP)-linux-amd64 ./cmd/cli
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 $(GO) build -ldflags="-s -w" -o $(BIN)/$(APP)-linux-arm64 ./cmd/cli
# Windows
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 $(GO) build -ldflags="-s -w" -o $(BIN)/$(APP)-windows-amd64.exe ./cmd/cli
# Cleanup
mv ./bin/* ~/Downloads

.PHONY: test
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/devemio/docker-color-output

go 1.23
go 1.24
2 changes: 1 addition & 1 deletion internal/app/const.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app

const (
Ver = "2.5.1"
Ver = "2.5.2"
Name = "docker-color-output"
)
53 changes: 36 additions & 17 deletions internal/stdin/stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,53 @@ import (

var ErrEmpty = errors.New("empty")

const rowsCap = 20
const (
capRows = 50
)

// clsBytes contains bytes to clear
// the screen for nix systems.
var clsBytes = []byte("\033[2J\033[H") //nolint:gochecknoglobals
//nolint:gochecknoglobals
var (
xClearToEnd = []byte("\033[J")
xClearToLineEnd = []byte("\033[K")
xMoveCursorHome = []byte("\033[H")
)

//nolint:cyclop
func Get(fn func(rows []string) error) error {
fi, err := os.Stdin.Stat()
if err != nil {
return fmt.Errorf("stdin: %w", err)
}

if fi.Mode()&os.ModeNamedPipe == 0 && fi.Size() <= 0 {
} else if fi.Mode()&os.ModeNamedPipe == 0 && fi.Size() <= 0 {
return fmt.Errorf("stdin: %w", ErrEmpty)
}

rows := make([]string, 0, rowsCap)
var (
rows = make([]string, 0, capRows)
scanner = bufio.NewScanner(os.Stdin)
)

scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
row, found := scanner.Bytes(), false //nolint:wastedassign
//nolint:wastedassign
row, found := scanner.Bytes(), false

// xClearToLineEnd
if _, found = bytes.CutPrefix(row, xClearToLineEnd); found {
stdout.Print(string(xClearToLineEnd))

continue
}

// xClearToLineEnd
row = bytes.TrimSuffix(row, xClearToLineEnd)

// xClearToEnd
if row, found = bytes.CutPrefix(row, xClearToEnd); found {
stdout.Print(string(xClearToEnd))
}

if row, found = bytes.CutPrefix(row, clsBytes); found && len(rows) > 0 {
stdout.Print(string(clsBytes))
// xMoveCursorHome
if row, found = bytes.CutPrefix(row, xMoveCursorHome); found && len(rows) > 0 {
stdout.Print(string(xMoveCursorHome))

if err = fn(rows); err != nil {
return err
Expand All @@ -51,9 +74,5 @@ func Get(fn func(rows []string) error) error {
return fmt.Errorf("stdin: scan: %w", err)
}

if err = fn(rows); err != nil {
return err
}

return nil
return fn(rows)
}

0 comments on commit de359e1

Please sign in to comment.