diff options
| author | Rakesh | 2015-11-17 16:12:31 +0530 |
|---|---|---|
| committer | Mike McQuaid | 2015-12-30 08:10:36 +0000 |
| commit | a96a9004f4600ed71b0d9f6a22fbc9b2a2fe41f8 (patch) | |
| tree | ee668c6ff9da777a63c28b3386a23a85dba92ce2 | |
| parent | c15c9dbaf3d6bba4cf44b94963cad124952f902b (diff) | |
| download | brew-a96a9004f4600ed71b0d9f6a22fbc9b2a2fe41f8.tar.bz2 | |
utils: add readable disk space, numbers methods.
| -rw-r--r-- | Library/Homebrew/utils.rb | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index aeb706299..cd47d4a28 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -592,3 +592,32 @@ module GitHub open(uri) { |json| json["private"] } end end + +def disk_usage_readable(size_in_bytes) + len = size_in_bytes.to_s.length + case + when len > 9 + sym, unit = ["G", 1_073_741_824] + when len > 6 + sym, unit = ["M", 1_048_576] + when len > 3 + sym, unit = ["K", 1_024] + else + sym, unit = ["B", 1] + end + + num = "%.1f" % [size_in_bytes.to_f / unit] + # check whether the rounded value has a zero after decimal point, + # if true, then display just the integer value. + if num.split(".").last.to_i == 0 + "%d#{sym}" % num.to_i + else + "#{num}#{sym}" + end +end + +def number_readable(number) + numstr = number.to_i.to_s + (numstr.size - 3).step(1, -3) { |i| numstr.insert(i, ",") } + numstr +end |
