Changeset View
Changeset View
Standalone View
Standalone View
src/ref/ArcanistRefView.php
- This file was moved from src/ref/ArcanistDisplayRef.php.
| <?php | <?php | ||||
| final class ArcanistDisplayRef | final class ArcanistRefView | ||||
| extends Phobject | extends Phobject | ||||
| implements | implements | ||||
| ArcanistTerminalStringInterface { | ArcanistTerminalStringInterface { | ||||
| private $objectName; | |||||
| private $title; | |||||
| private $ref; | private $ref; | ||||
| private $uri; | private $uri; | ||||
| private $lines = array(); | private $lines = array(); | ||||
| private $children = array(); | |||||
| public function setRef(ArcanistRef $ref) { | public function setRef(ArcanistRef $ref) { | ||||
| $this->ref = $ref; | $this->ref = $ref; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getRef() { | public function getRef() { | ||||
| return $this->ref; | return $this->ref; | ||||
| } | } | ||||
| public function setObjectName($object_name) { | |||||
| $this->objectName = $object_name; | |||||
| return $this; | |||||
| } | |||||
| public function getObjectName() { | |||||
| return $this->objectName; | |||||
| } | |||||
| public function setTitle($title) { | |||||
| $this->title = $title; | |||||
| return $this; | |||||
| } | |||||
| public function getTitle() { | |||||
| return $this->title; | |||||
| } | |||||
| public function setURI($uri) { | public function setURI($uri) { | ||||
| $this->uri = $uri; | $this->uri = $uri; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getURI() { | public function getURI() { | ||||
| return $this->uri; | return $this->uri; | ||||
| } | } | ||||
| public function addChild(ArcanistRefView $view) { | |||||
| $this->children[] = $view; | |||||
| return $this; | |||||
| } | |||||
| private function getChildren() { | |||||
| return $this->children; | |||||
| } | |||||
| public function appendLine($line) { | public function appendLine($line) { | ||||
| $this->lines[] = $line; | $this->lines[] = $line; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function newTerminalString() { | public function newTerminalString() { | ||||
| return $this->newLines(0); | |||||
| } | |||||
| private function newLines($indent) { | |||||
| $ref = $this->getRef(); | $ref = $this->getRef(); | ||||
| if ($ref instanceof ArcanistDisplayRefInterface) { | $object_name = $this->getObjectName(); | ||||
| $object_name = $ref->getDisplayRefObjectName(); | $title = $this->getTitle(); | ||||
| $title = $ref->getDisplayRefTitle(); | |||||
| } else { | |||||
| $object_name = null; | |||||
| $title = $ref->getRefDisplayName(); | |||||
| } | |||||
| if ($object_name !== null) { | if ($object_name !== null) { | ||||
| $reserve_width = phutil_utf8_console_strlen($object_name) + 1; | $reserve_width = phutil_utf8_console_strlen($object_name) + 1; | ||||
| } else { | } else { | ||||
| $reserve_width = 0; | $reserve_width = 0; | ||||
| } | } | ||||
| if ($indent) { | |||||
| $indent_text = str_repeat(' ', $indent); | |||||
| } else { | |||||
| $indent_text = ''; | |||||
| } | |||||
| $indent_width = strlen($indent_text); | |||||
| $marker_width = 6; | $marker_width = 6; | ||||
| $display_width = phutil_console_get_terminal_width(); | $display_width = phutil_console_get_terminal_width(); | ||||
| $usable_width = ($display_width - $marker_width - $reserve_width); | $usable_width = ($display_width - $marker_width - $reserve_width); | ||||
| $usable_width = ($usable_width - $indent_width); | |||||
| // If the terminal is extremely narrow, don't degrade so much that the | // If the terminal is extremely narrow, don't degrade so much that the | ||||
| // output is completely unusable. | // output is completely unusable. | ||||
| $usable_width = max($usable_width, 16); | $usable_width = max($usable_width, 16); | ||||
| // TODO: This should truncate based on console display width, not | // TODO: This should truncate based on console display width, not | ||||
| // glyphs, but there's currently no "setMaximumConsoleCharacterWidth()". | // glyphs, but there's currently no "setMaximumConsoleCharacterWidth()". | ||||
| $title = id(new PhutilUTF8StringTruncator()) | $title = id(new PhutilUTF8StringTruncator()) | ||||
| ->setMaximumGlyphs($usable_width) | ->setMaximumGlyphs($usable_width) | ||||
| ->truncateString($title); | ->truncateString($title); | ||||
| if ($object_name !== null) { | if ($object_name !== null) { | ||||
| if (strlen($title)) { | if (strlen($title)) { | ||||
| $display_text = tsprintf('**%s** %s', $object_name, $title); | $display_text = tsprintf('**%s** %s', $object_name, $title); | ||||
| } else { | } else { | ||||
| $display_text = tsprintf('**%s**', $object_name); | $display_text = tsprintf('**%s**', $object_name); | ||||
| } | } | ||||
| } else { | } else { | ||||
| $display_text = $title; | $display_text = $title; | ||||
| } | } | ||||
| $ref = $this->getRef(); | |||||
| $output = array(); | $output = array(); | ||||
| $output[] = tsprintf( | $output[] = tsprintf( | ||||
| "<bg:cyan>** * **</bg> %s\n", | "<bg:cyan>** * **</bg> %s%s\n", | ||||
| $indent_text, | |||||
| $display_text); | $display_text); | ||||
| $uri = $this->getURI(); | $uri = $this->getURI(); | ||||
| if ($uri !== null) { | if ($uri !== null) { | ||||
| $output[] = tsprintf( | $output[] = tsprintf( | ||||
| "<bg:cyan>** :// **</bg> __%s__\n", | "<bg:cyan>** :// **</bg> %s__%s__\n", | ||||
| $indent_text, | |||||
| $uri); | $uri); | ||||
| } | } | ||||
| foreach ($this->lines as $line) { | foreach ($this->lines as $line) { | ||||
| $output[] = tsprintf(" %s\n", $line); | $output[] = tsprintf( | ||||
| " %s%s\n", | |||||
| $indent_text, | |||||
| $line); | |||||
| } | |||||
| foreach ($this->getChildren() as $child) { | |||||
| foreach ($child->newLines($indent + 1) as $line) { | |||||
| $output[] = $line; | |||||
| } | |||||
| } | } | ||||
| return $output; | return $output; | ||||
| } | } | ||||
| } | } | ||||