@@ -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 ( "Cannot use --size_format and --human-readable arguments at the same time. " );
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 ( 1000 ) );
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 ' :
@@ -869,7 +885,7 @@ public function size( $args, $assoc_args ) {
869885 }
870886 }
871887
872- if ( ! empty ( $ size_format ) && ! $ tables && ! $ format ) {
888+ if ( ! empty ( $ size_format ) && ! $ tables && ! $ format && ! $ human_readable ) {
873889 WP_CLI ::Line ( filter_var ( $ rows [0 ]['Size ' ], FILTER_SANITIZE_NUMBER_INT ) );
874890 } else {
875891 // Display the rows.
0 commit comments