Browse code

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 <yong.tang.github@outlook.com>

Yong Tang authored on 2016/09/05 06:44:34
Showing 5 changed files
... ...
@@ -194,8 +194,8 @@ func (s *containerStats) Display(w io.Writer) error {
194 194
 		s.CPUPercentage,
195 195
 		units.BytesSize(s.Memory), units.BytesSize(s.MemoryLimit),
196 196
 		s.MemoryPercentage,
197
-		units.HumanSize(s.NetworkRx), units.HumanSize(s.NetworkTx),
198
-		units.HumanSize(s.BlockRead), units.HumanSize(s.BlockWrite),
197
+		units.HumanSizeWithPrecision(s.NetworkRx, 3), units.HumanSizeWithPrecision(s.NetworkTx, 3),
198
+		units.HumanSizeWithPrecision(s.BlockRead, 3), units.HumanSizeWithPrecision(s.BlockWrite, 3),
199 199
 		s.PidsCurrent)
200 200
 	return nil
201 201
 }
... ...
@@ -25,7 +25,7 @@ func TestDisplay(t *testing.T) {
25 25
 		t.Fatalf("c.Display() gave error: %s", err)
26 26
 	}
27 27
 	got := b.String()
28
-	want := "app\t30.00%\t100 MiB / 2 GiB\t4.88%\t104.9 MB / 838.9 MB\t104.9 MB / 838.9 MB\t1\n"
28
+	want := "app\t30.00%\t100 MiB / 2 GiB\t4.88%\t105 MB / 839 MB\t105 MB / 839 MB\t1\n"
29 29
 	if got != want {
30 30
 		t.Fatalf("c.Display() = %q, want %q", got, want)
31 31
 	}
... ...
@@ -152,8 +152,8 @@ func (c *containerContext) Status() string {
152 152
 
153 153
 func (c *containerContext) Size() string {
154 154
 	c.addHeader(sizeHeader)
155
-	srw := units.HumanSize(float64(c.c.SizeRw))
156
-	sv := units.HumanSize(float64(c.c.SizeRootFs))
155
+	srw := units.HumanSizeWithPrecision(float64(c.c.SizeRw), 3)
156
+	sv := units.HumanSizeWithPrecision(float64(c.c.SizeRootFs), 3)
157 157
 
158 158
 	sf := srw
159 159
 	if c.c.SizeRootFs > 0 {
... ...
@@ -225,5 +225,5 @@ func (c *imageContext) CreatedAt() string {
225 225
 
226 226
 func (c *imageContext) Size() string {
227 227
 	c.addHeader(sizeHeader)
228
-	return units.HumanSize(float64(c.i.Size))
228
+	return units.HumanSizeWithPrecision(float64(c.i.Size), 3)
229 229
 }
... ...
@@ -86,7 +86,7 @@ func runHistory(dockerCli *command.DockerCli, opts historyOptions) error {
86 86
 
87 87
 		if opts.human {
88 88
 			created = units.HumanDuration(time.Now().UTC().Sub(time.Unix(entry.Created, 0))) + " ago"
89
-			size = units.HumanSize(float64(entry.Size))
89
+			size = units.HumanSizeWithPrecision(float64(entry.Size), 3)
90 90
 		} else {
91 91
 			created = time.Unix(entry.Created, 0).Format(time.RFC3339)
92 92
 			size = strconv.FormatInt(entry.Size, 10)