Skip to content

Commit 0a224e1

Browse files
committed
Show file size in the success message
1 parent 1d0adc4 commit 0a224e1

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

src/Dist_Archive_Command.php

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ function ( $ignored_file ) use ( $source_path ) {
171171
$escaped_shell_command = $this->escapeshellcmd( $cmd, $escape_whitelist );
172172
$ret = WP_CLI::launch( $escaped_shell_command, false, true );
173173
if ( 0 === $ret->return_code ) {
174-
$filename = pathinfo( $archive_absolute_filepath, PATHINFO_BASENAME );
175-
WP_CLI::success( "Created {$filename}" );
174+
$filename = pathinfo( $archive_absolute_filepath, PATHINFO_BASENAME );
175+
$file_size = $this->get_size_format( filesize( $archive_absolute_filepath ), 2 );
176+
177+
WP_CLI::success( "Created {$filename} (Size: {$file_size})" );
176178
} else {
177179
$error = $ret->stderr ?: $ret->stdout;
178180
WP_CLI::error( $error );
@@ -513,4 +515,82 @@ private function get_file_list( $source_dir_path, $excluded = false ) {
513515

514516
return $excluded ? $excluded_files : $included_files;
515517
}
518+
519+
/**
520+
* Converts a number of bytes to the largest unit the bytes will fit into.
521+
*
522+
* @param int $bytes Number of bytes.
523+
* @param int $decimals Precision of number of decimal places.
524+
* @return string Number string.
525+
*/
526+
private function get_size_format( $bytes, $decimals = 0 ) {
527+
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Backfilling WP native constants.
528+
if ( ! defined( 'KB_IN_BYTES' ) ) {
529+
define( 'KB_IN_BYTES', 1024 );
530+
}
531+
if ( ! defined( 'MB_IN_BYTES' ) ) {
532+
define( 'MB_IN_BYTES', 1024 * KB_IN_BYTES );
533+
}
534+
if ( ! defined( 'GB_IN_BYTES' ) ) {
535+
define( 'GB_IN_BYTES', 1024 * MB_IN_BYTES );
536+
}
537+
if ( ! defined( 'TB_IN_BYTES' ) ) {
538+
define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES );
539+
}
540+
// phpcs:enable
541+
542+
$size_key = floor( log( $bytes ) / log( 1000 ) );
543+
$sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
544+
545+
$size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0];
546+
547+
// Display the size as a number.
548+
switch ( $size_format ) {
549+
case 'TB':
550+
$divisor = pow( 1000, 4 );
551+
break;
552+
553+
case 'GB':
554+
$divisor = pow( 1000, 3 );
555+
break;
556+
557+
case 'MB':
558+
$divisor = pow( 1000, 2 );
559+
break;
560+
561+
case 'KB':
562+
$divisor = 1000;
563+
break;
564+
565+
case 'tb':
566+
case 'TiB':
567+
$divisor = TB_IN_BYTES;
568+
break;
569+
570+
case 'gb':
571+
case 'GiB':
572+
$divisor = GB_IN_BYTES;
573+
break;
574+
575+
case 'mb':
576+
case 'MiB':
577+
$divisor = MB_IN_BYTES;
578+
break;
579+
580+
case 'kb':
581+
case 'KiB':
582+
$divisor = KB_IN_BYTES;
583+
break;
584+
585+
case 'b':
586+
case 'B':
587+
default:
588+
$divisor = 1;
589+
break;
590+
}
591+
592+
$size_format_display = preg_replace( '/IB$/u', 'iB', strtoupper( $size_format ) );
593+
594+
return round( (int) $bytes / $divisor, $decimals ) . ' ' . $size_format_display;
595+
}
516596
}

0 commit comments

Comments
 (0)