Skip to content

Commit cb2f95c

Browse files
committed
Optimise docker stats to not require clearing the whole screen
Instead of clearing the whole screen and then writing the new stats, we now write the new stats on top of the old text, and then clear the remaining text. This is a more efficient way to update the stats, as it avoids the flickering that happens when the screen is cleared and rewritten. Signed-off-by: Giedrius Jonikas <[email protected]>
1 parent 9861ce9 commit cb2f95c

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

cli/command/container/stats.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,16 +287,26 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
287287
cStats.mu.RUnlock()
288288

289289
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")
290+
// Start by moving the cursor to the top-left
291+
_, _ = fmt.Fprint(&statsTextBuffer, "\033[H")
292292
}
293293

294294
if err = statsFormatWrite(statsCtx, ccStats, daemonOSType, !options.NoTrunc); err != nil {
295295
break
296296
}
297297

298-
_, _ = fmt.Fprint(dockerCLI.Out(), statsTextBuffer.String())
298+
if !options.NoStream {
299+
for _, line := range strings.Split(statsTextBuffer.String(), "\n") {
300+
// In case the new text is shorter than the one we are writing over,
301+
// we'll append the "erase line" escape sequence to clear the remaining text.
302+
_, _ = fmt.Fprint(&statsTextBuffer, line, "\033[K\n")
303+
}
299304

305+
// We might have fewer containers than before, so let's clear the remaining text
306+
_, _ = fmt.Fprint(&statsTextBuffer, "\033[J")
307+
}
308+
309+
_, _ = fmt.Fprint(dockerCLI.Out(), statsTextBuffer.String())
300310
statsTextBuffer.Reset()
301311

302312
if len(cStats.cs) == 0 && !showAll {

0 commit comments

Comments
 (0)