From: Nicolas Boisselier Date: Sun, 4 Sep 2016 00:32:12 +0000 (+0100) Subject: Bed X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=79659d29150a27e056aea58ba50b18cab02ed031;p=nb.git Bed --- diff --git a/lib/php/out.php b/lib/php/out.php index d7fb5247..66884920 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -9,10 +9,9 @@ Class Out extends Nb { public static function init() { self::$types = array( 'human' => [ - 'enclose' => array("","\n"), - 'eol' => "\n", + #'enclose' => array("","\n"), + #'eol' => "\n", 'sep' => (self::p('sep') ? self::p('sep') : " | "), - 'line' => '-', 'border' => '_', 'head' => 'out_human_head', 'function' => 'out_human', @@ -183,26 +182,6 @@ Class Out extends Nb { } - # For no scalar search max length - if (self::p('human') and !empty($data) and !is_scalar($data)) { - $o['fields_max_len'] = []; - - foreach ($data as $rec) { - $i = 0; - foreach ($rec as $k=>$v) { - if (!is_scalar($v)) continue; - if (0 - or empty($o['fields_max_len'][$i]) - or strlen($v) > $o['fields_max_len'][$i] - ) $o['fields_max_len'][$i] = strlen($v); - #echo strlen($v)."\n"; - $i++; - } - } - - #echo "LENGTH: "; bye( $o['fields_max_len'] ); - } - #bye($o); #if (self::p('header')==="0") return; @@ -220,6 +199,8 @@ Class Out extends Nb { if (self::p('header') === '0' ) return; if ($head === false) return; + + $o['data'] = $data; echo $o['head']($head,$o) . empty($o['eol']) ? '' : $o['eol']; return $head; @@ -420,15 +401,56 @@ function out_yaml(&$row,$o) { // // Human // -function out_human_head(&$row,$o) { - if (!is_array($row)) echo 0; +function out_human_head(&$head,&$o) { + if (!is_array($head)) { + echo 0; + return; + } + # For no scalar search max length + $o['fields_max_len'] = []; + + foreach ($o['data'] as $rec) { + $i = 0; + foreach ($rec as $k=>$v) { + if (!is_scalar($v)) continue; + if (0 + or empty($o['fields_max_len'][$i]) + or strlen($v) >= $o['fields_max_len'][$i] + ) $o['fields_max_len'][$i] = strlen($v); + #echo strlen($v)."\n"; + $i++; + } + } - if (out::is_hash($row)) { - $ar = array_keys($row); - out_human($ar,$o); - } else { - out_human($row,$o); + if (out::is_hash($head)) { + $head = array_keys($head); } + + $i = 0; + foreach ($head as $k => $v) { + if (!is_scalar($v)) continue; + if (0 + or empty($o['fields_max_len'][$i]) + or strlen($v) >= $o['fields_max_len'][$i] + ) $o['fields_max_len'][$i] = strlen($v); + #echo strlen($v)."\n"; + $i++; + } + + #debug($line); + #$o['line'] = $line; + #echo "LENGTH: "; bye( $o['fields_max_len'] ); + #echo "LENGTH: "; + #bye([ 'fields_max_len' => $o['fields_max_len'], 'data' => $o['data'], ]); + + $line = '+'; + foreach ($o['fields_max_len'] as $i=>$v) { + $line .= str_repeat('-',$v+strlen(trim($o['sep']))+1).'+'; + } + $o['line'] = $line; + echo "$line\n"; + out_human($head,$o); + echo "$line\n"; } function out_human(&$row,$o) { @@ -438,9 +460,13 @@ function out_human(&$row,$o) { $i = 0; $values = []; foreach (array_values($row) as $k=>$v) { - $values[] = sprintf("%" - .(empty($o['fields_max_len']) and empty($o['fields_max_len'][$i]) ? '' : '-'.$o['fields_max_len'][$i]) + #debug(strlen($v).' == '.$o['fields_max_len'][$i]); + #debug("%".$o['fields_max_len'][$i]."s"); + + $values[] = sprintf("%".'-'.$o['fields_max_len'][$i] + #.(empty($o['fields_max_len']) or empty($o['fields_max_len'][$i]) ? '' : '-'.$o['fields_max_len'][$i]) ."s",$v); + $i++; } @@ -449,7 +475,7 @@ function out_human(&$row,$o) { . ltrim($o['sep']) . join($o['sep'],$values) . rtrim($o['sep']) - ; + ."\n"; } // @@ -537,8 +563,8 @@ function out_xml(&$row,$o) { #die("A NB_EOL B \n"); if (empty($argv) or count($argv) < 2 or $argv[1] != 'test') return true; $data = array( - 'fatc_name_idx' => 'name', - 'fatc_value_idx' => 'value', + 'name' => 'Name 1', + 'value' => 'Value 1', ); /* $data = array( 'a', 'b', 'c' );