Example #1
0
 /**
  * Renders a row for output.
  *
  * @param array  $row  The table row.
  * @return string  The formatted table row.
  */
 public function row(array $row)
 {
     $extra_rows = array_fill(0, count($row), array());
     $extra_row_count = 0;
     foreach ($row as $col => $value) {
         $value = str_replace(PHP_EOL, ' ', $value);
         $col_width = $this->_widths[$col];
         $original_val_width = Colors::length($value);
         if ($original_val_width > $col_width) {
             $row[$col] = \cli\safe_substr($value, 0, $col_width);
             $value = \cli\safe_substr($value, $col_width, $original_val_width);
             $i = 0;
             do {
                 $extra_value = \cli\safe_substr($value, 0, $col_width);
                 $val_width = \cli\safe_strlen($extra_value);
                 if ($val_width) {
                     $extra_rows[$col][] = $extra_value;
                     $value = \cli\safe_substr($value, $col_width, $original_val_width);
                     $i++;
                     if ($i > $extra_row_count) {
                         $extra_row_count = $i;
                     }
                 }
             } while ($value);
         }
     }
     $row = array_map(array($this, 'padColumn'), $row, array_keys($row));
     array_unshift($row, '');
     // First border
     array_push($row, '');
     // Last border
     $ret = join($this->_characters['border'], $row);
     if ($extra_row_count) {
         foreach ($extra_rows as $col => $col_values) {
             while (count($col_values) < $extra_row_count) {
                 $col_values[] = '';
             }
         }
         do {
             $row_values = array();
             $has_more = false;
             foreach ($extra_rows as $col => &$col_values) {
                 $row_values[$col] = array_shift($col_values);
                 if (count($col_values)) {
                     $has_more = true;
                 }
             }
             $row_values = array_map(array($this, 'padColumn'), $row_values, array_keys($row_values));
             array_unshift($row_values, '');
             // First border
             array_push($row_values, '');
             // Last border
             $ret .= PHP_EOL . join($this->_characters['border'], $row_values);
         } while ($has_more);
     }
     return $ret;
 }
Example #2
0
 function test_encoded_substr()
 {
     $this->assertEquals(\cli\safe_substr(\cli\Colors::pad('hello', 6), 0, 2), 'he');
     $this->assertEquals(\cli\safe_substr(\cli\Colors::pad('óra', 6), 0, 2), 'ór');
     $this->assertEquals(\cli\safe_substr(\cli\Colors::pad('日本語', 6), 0, 2), '日本');
 }