From ae36a80a6c7622ae1d017d619e6fd1b393481d3a Mon Sep 17 00:00:00 2001 From: Arda Cinar Date: Tue, 10 Jan 2023 15:07:17 +0300 Subject: [PATCH] df: Add an option to print the human readable sizes in powers of 10 --- Userland/Utilities/df.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Userland/Utilities/df.cpp b/Userland/Utilities/df.cpp index 760b0456d2..a2454542d2 100644 --- a/Userland/Utilities/df.cpp +++ b/Userland/Utilities/df.cpp @@ -26,11 +26,13 @@ struct FileSystem { ErrorOr serenity_main(Main::Arguments arguments) { bool flag_human_readable = false; + bool flag_human_readable_si = false; bool flag_inode_info = false; Core::ArgsParser args_parser; args_parser.set_general_help("Display free disk space of each partition."); args_parser.add_option(flag_human_readable, "Print human-readable sizes", "human-readable", 'h'); + args_parser.add_option(flag_human_readable_si, "Print human-readable sizes in SI units", "si", 'H'); args_parser.add_option(flag_inode_info, "Show inode information as well", "inodes", 'i'); args_parser.parse(arguments); @@ -80,10 +82,13 @@ ErrorOr serenity_main(Main::Arguments arguments) out("{:12} ", fs); - if (flag_human_readable) { - out("{:>12} ", human_readable_size(total_block_count * block_size)); - out("{:>12} ", human_readable_size(used_block_count * block_size)); - out("{:>12} ", human_readable_size(free_block_count * block_size)); + bool human_readable = flag_human_readable || flag_human_readable_si; + auto human_readable_based_on = flag_human_readable_si ? AK::HumanReadableBasedOn::Base10 : AK::HumanReadableBasedOn::Base2; + + if (human_readable) { + out("{:>12} ", human_readable_size(total_block_count * block_size, human_readable_based_on)); + out("{:>12} ", human_readable_size(used_block_count * block_size, human_readable_based_on)); + out("{:>12} ", human_readable_size(free_block_count * block_size, human_readable_based_on)); out("{:>11}% ", used_percentage); } else { out("{:>12} ", (uint64_t)total_block_count); @@ -93,10 +98,10 @@ ErrorOr serenity_main(Main::Arguments arguments) } if (flag_inode_info) { - if (flag_human_readable) { - out("{:>12} ", human_readable_quantity(total_inode_count)); - out("{:>12} ", human_readable_quantity(used_inode_count)); - out("{:>12} ", human_readable_quantity(free_inode_count)); + if (human_readable) { + out("{:>12} ", human_readable_quantity(total_inode_count, human_readable_based_on)); + out("{:>12} ", human_readable_quantity(used_inode_count, human_readable_based_on)); + out("{:>12} ", human_readable_quantity(free_inode_count, human_readable_based_on)); out("{:>11}% ", used_inode_percentage); } else { out("{:>12} ", (uint64_t)total_inode_count);