[lxc-devel] [lxd/master] util: GetByteSizeString() take precision argument
brauner on Github
lxc-bot at linuxcontainers.org
Tue Dec 20 11:59:11 UTC 2016
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161220/d2d0efc1/attachment.bin>
-------------- next part --------------
From 8fe5a0919ccfe0d14c2f9dc73830515d24458246 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 20 Dec 2016 12:56:57 +0100
Subject: [PATCH] util: GetByteSizeString() take precision argument
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
lxc/image.go | 2 +-
lxc/info.go | 14 +++++++-------
lxd/daemon_images.go | 2 +-
lxd/storage.go | 4 ++--
shared/util.go | 10 +++++++---
5 files changed, 18 insertions(+), 14 deletions(-)
diff --git a/lxc/image.go b/lxc/image.go
index 30592cf..082474c 100644
--- a/lxc/image.go
+++ b/lxc/image.go
@@ -425,7 +425,7 @@ func (c *imageCmd) run(config *lxd.Config, args []string) error {
} else {
progress := ProgressRenderer{Format: i18n.G("Transferring image: %s")}
handler := func(percent int64, speed int64) {
- progress.Update(fmt.Sprintf("%d%% (%s/s)", percent, shared.GetByteSizeString(speed)))
+ progress.Update(fmt.Sprintf("%d%% (%s/s)", percent, shared.GetByteSizeString(speed, -1)))
}
fingerprint, err = d.PostImage(imageFile, rootfsFile, properties, c.publicImage, c.addAliases, handler)
diff --git a/lxc/info.go b/lxc/info.go
index 64af2ac..37d1afc 100644
--- a/lxc/info.go
+++ b/lxc/info.go
@@ -134,7 +134,7 @@ func (c *infoCmd) containerInfo(d *lxd.Client, name string, showLog bool) error
if cs.Disk != nil {
for entry, disk := range cs.Disk {
if disk.Usage != 0 {
- diskInfo += fmt.Sprintf(" %s: %s\n", entry, shared.GetByteSizeString(disk.Usage))
+ diskInfo += fmt.Sprintf(" %s: %s\n", entry, shared.GetByteSizeString(disk.Usage, -1))
}
}
}
@@ -158,19 +158,19 @@ func (c *infoCmd) containerInfo(d *lxd.Client, name string, showLog bool) error
// Memory usage
memoryInfo := ""
if cs.Memory.Usage != 0 {
- memoryInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Memory (current)"), shared.GetByteSizeString(cs.Memory.Usage))
+ memoryInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Memory (current)"), shared.GetByteSizeString(cs.Memory.Usage, -1))
}
if cs.Memory.UsagePeak != 0 {
- memoryInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Memory (peak)"), shared.GetByteSizeString(cs.Memory.UsagePeak))
+ memoryInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Memory (peak)"), shared.GetByteSizeString(cs.Memory.UsagePeak, -1))
}
if cs.Memory.SwapUsage != 0 {
- memoryInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Swap (current)"), shared.GetByteSizeString(cs.Memory.SwapUsage))
+ memoryInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Swap (current)"), shared.GetByteSizeString(cs.Memory.SwapUsage, -1))
}
if cs.Memory.SwapUsagePeak != 0 {
- memoryInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Swap (peak)"), shared.GetByteSizeString(cs.Memory.SwapUsagePeak))
+ memoryInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Swap (peak)"), shared.GetByteSizeString(cs.Memory.SwapUsagePeak, -1))
}
if memoryInfo != "" {
@@ -183,8 +183,8 @@ func (c *infoCmd) containerInfo(d *lxd.Client, name string, showLog bool) error
if cs.Network != nil {
for netName, net := range cs.Network {
networkInfo += fmt.Sprintf(" %s:\n", netName)
- networkInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Bytes received"), shared.GetByteSizeString(net.Counters.BytesReceived))
- networkInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Bytes sent"), shared.GetByteSizeString(net.Counters.BytesSent))
+ networkInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Bytes received"), shared.GetByteSizeString(net.Counters.BytesReceived, -1))
+ networkInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Bytes sent"), shared.GetByteSizeString(net.Counters.BytesSent, -1))
networkInfo += fmt.Sprintf(" %s: %d\n", i18n.G("Packets received"), net.Counters.PacketsReceived)
networkInfo += fmt.Sprintf(" %s: %d\n", i18n.G("Packets sent"), net.Counters.PacketsSent)
}
diff --git a/lxd/daemon_images.go b/lxd/daemon_images.go
index c2dd4f6..3161971 100644
--- a/lxd/daemon_images.go
+++ b/lxd/daemon_images.go
@@ -269,7 +269,7 @@ func (d *Daemon) ImageDownload(op *operation, server string, protocol string, ce
meta = make(map[string]interface{})
}
- progress := fmt.Sprintf("%d%% (%s/s)", progressInt, shared.GetByteSizeString(speedInt))
+ progress := fmt.Sprintf("%d%% (%s/s)", progressInt, shared.GetByteSizeString(speedInt, -1))
if meta["download_progress"] != progress {
meta["download_progress"] = progress
diff --git a/lxd/storage.go b/lxd/storage.go
index 3edf294..a3c3e70 100644
--- a/lxd/storage.go
+++ b/lxd/storage.go
@@ -813,9 +813,9 @@ func progressWrapperRender(op *operation, key string, description string, progre
meta = make(map[string]interface{})
}
- progress := fmt.Sprintf("%s (%s/s)", shared.GetByteSizeString(progressInt), shared.GetByteSizeString(speedInt))
+ progress := fmt.Sprintf("%s (%s/s)", shared.GetByteSizeString(progressInt, -1), shared.GetByteSizeString(speedInt, -1))
if description != "" {
- progress = fmt.Sprintf("%s: %s (%s/s)", description, shared.GetByteSizeString(progressInt), shared.GetByteSizeString(speedInt))
+ progress = fmt.Sprintf("%s: %s (%s/s)", description, shared.GetByteSizeString(progressInt, -1), shared.GetByteSizeString(speedInt, -1))
}
if meta[key] != progress {
diff --git a/shared/util.go b/shared/util.go
index cb28ba3..85920eb 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -708,7 +708,11 @@ func ParseBitSizeString(input string) (int64, error) {
return valueInt * multiplicator, nil
}
-func GetByteSizeString(input int64) string {
+func GetByteSizeString(input int64, precision int) string {
+ if precision < 0 {
+ precision = 2
+ }
+
if input < 1024 {
return fmt.Sprintf("%d bytes", input)
}
@@ -718,11 +722,11 @@ func GetByteSizeString(input int64) string {
for _, unit := range []string{"kB", "MB", "GB", "TB", "PB", "EB"} {
value = value / 1024
if value < 1024 {
- return fmt.Sprintf("%.2f%s", value, unit)
+ return fmt.Sprintf("%.*f%s", precision, value, unit)
}
}
- return fmt.Sprintf("%.2fEB", value)
+ return fmt.Sprintf("%.*fEB", precision, value)
}
// RemoveDuplicatesFromString removes all duplicates of the string 'sep'
More information about the lxc-devel
mailing list