use console_commandline for parameter parsing
[bdrem.git] / src / bdrem / Renderer / Console.php
index 2cd185f4ba43e4308fb0410a3f14d85e18df0a7f..b4ef8a80380efef95493bddb028d5f5b7c0245c1 100644 (file)
@@ -1,38 +1,93 @@
 <?php
 namespace bdrem;
 
-class Renderer_Console
+class Renderer_Console extends Renderer
 {
+    protected $httpContentType = 'text/plain; charset=utf-8';
+
+    /**
+     * Use ANSI color codes for output coloring
+     *
+     * @var boolean
+     */
+    public $ansi = false;
+
+    /**
+     * @var \Console_Color2
+     */
+    protected $cc;
+
     public function render($arEvents)
     {
-        $s  = "Days Age Name                                     Event                Date\n";
-        $s .= "---- --- ---------------------------------------- -------------------- ----------\n";
+        $this->loadConfig();
+        if ($this->ansi) {
+            $this->cc = new \Console_Color2();
+        }
+
+        $tbl = new \Console_Table(
+            CONSOLE_TABLE_ALIGN_LEFT,
+            array('sect' => '', 'rule' => '-', 'vert' => ''),
+            1, null, $this->ansi
+        );
+        $tbl->setAlign(0, CONSOLE_TABLE_ALIGN_RIGHT);
+        $tbl->setAlign(1, CONSOLE_TABLE_ALIGN_RIGHT);
+
+        $tbl->setHeaders(
+            $this->ansiWrap(
+                array('Days', 'Age', 'Name', 'Event', 'Date', 'Day'),
+                '%_%9'
+            )
+        );
+        $tbl->setBorderVisibility(
+            array(
+                'left'   => false,
+                'right'  => false,
+                'top'    => true,
+                'bottom' => false,
+                'inner'  => true,
+            )
+        );
+
         foreach ($arEvents as $event) {
-            $s .= sprintf(
-                "%3d %4s %s %s %s\n",
-                $event->days,
-                $event->age,
-                $this->str_pad($event->title, 40),
-                $this->str_pad($event->type, 20),
-                $event->date
+            $colorCode = null;
+            if ($event->days == 0) {
+                $colorCode = '%R';
+            }
+            $tbl->addRow(
+                $this->ansiWrap(
+                    array(
+                        $event->days,
+                        $event->age,
+                        wordwrap($event->title, 30, "\n", true),
+                        wordwrap($event->type, 20, "\n", true),
+                        $event->date,
+                        strftime('%a', strtotime($event->localDate))
+                    ),
+                    $colorCode
+                )
             );
         }
-        return $s;
+        return $tbl->getTable();
     }
 
-    public function str_pad(
-        $input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT
-    ) {
-        $l = mb_strlen($input, 'utf-8');
-        if ($l >= $pad_length) {
-            return $input;
+    protected function ansiWrap($data, $colorCode = null)
+    {
+        if (!$this->ansi || $colorCode === null) {
+            return $data;
+        }
+
+        foreach ($data as $k => &$value) {
+            $value = $this->cc->convert(
+                $colorCode . $value . '%n'
+            );
         }
+        return $data;
+    }
 
-        $p = str_repeat($pad_string, $pad_length - $l);
-        if ($pad_type == STR_PAD_RIGHT) {
-            return $input . $p;
-        } else {
-            return $p . $input;
+    protected function loadConfig()
+    {
+        if (isset($this->config->ansi)) {
+            $this->ansi = $this->config->ansi;
         }
     }
 }