summaryrefslogtreecommitdiffstats
path: root/application
diff options
context:
space:
mode:
Diffstat (limited to 'application')
-rw-r--r--application/service/files.php38
-rw-r--r--application/test/tests/test_service_files.php32
2 files changed, 70 insertions, 0 deletions
diff --git a/application/service/files.php b/application/service/files.php
index c53448eb6..8f7595c50 100644
--- a/application/service/files.php
+++ b/application/service/files.php
@@ -95,6 +95,44 @@ class files {
self::add_file_callback($id, $file, $filename);
}
+ /**
+ * Ellipsize text to be at max $max_lines lines long. If the last line is
+ * not complete (strlen($text) < $filesize), drop it so that every line of
+ * the returned text is complete. If there is only one line, return that
+ * line as is and add the ellipses at the end.
+ *
+ * @param text Text to add ellipses to
+ * @param max_lines Number of lines the returned text should contain
+ * @param filesize size of the original file where the text comes from
+ * @return ellipsized text
+ */
+ static public function ellipsize($text, $max_lines, $filesize)
+ {
+ $lines = explode("\n", $text);
+ $orig_len = strlen($text);
+ $orig_linecount = count($lines);
+
+ if ($orig_linecount > 1) {
+ if ($orig_len < $filesize) {
+ // ensure we have a full line at the end
+ $lines = array_slice($lines, 0, -1);
+ }
+
+ if (count($lines) > $max_lines) {
+ $lines = array_slice($lines, 0, $max_lines);
+ }
+
+ if (count($lines) != $orig_linecount) {
+ // only add elipses when we drop at least one line
+ $lines[] = "...";
+ }
+ } elseif ($orig_len < $filesize) {
+ $lines[count($lines) - 1] .= " ...";
+ }
+
+ return implode("\n", $lines);
+ }
+
static public function add_uploaded_file($id, $file, $filename)
{
self::add_file_callback($id, $file, $filename);
diff --git a/application/test/tests/test_service_files.php b/application/test/tests/test_service_files.php
index 21688230f..401ddbe86 100644
--- a/application/test/tests/test_service_files.php
+++ b/application/test/tests/test_service_files.php
@@ -80,6 +80,38 @@ class test_service_files extends \test\Test {
}
}
+ public function test_ellipsize()
+ {
+ $a1 = "abc";
+ $a2 = "abc\nabc";
+ $a3 = "abc\nabc\nabc";
+ $a4 = "abc\nabc\nabc\nabc";
+
+ $this->t->is(\service\files::ellipsize($a1, 1, strlen($a1)),
+ $a1, "Trim 1 line to 1, no change");
+
+ $this->t->is(\service\files::ellipsize($a3, 3, strlen($a3)),
+ $a3, "Trim 3 lines to 3, no change");
+
+ $this->t->is(\service\files::ellipsize($a3, 5, strlen($a3)),
+ $a3, "Trim 3 lines to 5, no change");
+
+ $this->t->is(\service\files::ellipsize($a2, 1, strlen($a2)),
+ "$a1\n...", "Trim 2 lines to 1, drop one line");
+
+ $this->t->is(\service\files::ellipsize($a3, 2, strlen($a3)),
+ "$a2\n...", "Trim 3 lines to 2, drop one line");
+
+ $this->t->is(\service\files::ellipsize($a4, 2, strlen($a4)),
+ "$a2\n...", "Trim 4 lines to 2, drop 2 lines");
+
+ $this->t->is(\service\files::ellipsize($a3, 3, strlen($a3) + 1),
+ "$a2\n...", "Last line incomplete, drop one line");
+
+ $this->t->is(\service\files::ellipsize($a1, 5, strlen($a1) + 1),
+ "$a1 ...", "Single line incomplete, only add dots");
+ }
+
}