@@ -685,6 +685,9 @@ public function tables( $args, $assoc_args ) {
685
685
* [--tables]
686
686
* : Display each table name and size instead of the database size.
687
687
*
688
+ * [--human-readable]
689
+ * : Display database sizes in human readable formats.
690
+ *
688
691
* [--format]
689
692
* : table, csv, json
690
693
* ---
@@ -751,11 +754,17 @@ public function size( $args, $assoc_args ) {
751
754
752
755
$ format = WP_CLI \Utils \get_flag_value ( $ assoc_args , 'format ' );
753
756
$ 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 );
754
758
$ tables = WP_CLI \Utils \get_flag_value ( $ assoc_args , 'tables ' );
755
759
$ tables = ! empty ( $ tables );
756
760
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
+
757
765
unset( $ assoc_args ['format ' ] );
758
766
unset( $ assoc_args ['size_format ' ] );
767
+ unset( $ assoc_args ['human-readable ' ] );
759
768
unset( $ assoc_args ['tables ' ] );
760
769
761
770
if ( empty ( $ args ) && empty ( $ assoc_args ) ) {
@@ -766,7 +775,7 @@ public function size( $args, $assoc_args ) {
766
775
$ rows = array ();
767
776
$ fields = array ( 'Name ' , 'Size ' );
768
777
769
- $ default_unit = ( empty ( $ size_format ) ) ? ' B ' : '' ;
778
+ $ default_unit = ( empty ( $ size_format ) && ! $ human_readable ) ? ' B ' : '' ;
770
779
771
780
if ( $ tables ) {
772
781
@@ -803,7 +812,7 @@ public function size( $args, $assoc_args ) {
803
812
);
804
813
}
805
814
806
- if ( ! empty ( $ size_format ) ) {
815
+ if ( ! empty ( $ size_format ) || $ human_readable ) {
807
816
foreach ( $ rows as $ index => $ row ) {
808
817
// These added WP 4.4.0.
809
818
if ( ! defined ( 'KB_IN_BYTES ' ) ) {
@@ -819,6 +828,13 @@ public function size( $args, $assoc_args ) {
819
828
define ( 'TB_IN_BYTES ' , 1024 * GB_IN_BYTES );
820
829
}
821
830
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
+
822
838
// Display the database size as a number.
823
839
switch ( $ size_format ) {
824
840
case 'TB ' :
@@ -869,7 +885,7 @@ public function size( $args, $assoc_args ) {
869
885
}
870
886
}
871
887
872
- if ( ! empty ( $ size_format ) && ! $ tables && ! $ format ) {
888
+ if ( ! empty ( $ size_format ) && ! $ tables && ! $ format && ! $ human_readable ) {
873
889
WP_CLI ::Line ( filter_var ( $ rows [0 ]['Size ' ], FILTER_SANITIZE_NUMBER_INT ) );
874
890
} else {
875
891
// Display the rows.
0 commit comments