Page MenuHomePhabricator

D9533.id22853.diff
No OneTemporary

D9533.id22853.diff

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,141 @@
+<?php
+
+/**
+ * Show a table in the console.
+ */
+final class PhutilConsoleTable extends Phobject {
+
+ private $header = array();
+ private $data = array();
+ private $widths = array();
+ private $borders = false;
+ private $console;
+
+ public function setConsole(PhutilConsole $console) {
+ $this->console = $console;
+ return $this;
+ }
+
+ private function getConsole() {
+ if ($this->console) {
+ return $this->console;
+ }
+ return PhutilConsole::getConsole();
+ }
+
+ public function setHeader(array $header) {
+ $this->header = $header;
+ }
+
+ public function addData(array $data) {
+ $this->data[] = $data;
+
+ foreach ($data as $key => $value) {
+ $this->widths[$key] = max(idx($this->widths, $key, 0), strlen($value));
+ }
+ }
+
+ public function setDrawBorders($borders) {
+ $this->borders = $borders;
+ }
+
+ public function draw() {
+ $console = $this->getConsole();
+
+ $console->writeOut($this->getHeader());
+ $console->writeOut($this->getBody());
+ $console->writeOut($this->getFooter());
+ }
+
+
+
+ private function getHeader() {
+ $argv = array_merge(
+ array($this->getFormatString()),
+ $this->header);
+
+ $header = call_user_func_array('sprintf', $argv);
+
+ if ($this->borders) {
+ $header = str_repeat('=', $this->getTotalWidth())."\n".
+ $header.
+ str_repeat('-', $this->getTotalWidth())."\n";
+ }
+
+ return $header;
+ }
+
+ private function getBody() {
+ $format = $this->getFormatString();
+ $output = '';
+
+ foreach ($this->data as $row) {
+ $argv = array($format);
+
+ foreach ($this->header as $column) {
+ $argv[] = idx($row, $column, '');
+ }
+
+ $output .= call_user_func_array('sprintf', $argv);
+ }
+
+ return $output;
+ }
+
+ private function getFooter() {
+ $footer = '';
+
+ if ($this->borders) {
+ $footer .= str_repeat('=', $this->getTotalWidth())."\n";
+ }
+
+ return $footer;
+ }
+
+ private function getFormatString() {
+ $format = '';
+ $widths = $this->getWidths();
+
+ foreach ($this->header as $column) {
+ if ($format !== '') {
+ $format .= ' ';
+ }
+
+ $format .= $this->getColumnSeparator();
+ $format .= sprintf('%%%us', idx($widths, $column));
+ }
+
+ $format .= $this->getColumnTrailingSeparator();
+
+ return $format."\n";
+ }
+
+ private function getWidths() {
+ $widths = array();
+ $separator = $this->getColumnSeparator();
+
+ foreach ($this->widths as $width) {
+ $widths[] = $width + strlen($separator);
+ }
+
+ return $widths;
+ }
+
+ private function getTotalWidth() {
+ return array_sum($this->getWidths()) +
+ strlen($this->getColumnTrailingSeparator()) + 1;
+ }
+
+ private function getColumnSeparator() {
+ if (!$this->borders) {
+ return '';
+ } else {
+ return '| ';
+ }
+ }
+
+ private function getColumnTrailingSeparator() {
+ return strrev($this->getColumnSeparator());
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Sun, Oct 20, 11:30 AM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6719655
Default Alt Text
D9533.id22853.diff (4 KB)

Event Timeline