about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorBenjamin Hipple <bhipple@protonmail.com>2018-08-25T22·35-0400
committerEelco Dolstra <edolstra@gmail.com>2018-08-30T14·22+0200
commit3407a5d9361611ea8b5da9e02a7a1e2f90c928cd (patch)
tree2246d1a617397069e92511bac7b6f1823a5e6114 /src
parent5e83b0227f024c63fd1adb7995d17a8a1604b10c (diff)
Add human readable closure sizes to nix path-info
Unfortunately, -h is already taken as a short option by --help, so we have to
use a different letter or the capitalized version.

Resolves #2363
Diffstat (limited to 'src')
-rw-r--r--src/nix/path-info.cc29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/nix/path-info.cc b/src/nix/path-info.cc
index 47caa401d3c9..ee48d483c7ef 100644
--- a/src/nix/path-info.cc
+++ b/src/nix/path-info.cc
@@ -13,12 +13,14 @@ struct CmdPathInfo : StorePathsCommand, MixJSON
 {
     bool showSize = false;
     bool showClosureSize = false;
+    bool humanReadable = false;
     bool showSigs = false;
 
     CmdPathInfo()
     {
         mkFlag('s', "size", "print size of the NAR dump of each path", &showSize);
         mkFlag('S', "closure-size", "print sum size of the NAR dumps of the closure of each path", &showClosureSize);
+        mkFlag('H', "human-readable", "with -s and -S, print sizes like 1K 234M 5.67G etc.", &humanReadable);
         mkFlag(0, "sigs", "show signatures", &showSigs);
     }
 
@@ -40,6 +42,10 @@ struct CmdPathInfo : StorePathsCommand, MixJSON
                 "nix path-info -rS /run/current-system | sort -nk2"
             },
             Example{
+                "To show a package's closure size and all its dependencies with human readable sizes:",
+                "nix path-info -rsSH nixpkgs.rust"
+            },
+            Example{
                 "To check the existence of a path in a binary cache:",
                 "nix path-info -r /nix/store/7qvk5c91...-geeqie-1.1 --store https://cache.nixos.org/"
             },
@@ -58,6 +64,25 @@ struct CmdPathInfo : StorePathsCommand, MixJSON
         };
     }
 
+    void printSize(int value)
+    {
+        if (!humanReadable) {
+            std::cout << '\t' << std::setw(11) << value;
+            return;
+        }
+
+        static constexpr std::array<char, 6> idents = {
+            ' ', 'K', 'M', 'G', 'T', 'P'
+        };
+        size_t power = 0;
+        double res = value;
+        while (res > 1024) {
+            ++power;
+            res /= 1024;
+        }
+        std::cout << '\t' << std::setw(11) << std::setprecision(3) << res << idents[power];
+    }
+
     void run(ref<Store> store, Paths storePaths) override
     {
         size_t pathLen = 0;
@@ -81,10 +106,10 @@ struct CmdPathInfo : StorePathsCommand, MixJSON
                 std::cout << storePath << std::string(std::max(0, (int) pathLen - (int) storePath.size()), ' ');
 
                 if (showSize)
-                    std::cout << '\t' << std::setw(11) << info->narSize;
+                    printSize(info->narSize);
 
                 if (showClosureSize)
-                    std::cout << '\t' << std::setw(11) << store->getClosureSize(storePath).first;
+                    printSize(store->getClosureSize(storePath).first);
 
                 if (showSigs) {
                     std::cout << '\t';