Changeset View
Changeset View
Standalone View
Standalone View
src/console/grid/ArcanistGridView.php
| <?php | <?php | ||||
| final class ArcanistGridView | final class ArcanistGridView | ||||
| extends Phobject { | extends Phobject { | ||||
| private $rows = array(); | private $rows = array(); | ||||
| private $columns = array(); | private $columns = array(); | ||||
| private $displayWidths = array(); | private $displayWidths = array(); | ||||
| public function setColumns(array $columns) { | public function setColumns(array $columns) { | ||||
| assert_instances_of($columns, 'ArcanistGridColumn'); | assert_instances_of($columns, 'ArcanistGridColumn'); | ||||
| $this->columns = $columns; | $this->columns = mpull($columns, null, 'getKey'); | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getColumns() { | public function getColumns() { | ||||
| return $this->columns; | return $this->columns; | ||||
| } | } | ||||
| public function newColumn($key) { | public function newColumn($key) { | ||||
| Show All 29 Lines | foreach ($this->rows as $row) { | ||||
| $rows[] = $this->drawRow($row); | $rows[] = $this->drawRow($row); | ||||
| } | } | ||||
| $rows = phutil_glue($rows, tsprintf("\n")); | $rows = phutil_glue($rows, tsprintf("\n")); | ||||
| return tsprintf("%s\n", $rows); | return tsprintf("%s\n", $rows); | ||||
| } | } | ||||
| private function getDisplayWidth($key) { | private function getDisplayWidth($display_key) { | ||||
| if (!isset($this->displayWidths[$key])) { | if (!isset($this->displayWidths[$display_key])) { | ||||
| $column = $this->getColumn($key); | $flexible_columns = array(); | ||||
| $columns = $this->getColumns(); | |||||
| foreach ($columns as $key => $column) { | |||||
| $width = $column->getDisplayWidth(); | $width = $column->getDisplayWidth(); | ||||
| if ($width === null) { | if ($width === null) { | ||||
| $width = 1; | $width = 1; | ||||
| foreach ($this->getRows() as $row) { | foreach ($this->getRows() as $row) { | ||||
| if (!$row->hasCell($key)) { | if (!$row->hasCell($key)) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| $cell = $row->getCell($key); | $cell = $row->getCell($key); | ||||
| $width = max($width, $cell->getContentDisplayWidth()); | $width = max($width, $cell->getContentDisplayWidth()); | ||||
| } | } | ||||
| } | } | ||||
| if ($column->getMinimumWidth() !== null) { | |||||
| $flexible_columns[] = $key; | |||||
| } | |||||
| $this->displayWidths[$key] = $width; | $this->displayWidths[$key] = $width; | ||||
| } | } | ||||
| return $this->displayWidths[$key]; | $available_width = phutil_console_get_terminal_width(); | ||||
| // Adjust the available width to account for cell spacing. | |||||
| $available_width -= (2 * (count($columns) - 1)); | |||||
| while (true) { | |||||
| $total_width = array_sum($this->displayWidths); | |||||
| if ($total_width <= $available_width) { | |||||
| break; | |||||
| } | |||||
| if (!$flexible_columns) { | |||||
| break; | |||||
| } | |||||
| // NOTE: This is very unsophisticated, and just shortcuts us to a | |||||
| // reasonable result when only one column is flexible. | |||||
| foreach ($flexible_columns as $flexible_key) { | |||||
| $column = $columns[$flexible_key]; | |||||
| $need_width = ($total_width - $available_width); | |||||
| $old_width = $this->displayWidths[$flexible_key]; | |||||
| $new_width = ($old_width - $need_width); | |||||
| $new_width = max($new_width, $column->getMinimumWidth()); | |||||
| $this->displayWidths[$flexible_key] = $new_width; | |||||
| $flexible_columns = array(); | |||||
| break; | |||||
| } | |||||
| } | |||||
| } | |||||
| return $this->displayWidths[$display_key]; | |||||
| } | } | ||||
| public function getColumn($key) { | public function getColumn($key) { | ||||
| if (!isset($this->columns[$key])) { | if (!isset($this->columns[$key])) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht( | pht( | ||||
| 'Grid has no column "%s".', | 'Grid has no column "%s".', | ||||
| $key)); | $key)); | ||||
| ▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | private function padContentLineToWidth( | ||||
| return $result; | return $result; | ||||
| } | } | ||||
| private function truncateContentLineToWidth( | private function truncateContentLineToWidth( | ||||
| $line, | $line, | ||||
| $src_width, | $src_width, | ||||
| $dst_width, | $dst_width, | ||||
| $alignment) { | $alignment) { | ||||
| return $line; | |||||
| $line = phutil_string_cast($line); | |||||
| return id(new PhutilUTF8StringTruncator()) | |||||
| ->setMaximumGlyphs($dst_width) | |||||
| ->truncateString($line); | |||||
| } | } | ||||
| } | } | ||||