From b8ea65d4f607ffd2d04fafdee972bc536b80b4e9 Mon Sep 17 00:00:00 2001 From: Gilles Crettenand Date: Wed, 3 Jun 2015 10:47:00 +0200 Subject: [PATCH] improve HTML output --- Lib/Formatter/Html.php | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/Lib/Formatter/Html.php b/Lib/Formatter/Html.php index 166d201..72db84a 100644 --- a/Lib/Formatter/Html.php +++ b/Lib/Formatter/Html.php @@ -10,11 +10,36 @@ class Html extends Formatter { )); } + protected function truncate($v, $ellipsis = ' [...]') { + $limit = 50; + if(strlen($v) > $limit) { + $v = substr($v, 0, $limit).$ellipsis; + } + return $v; + } + + protected function formatValue($v) { + if(is_numeric($v)) { + return $v; + } + if(is_bool($v)) { + return ''; + } + if(is_string($v) && strpos($v, 'http') !== false) { + $url = $v; + $v = $this->truncate($v, '...'); + return "$v"; + } + + return $this->truncate(print_r($v, true)); + } + protected function result($data) { // the format is array('result' => array('function name' => DATA)) // so take the first element of the 'result' array $func = key($data['result']); $data = reset($data['result']); + $title = $func; $first = reset($data); $single = ! is_array($first); @@ -25,6 +50,8 @@ class Html extends Formatter { } else { $columns = array_keys($first); + $title .= ' ('.count($data).' results)'; + $content .= ''; foreach($columns as $k) { $content .= "$k"; @@ -34,19 +61,13 @@ class Html extends Formatter { $content .= ''; if($single) { foreach($data as $k => $v) { - $content .= "$k".print_r($v, true).""; + $content .= "$k".$this->formatValue($v).""; } } else { foreach($data as $row) { $content .= ''; foreach($columns as $c) { - $v = print_r(isset($row[$c]) ? $row[$c] : '', true); - - $limit = 50; - if(strlen($v) > $limit) { - $v = substr($v, 0, $limit).' [...]'; - } - $content .= "$v"; + $content .= ''.$this->formatValue(isset($row[$c]) ? $row[$c] : '').''; } $content .= ''; } @@ -54,7 +75,7 @@ class Html extends Formatter { $content .= ''; return array( - 'title' => $func, + 'title' => $title, 'content' => $content, 'status' => 'success', );