]> git.nbdom.net Git - nb.git/commitdiff
fix human bugs
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 5 Sep 2016 21:35:58 +0000 (22:35 +0100)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 5 Sep 2016 21:35:58 +0000 (22:35 +0100)
lib/php/db/table.php
lib/php/out.php

index bb3d4dea11e540bf82f410346d8cdf6be658147d..37b2ea963d0ffa598bddf06bf5d9221fe9cfac58 100644 (file)
@@ -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 ''; }
 
   /*-----------------------------------------------------------------
index b6babb4e182a7dd9e48cce33fba13aa0e0be8635..88bece4d285019d630502b2e99516bf98281f8c3 100644 (file)
@@ -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
 /****************************************************************************/