Skip to content

Commit

Permalink
Add format_size Twig filter
Browse files Browse the repository at this point in the history
stops filesizes sometimes being displayed as "0 kb
  • Loading branch information
fauxpark committed Jul 6, 2014
1 parent 1bef1b9 commit ff571bf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/GitList/Application.php
Expand Up @@ -65,6 +65,7 @@ public function __construct(Config $config, $root = null)
$twig->addFilter(new \Twig_SimpleFilter('htmlentities', 'htmlentities'));
$twig->addFilter(new \Twig_SimpleFilter('md5', 'md5'));
$twig->addFilter(new \Twig_SimpleFilter('format_date', array($app, 'formatDate')));
$twig->addFilter(new \Twig_SimpleFilter('format_size', array($app, 'formatSize')));

return $twig;
}));
Expand Down Expand Up @@ -97,6 +98,14 @@ public function formatDate($date)
return $date->format($this['date.format']);
}

public function formatSize($size)
{
$mod = 1000;
$units = array('B', 'kB', 'MB', 'GB');
for($i = 0; $size > $mod; $i++) $size /= $mod;
return round($size, 2) . $units[$i];
}

public function getPath()
{
return $this->path . DIRECTORY_SEPARATOR;
Expand Down
2 changes: 1 addition & 1 deletion themes/default/twig/stats.twig
Expand Up @@ -37,7 +37,7 @@
</p>

<p>
<strong>Total bytes:</strong> {{ stats.size }} bytes ({{ ((stats.size / 1024) / 1024) | number_format }} MB)
<strong>Total bytes:</strong> {{ stats.size }} bytes ({{ stats.size | format_size }})
</p>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion themes/default/twig/tree.twig
Expand Up @@ -50,7 +50,7 @@
{%- endif -%}
">{{ file.name }}</a></td>
<td>{{ file.mode }}</td>
<td>{% if file.size %}{{ (file.size / 1024) | number_format }} kb{% endif %}</td>
<td>{% if file.size %}{{ (file.size | format_size }}{% endif %}</td>
</tr>
{% endfor %}
</tbody>
Expand Down

0 comments on commit ff571bf

Please sign in to comment.