Skip to content

Commit 2995631

Browse files
authored
Merge pull request #5586 from Giedriusj1/master
2 parents fb103cb + 0b16070 commit 2995631

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

cli/command/container/stats.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package container
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"io"
@@ -264,31 +265,40 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
264265
// so we unlikely hit this code in practice.
265266
daemonOSType = dockerCLI.ServerInfo().OSType
266267
}
268+
269+
// Buffer to store formatted stats text.
270+
// Once formatted, it will be printed in one write to avoid screen flickering.
271+
var statsTextBuffer bytes.Buffer
272+
267273
statsCtx := formatter.Context{
268-
Output: dockerCLI.Out(),
274+
Output: &statsTextBuffer,
269275
Format: NewStatsFormat(format, daemonOSType),
270276
}
271-
cleanScreen := func() {
272-
if !options.NoStream {
273-
_, _ = fmt.Fprint(dockerCLI.Out(), "\033[2J")
274-
_, _ = fmt.Fprint(dockerCLI.Out(), "\033[H")
275-
}
276-
}
277277

278278
var err error
279279
ticker := time.NewTicker(500 * time.Millisecond)
280280
defer ticker.Stop()
281281
for range ticker.C {
282-
cleanScreen()
283282
var ccStats []StatsEntry
284283
cStats.mu.RLock()
285284
for _, c := range cStats.cs {
286285
ccStats = append(ccStats, c.GetStatistics())
287286
}
288287
cStats.mu.RUnlock()
288+
289+
if !options.NoStream {
290+
// Start by clearing the screen and moving the cursor to the top-left
291+
_, _ = fmt.Fprint(&statsTextBuffer, "\033[2J\033[H")
292+
}
293+
289294
if err = statsFormatWrite(statsCtx, ccStats, daemonOSType, !options.NoTrunc); err != nil {
290295
break
291296
}
297+
298+
_, _ = fmt.Fprint(dockerCLI.Out(), statsTextBuffer.String())
299+
300+
statsTextBuffer.Reset()
301+
292302
if len(cStats.cs) == 0 && !showAll {
293303
break
294304
}

0 commit comments

Comments
 (0)