| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2016 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | package App::Devel::MAT::Explorer::GTK::Utils; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | |||
| 11 | use Exporter 'import'; | ||
| 12 | |||
| 13 | our @EXPORT = qw( | ||
| 14 | bytes2size | ||
| 15 | ); | ||
| 16 | |||
| 17 | sub bytes2size | ||
| 18 | { | ||
| 19 | my ( $bytes ) = @_; | ||
| 20 | |||
| 21 | if( $bytes < 1024 ) { | ||
| 22 | return $bytes | ||
| 23 | } | ||
| 24 | if( $bytes < 1024**2 ) { | ||
| 25 | return sprintf "%.1f Ki", $bytes / 1024; | ||
| 26 | } | ||
| 27 | if( $bytes < 1024**3 ) { | ||
| 28 | return sprintf "%.1f Mi", $bytes / 1024**2; | ||
| 29 | } | ||
| 30 | if( $bytes < 1024**4 ) { | ||
| 31 | return sprintf "%.1f Gi", $bytes / 1024**3; | ||
| 32 | } | ||
| 33 | return sprintf "%.1f Ti", $bytes / 1024**4; | ||
| 34 | } | ||
| 35 |