From 48d56092038e7809cf0b4ca388e5aedf348b65b9 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 5 Sep 2016 22:35:58 +0100 Subject: [PATCH] fix human bugs --- lib/php/db/table.php | 8 ++++---- lib/php/out.php | 41 +++++++++++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/lib/php/db/table.php b/lib/php/db/table.php index bb3d4dea..37b2ea96 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -730,7 +730,7 @@ Class Table extends nb { return true; } - public function rows(&$opt=array(),$opt_by_val=null) { + public function rows(&$opt=[],$opt_by_val=null) { $this->create_temporary(); @@ -769,14 +769,14 @@ Class Table extends nb { # Use the module out when format unknow $out_conf = null; if ($this->p('out') or !preg_match('/^('.join('|', - array( 'table','sql','div','wp','_csv','_xml','_json','_yaml' ) # local + [ 'table','sql','div','wp','_csv','_xml','_json','_yaml' ] # local ).')$/',$format)) { if (!($out_conf = out::type($format))) $this->bye("Unknow format `$format`"); self::$params += array_values(out::$types); - if (empty($out_conf['enclose'])) $out_conf['enclose'] = array('',''); + if (empty($out_conf['enclose'])) $out_conf['enclose'] = ['','']; debug('Using out module!',3); } @@ -1041,7 +1041,7 @@ Class Table extends nb { -----------------------------------------------------------------*/ public function rows_begin_yaml() { return "---\n"; } #public function rows_rec_yaml(&$row) { return $this->yaml_encode($row); } - public function rows_rec_yaml(&$row) { out::row(array("function"=>"out_yaml"),$row); } + public function rows_rec_yaml(&$row) { $o=["function"=>"out_yaml"];out::row($o,$row); } public function rows_end_yaml() { return ''; } /*----------------------------------------------------------------- diff --git a/lib/php/out.php b/lib/php/out.php index b6babb4e..88bece4d 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -180,7 +180,6 @@ Class Out extends Nb { #bye($o); - #if (self::p('header')==="0") return; if (isset($o['enclose'])) echo $o['enclose'][0]; if (!isset($o['head'])) return; @@ -196,13 +195,12 @@ 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; } - public static function row($o,&$row) { + public static function row(&$o,&$row) { # Defined function if (isset($o['function'])) { @@ -485,17 +483,24 @@ function out_xml(&$row,$o) { // Human // function out_human_head(&$head,&$o) { - $o['return'] = true; - $o['sep.human'] = $o['sep']; - $o['sep'] = "\t"; - $GLOBALS['_human_len'] = []; - $o['tmpfile'] = tmpfile(); #$line = "a\tb\tc\t\t\tg"; bye(explode("\t",$line)); + if (out::p('header')==='0') return ''; + $o['_human'] = [ + 'head' => 1, + ]; if (out::is_hash($head)) $head = array_keys($head); return out_human($head,$o); } function out_human(&$row,&$o) { + if (empty($GLOBALS['_human_len'])) { + $GLOBALS['_human_len'] = []; + if (empty($o['_human'])) $o['_human'] = []; + $o['sep.human'] = $o['sep']; + $o['sep'] = "\t"; + $o['return'] = true; + $o['tmpfile'] = tmpfile(); + } $line = out_csv($row,$o); $i = 0; @@ -526,6 +531,7 @@ function out_human_end(&$o) { $sep_line .= '+'."\n"; } + $header = empty($o['_human']['head']) ? 0 : 1; $count = 0; while (($line = fgets($o['tmpfile'])) !== false) { $values = []; @@ -534,22 +540,33 @@ function out_human_end(&$o) { $values[] = sprintf("%".'-'.$len[$i]."s",$v); $i++; } - if (!$count) echo $sep_line; - echo '' + # header + if (!$count) { + echo $sep_line; + } + + #debug($count); + if ($header or $count>0)echo '' . ltrim($o['sep.human']) . join($o['sep.human'],$values) . rtrim($o['sep.human']) ."\n"; - if (!$count) echo $sep_line; + if ($header and !$count) echo $sep_line; $count++; } fclose($o['tmpfile']); + unset($GLOBALS['_human_len']); + unset($o['_human']); + + if ($header) $count--; if (!$count) return; + echo $sep_line; - print "$count Records\n"; + echo "$count Records\n"; + } # < Functions /****************************************************************************/ -- 2.47.3