Skip to content

Commit c260be5

Browse files
authored
Merge pull request #104 from fumikito/fix-size-format
Add size formats as described in issue #102
2 parents 27dd210 + 5b5b47f commit c260be5

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

features/db-size.feature

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,50 @@ Feature: Display database size
3636
When I run `wp db size --size_format=b`
3737
Then STDOUT should be a number
3838

39-
4039
Scenario: Display only database size in kilobytes for a WordPress install
4140
Given a WP install
4241

4342
When I run `wp db size --size_format=kb`
4443
Then STDOUT should be a number
4544

46-
4745
Scenario: Display only database size in megabytes for a WordPress install
4846
Given a WP install
4947

5048
When I run `wp db size --size_format=mb`
5149
Then STDOUT should be a number
52-
50+
5351
Scenario: Display only database size in gigabytes for a WordPress install
5452
Given a WP install
5553

5654
When I run `wp db size --size_format=gb`
5755
Then STDOUT should be a number
58-
56+
5957
Scenario: Display only database size in terabytes for a WordPress install
6058
Given a WP install
6159

6260
When I run `wp db size --size_format=tb`
6361
Then STDOUT should be a number
62+
63+
Scenario: Display only database size in Kibibytes for a WordPress install
64+
Given a WP install
65+
66+
When I run `wp db size --size_format=KB`
67+
Then STDOUT should be a number
68+
69+
Scenario: Display only database size in Mebibytes for a WordPress install
70+
Given a WP install
71+
72+
When I run `wp db size --size_format=MB`
73+
Then STDOUT should be a number
74+
75+
Scenario: Display only database size in Gibibytes for a WordPress install
76+
Given a WP install
77+
78+
When I run `wp db size --size_format=GB`
79+
Then STDOUT should be a number
80+
81+
Scenario: Display only database size in Tebibytes for a WordPress install
82+
Given a WP install
83+
84+
When I run `wp db size --size_format=TB`
85+
Then STDOUT should be a number

src/DB_Command.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,15 @@ public function tables( $args, $assoc_args ) {
666666
* - mb (megabytes)
667667
* - gb (gigabytes)
668668
* - tb (terabytes)
669+
* - B (ISO Byte setting, with no conversion)
670+
* - KB (ISO Kilobyte setting, with 1 KB = 1,000 B)
671+
* - KiB (ISO Kibibyte setting, with 1 KiB = 1,024 B)
672+
* - MB (ISO Megabyte setting, with 1 MB = 1,000 KB)
673+
* - MiB (ISO Mebibyte setting, with 1 MiB = 1,024 KiB)
674+
* - GB (ISO Gigabyte setting, with 1 GB = 1,000 MB)
675+
* - GiB (ISO Gibibyte setting, with 1 GiB = 1,024 MiB)
676+
* - TB (ISO Terabyte setting, with 1 TB = 1,000 GB)
677+
* - TiB (ISO Tebibyte setting, with 1 TiB = 1,024 GiB)
669678
* ---
670679
*
671680
* [--tables]
@@ -807,29 +816,51 @@ public function size( $args, $assoc_args ) {
807816

808817
// Display the database size as a number.
809818
switch( $size_format ) {
819+
case 'TB':
820+
$divisor = pow( 1000, 4 );
821+
break;
822+
823+
case 'GB':
824+
$divisor = pow( 1000, 3 );
825+
break;
826+
827+
case 'MB':
828+
$divisor = pow( 1000, 2 );
829+
break;
830+
831+
case 'KB':
832+
$divisor = 1000;
833+
break;
834+
810835
case 'tb':
836+
case 'TiB':
811837
$divisor = TB_IN_BYTES;
812838
break;
813839

814840
case 'gb':
841+
case 'GiB':
815842
$divisor = GB_IN_BYTES;
816843
break;
817844

818845
case 'mb':
846+
case 'MiB':
819847
$divisor = MB_IN_BYTES;
820848
break;
821849

822850
case 'kb':
851+
case 'KiB':
823852
$divisor = KB_IN_BYTES;
824853
break;
825854

826855
case 'b':
856+
case 'B':
827857
default:
828858
$divisor = 1;
829859
break;
830860
}
861+
$size_format_display = preg_replace( '/IB$/u', 'iB', strtoupper( $size_format ) );
831862

832-
$rows[ $index ]['Size'] = ceil( $row['Size'] / $divisor ) . " " . strtoupper( $size_format );
863+
$rows[ $index ]['Size'] = ceil( $row['Size'] / $divisor ) . " " . $size_format_display;
833864
}
834865
}
835866

0 commit comments

Comments
 (0)