Skip to content

Commit a5ba93d

Browse files
committed
Add size formats as described in issue #102
1 parent c9b7a5e commit a5ba93d

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

features/db-size.feature

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,45 @@ Feature: Display database size
4949

5050
When I run `wp db size --size_format=mb`
5151
Then STDOUT should be a number
52-
52+
53+
5354
Scenario: Display only database size in gigabytes for a WordPress install
5455
Given a WP install
5556

5657
When I run `wp db size --size_format=gb`
5758
Then STDOUT should be a number
58-
59+
60+
5961
Scenario: Display only database size in terabytes for a WordPress install
6062
Given a WP install
6163

6264
When I run `wp db size --size_format=tb`
6365
Then STDOUT should be a number
66+
67+
68+
Scenario: Display only database size in Kibibytes for a WordPress install
69+
Given a WP install
70+
71+
When I run `wp db size --size_format=KB`
72+
Then STDOUT should be a number
73+
74+
75+
Scenario: Display only database size in Mebibytes for a WordPress install
76+
Given a WP install
77+
78+
When I run `wp db size --size_format=MB`
79+
Then STDOUT should be a number
80+
81+
82+
Scenario: Display only database size in Gibibytes for a WordPress install
83+
Given a WP install
84+
85+
When I run `wp db size --size_format=GB`
86+
Then STDOUT should be a number
87+
88+
89+
Scenario: Display only database size in Tebibytes for a WordPress install
90+
Given a WP install
91+
92+
When I run `wp db size --size_format=TB`
93+
Then STDOUT should be a number

src/DB_Command.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,15 @@ public function tables( $args, $assoc_args ) {
659659
* - mb (megabytes)
660660
* - gb (gigabytes)
661661
* - tb (terabytes)
662+
* - B (ISO Byte setting, with no conversion)
663+
* - KB (ISO Kilobyte setting, with 1 kB = 1,000 B)
664+
* - KiB (ISO Kibibyte setting, with 1 KiB = 1,024 B)
665+
* - MB (ISO Megabyte setting, with 1 MB = 1,000 kB)
666+
* - MiB (ISO Mebibyte setting, with 1 MiB = 1,024 KiB)
667+
* - GB (ISO Gigabyte setting, with 1 GB = 1,000 MB)
668+
* - GiB (ISO Gibibyte setting, with 1 GiB = 1,024 MiB)
669+
* - TB (ISO Terabyte setting, with 1 TB = 1,000 GB)
670+
* - TiB (ISO Tebibyte setting, with 1 TiB = 1,024 GiB)
662671
* ---
663672
*
664673
* [--tables]
@@ -800,29 +809,51 @@ public function size( $args, $assoc_args ) {
800809

801810
// Display the database size as a number.
802811
switch( $size_format ) {
812+
case 'TB':
813+
$divisor = pow( 1000, 4 );
814+
break;
815+
816+
case 'GB':
817+
$divisor = pow( 1000, 3 );
818+
break;
819+
820+
case 'MB':
821+
$divisor = pow( 1000, 2 );
822+
break;
823+
824+
case 'KB':
825+
$divisor = 1000;
826+
break;
827+
803828
case 'tb':
829+
case 'TiB':
804830
$divisor = TB_IN_BYTES;
805831
break;
806832

807833
case 'gb':
834+
case 'GiB':
808835
$divisor = GB_IN_BYTES;
809836
break;
810837

811838
case 'mb':
839+
case 'MiB':
812840
$divisor = MB_IN_BYTES;
813841
break;
814842

815843
case 'kb':
844+
case 'KiB':
816845
$divisor = KB_IN_BYTES;
817846
break;
818847

819848
case 'b':
849+
case 'B':
820850
default:
821851
$divisor = 1;
822852
break;
823853
}
854+
$size_format_display = preg_replace( '/IB$/u', 'iB', strtoupper( $size_format ) );
824855

825-
$rows[ $index ]['Size'] = ceil( $row['Size'] / $divisor ) . " " . strtoupper( $size_format );
856+
$rows[ $index ]['Size'] = ceil( $row['Size'] / $divisor ) . " " . $size_format_display;
826857
}
827858
}
828859

0 commit comments

Comments
 (0)