Skip to content

Commit 4754f26

Browse files
committed
Adds —human-readable parameter to db size command
1 parent 99188aa commit 4754f26

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/DB_Command.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ public function tables( $args, $assoc_args ) {
685685
* [--tables]
686686
* : Display each table name and size instead of the database size.
687687
*
688+
* [--human-readable]
689+
* : Display database sizes in human readable formats.
690+
*
688691
* [--format]
689692
* : table, csv, json
690693
* ---
@@ -751,11 +754,17 @@ public function size( $args, $assoc_args ) {
751754

752755
$format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );
753756
$size_format = WP_CLI\Utils\get_flag_value( $assoc_args, 'size_format' );
757+
$human_readable = WP_CLI\Utils\get_flag_value( $assoc_args, 'human-readable', false );
754758
$tables = WP_CLI\Utils\get_flag_value( $assoc_args, 'tables' );
755759
$tables = ! empty( $tables );
756760

761+
if( ! is_null( $size_format ) && $human_readable ) {
762+
WP_CLI::error( "The size_format and human-readable arguments were both passed." );
763+
}
764+
757765
unset( $assoc_args['format'] );
758766
unset( $assoc_args['size_format'] );
767+
unset( $assoc_args['human-readable'] );
759768
unset( $assoc_args['tables'] );
760769

761770
if ( empty( $args ) && empty( $assoc_args ) ) {
@@ -766,7 +775,7 @@ public function size( $args, $assoc_args ) {
766775
$rows = array();
767776
$fields = array( 'Name', 'Size' );
768777

769-
$default_unit = ( empty( $size_format ) ) ? ' B' : '';
778+
$default_unit = ( empty( $size_format ) && ! $human_readable ) ? ' B' : '';
770779

771780
if ( $tables ) {
772781

@@ -803,7 +812,7 @@ public function size( $args, $assoc_args ) {
803812
);
804813
}
805814

806-
if ( ! empty( $size_format ) ) {
815+
if ( ! empty( $size_format ) || $human_readable ) {
807816
foreach( $rows as $index => $row ) {
808817
// These added WP 4.4.0.
809818
if ( ! defined( 'KB_IN_BYTES' ) ) {
@@ -819,6 +828,13 @@ public function size( $args, $assoc_args ) {
819828
define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES );
820829
}
821830

831+
if( $human_readable ) {
832+
$size_key = floor( log( $row['Size'] ) / log( 1024 ) );
833+
$sizes = array( 'B', 'KB', 'MB', 'GB', 'TB' );
834+
835+
$size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[ 0 ];
836+
}
837+
822838
// Display the database size as a number.
823839
switch( $size_format ) {
824840
case 'TB':

0 commit comments

Comments
 (0)