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',
}
- # 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;
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;
//
// 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) {
$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++;
}
. ltrim($o['sep'])
. join($o['sep'],$values)
. rtrim($o['sep'])
- ;
+ ."\n";
}
//
#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' );