From ff571bf3b1b556bc55a7cd3306648e798a19eb7a Mon Sep 17 00:00:00 2001
From: fauxpark
Date: Sun, 6 Jul 2014 22:59:57 +1000
Subject: [PATCH 1/2] Add format_size Twig filter
stops filesizes sometimes being displayed as "0 kb
---
src/GitList/Application.php | 9 +++++++++
themes/default/twig/stats.twig | 2 +-
themes/default/twig/tree.twig | 2 +-
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/src/GitList/Application.php b/src/GitList/Application.php
index 18cc681..34eb03e 100644
--- a/src/GitList/Application.php
+++ b/src/GitList/Application.php
@@ -65,6 +65,7 @@ class Application extends SilexApplication
$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;
}));
@@ -97,6 +98,14 @@ class Application extends SilexApplication
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;
diff --git a/themes/default/twig/stats.twig b/themes/default/twig/stats.twig
index 7d36093..495663e 100644
--- a/themes/default/twig/stats.twig
+++ b/themes/default/twig/stats.twig
@@ -37,7 +37,7 @@
- Total bytes: {{ stats.size }} bytes ({{ ((stats.size / 1024) / 1024) | number_format }} MB)
+ Total bytes: {{ stats.size }} bytes ({{ stats.size | format_size }})
diff --git a/themes/default/twig/tree.twig b/themes/default/twig/tree.twig
index a33bbb4..828275f 100644
--- a/themes/default/twig/tree.twig
+++ b/themes/default/twig/tree.twig
@@ -50,7 +50,7 @@
{%- endif -%}
">{{ file.name }}
{{ file.mode }} |
- {% if file.size %}{{ (file.size / 1024) | number_format }} kb{% endif %} |
+ {% if file.size %}{{ (file.size | format_size }}{% endif %} |
{% endfor %}
From 2c83a741587ddfa1205221efab07a818df3beeee Mon Sep 17 00:00:00 2001
From: fauxpark
Date: Sun, 6 Jul 2014 23:50:59 +1000
Subject: [PATCH 2/2] Missed a parenthesis
---
themes/default/twig/tree.twig | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/themes/default/twig/tree.twig b/themes/default/twig/tree.twig
index 828275f..b3f84f5 100644
--- a/themes/default/twig/tree.twig
+++ b/themes/default/twig/tree.twig
@@ -50,7 +50,7 @@
{%- endif -%}
">{{ file.name }}
{{ file.mode }} |
- {% if file.size %}{{ (file.size | format_size }}{% endif %} |
+ {% if file.size %}{{ file.size | format_size }}{% endif %} |
{% endfor %}