Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13987011
D9533.id22875.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D9533.id22875.diff
View Options
diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -135,6 +135,7 @@
'PhutilConsoleServerChannel' => 'console/PhutilConsoleServerChannel.php',
'PhutilConsoleStdinNotInteractiveException' => 'console/PhutilConsoleStdinNotInteractiveException.php',
'PhutilConsoleSyntaxHighlighter' => 'markup/syntax/highlighter/PhutilConsoleSyntaxHighlighter.php',
+ 'PhutilConsoleTable' => 'console/PhutilConsoleTable.php',
'PhutilConsoleWrapTestCase' => 'console/__tests__/PhutilConsoleWrapTestCase.php',
'PhutilContextFreeGrammar' => 'grammar/PhutilContextFreeGrammar.php',
'PhutilDaemon' => 'daemon/PhutilDaemon.php',
@@ -417,6 +418,7 @@
'queryfx' => 'xsprintf/queryfx.php',
'queryfx_all' => 'xsprintf/queryfx.php',
'queryfx_one' => 'xsprintf/queryfx.php',
+ 'sprintf_escape' => 'utils/utils.php',
'urisprintf' => 'xsprintf/urisprintf.php',
'vcsprintf' => 'xsprintf/csprintf.php',
'vjsprintf' => 'xsprintf/jsprintf.php',
@@ -550,6 +552,7 @@
'PhutilConsoleProgressBar' => 'Phobject',
'PhutilConsoleServerChannel' => 'PhutilChannelChannel',
'PhutilConsoleStdinNotInteractiveException' => 'Exception',
+ 'PhutilConsoleTable' => 'Phobject',
'PhutilConsoleWrapTestCase' => 'PhutilTestCase',
'PhutilDefaultSyntaxHighlighterEngine' => 'PhutilSyntaxHighlighterEngine',
'PhutilDefaultSyntaxHighlighterEnginePygmentsFuture' => 'FutureProxy',
diff --git a/src/console/PhutilConsoleTable.php b/src/console/PhutilConsoleTable.php
new file mode 100644
--- /dev/null
+++ b/src/console/PhutilConsoleTable.php
@@ -0,0 +1,167 @@
+<?php
+
+/**
+ * Show a table in the console.
+ */
+final class PhutilConsoleTable extends Phobject {
+
+ private $columns = array();
+ private $data = array();
+ private $widths = array();
+ private $borders = false;
+ private $padding = 1;
+ private $console;
+
+
+/* -( Console )------------------------------------------------------------ */
+
+ private function getConsole() {
+ if ($this->console) {
+ return $this->console;
+ }
+ return PhutilConsole::getConsole();
+ }
+
+ public function setConsole(PhutilConsole $console) {
+ $this->console = $console;
+ return $this;
+ }
+
+
+/* -( Configuration )------------------------------------------------------ */
+
+ public function setDrawBorders($borders) {
+ $this->borders = $borders;
+ return $this;
+ }
+
+ public function setPadding($padding) {
+ $this->padding = $padding;
+ }
+
+
+/* -( Data )--------------------------------------------------------------- */
+
+ public function addColumn($key, array $column) {
+ PhutilTypeSpec::checkMap($column, array(
+ 'title' => 'string',
+ ));
+ $this->columns[$key] = $column;
+ return $this;
+ }
+
+ public function addData(array $data) {
+ $this->data[] = $data;
+
+ foreach ($data as $key => $value) {
+ $this->widths[$key] = max(
+ idx($this->widths, $key, 0),
+ phutil_utf8_console_strlen($value));
+ }
+
+ return $this;
+ }
+
+
+/* -( Drawing )------------------------------------------------------------ */
+
+ public function draw() {
+ $this->drawHeader();
+ $this->drawBody();
+ $this->drawFooter();
+ }
+
+ private function drawHeader() {
+ $console = $this->getConsole();
+
+ if ($this->borders) {
+ $columns = array();
+
+ foreach ($this->getColumns() as $column) {
+ $columns[] = str_repeat('=', $this->getWidth($column));
+ }
+
+ $console->writeOut('+'.implode('+', $columns)."+\n");
+ }
+
+ $columns = array();
+ foreach ($this->columns as $key => $column) {
+ $columns[] =
+ idx($column, 'title', '').
+ str_repeat(' ', $this->widths[$key] - phutil_utf8_console_strlen($column['title']));
+ }
+
+ if ($this->borders) {
+ $separator = $this->getPadding().'|'.$this->getPadding();
+ $console->writeOut('|'.$this->getPadding().implode($separator, $columns).$this->getPadding()."|\n");
+ } else {
+ $separator = $this->getPadding();
+ $console->writeOut(implode($separator, $columns)."\n");
+ }
+
+ if ($this->borders) {
+ $columns = array();
+
+ foreach ($this->getColumns() as $column) {
+ $columns[] = str_repeat('=', $this->getWidth($column));
+ }
+
+ $console->writeOut('+'.implode('+', $columns)."+\n");
+ }
+ }
+
+ private function drawBody() {
+ $console = $this->getConsole();
+
+ foreach ($this->data as $data) {
+ $columns = array();
+
+ foreach ($this->getColumns() as $key) {
+ $cell = (string)idx($data, $key, '');
+
+ $columns[] =
+ $cell.
+ str_repeat(' ', $this->widths[$key] - phutil_utf8_console_strlen($cell));
+ }
+
+ if ($this->borders) {
+ $separator = $this->getPadding().'|'.$this->getPadding();
+ $console->writeOut('|'.$this->getPadding().implode($separator, $columns).$this->getPadding()."|\n");
+ } else {
+ $console->writeOut(implode($this->getPadding(), $columns)."\n");
+ }
+ }
+
+ //
+ }
+
+ private function drawFooter() {
+ $console = $this->getConsole();
+
+ if ($this->borders) {
+ $columns = array();
+
+ foreach ($this->getColumns() as $column) {
+ $columns[] = str_repeat('=', $this->getWidth($column));
+ }
+
+ $console->writeOut('+'.implode('+', $columns)."+\n");
+ }
+ }
+
+
+/* -( Drawing )------------------------------------------------------------ */
+
+ private function getColumns() {
+ return array_keys($this->columns);
+ }
+
+ private function getWidth($key) {
+ return idx($this->widths, $key) + 2 * $this->padding;
+ }
+
+ private function getPadding() {
+ return str_repeat(' ', $this->padding);
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Oct 22, 6:58 AM (3 w, 2 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6740731
Default Alt Text
D9533.id22875.diff (5 KB)
Attached To
Mode
D9533: Add a `PhutilConsoleTable` class for drawing tables.
Attached
Detach File
Event Timeline
Log In to Comment