From c68bb5795901881bf1c42faca66c474e1445d3b3 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Sun, 4 Sep 2016 14:44:34 -0700 Subject: [PATCH] Restrict size to 2 fractional digits for `docker images` This fix tries to address the issue raised in 26300. Previously `docker images` will use `HumanSize()` to display the size which has a fixed precision of 4 (thus 3 fractional digits). This could be problematic in certain languages (e.g. , German, see 26300) as `.` may be interpreted as thousands-separator in number. This fix use `CustomSize()` instead and limit the precision to 3 (thus 2 fractional digits). This fix has been tested manually. This fix fixes 26300. Signed-off-by: Yong Tang --- command/container/stats_helpers.go | 4 ++-- command/container/stats_unit_test.go | 2 +- command/formatter/container.go | 4 ++-- command/formatter/image.go | 2 +- command/image/history.go | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/command/container/stats_helpers.go b/command/container/stats_helpers.go index b5e8e0472f..54cc5589c1 100644 --- a/command/container/stats_helpers.go +++ b/command/container/stats_helpers.go @@ -194,8 +194,8 @@ func (s *containerStats) Display(w io.Writer) error { s.CPUPercentage, units.BytesSize(s.Memory), units.BytesSize(s.MemoryLimit), s.MemoryPercentage, - units.HumanSize(s.NetworkRx), units.HumanSize(s.NetworkTx), - units.HumanSize(s.BlockRead), units.HumanSize(s.BlockWrite), + units.HumanSizeWithPrecision(s.NetworkRx, 3), units.HumanSizeWithPrecision(s.NetworkTx, 3), + units.HumanSizeWithPrecision(s.BlockRead, 3), units.HumanSizeWithPrecision(s.BlockWrite, 3), s.PidsCurrent) return nil } diff --git a/command/container/stats_unit_test.go b/command/container/stats_unit_test.go index 6f6a468068..182ab5b30d 100644 --- a/command/container/stats_unit_test.go +++ b/command/container/stats_unit_test.go @@ -25,7 +25,7 @@ func TestDisplay(t *testing.T) { t.Fatalf("c.Display() gave error: %s", err) } got := b.String() - want := "app\t30.00%\t100 MiB / 2 GiB\t4.88%\t104.9 MB / 838.9 MB\t104.9 MB / 838.9 MB\t1\n" + want := "app\t30.00%\t100 MiB / 2 GiB\t4.88%\t105 MB / 839 MB\t105 MB / 839 MB\t1\n" if got != want { t.Fatalf("c.Display() = %q, want %q", got, want) } diff --git a/command/formatter/container.go b/command/formatter/container.go index f1c985791b..6f519e4493 100644 --- a/command/formatter/container.go +++ b/command/formatter/container.go @@ -152,8 +152,8 @@ func (c *containerContext) Status() string { func (c *containerContext) Size() string { c.addHeader(sizeHeader) - srw := units.HumanSize(float64(c.c.SizeRw)) - sv := units.HumanSize(float64(c.c.SizeRootFs)) + srw := units.HumanSizeWithPrecision(float64(c.c.SizeRw), 3) + sv := units.HumanSizeWithPrecision(float64(c.c.SizeRootFs), 3) sf := srw if c.c.SizeRootFs > 0 { diff --git a/command/formatter/image.go b/command/formatter/image.go index 0ffcfaf728..012860e04e 100644 --- a/command/formatter/image.go +++ b/command/formatter/image.go @@ -225,5 +225,5 @@ func (c *imageContext) CreatedAt() string { func (c *imageContext) Size() string { c.addHeader(sizeHeader) - return units.HumanSize(float64(c.i.Size)) + return units.HumanSizeWithPrecision(float64(c.i.Size), 3) } diff --git a/command/image/history.go b/command/image/history.go index a75403a45f..91c8f75a63 100644 --- a/command/image/history.go +++ b/command/image/history.go @@ -86,7 +86,7 @@ func runHistory(dockerCli *command.DockerCli, opts historyOptions) error { if opts.human { created = units.HumanDuration(time.Now().UTC().Sub(time.Unix(entry.Created, 0))) + " ago" - size = units.HumanSize(float64(entry.Size)) + size = units.HumanSizeWithPrecision(float64(entry.Size), 3) } else { created = time.Unix(entry.Created, 0).Format(time.RFC3339) size = strconv.FormatInt(entry.Size, 10)