Add ansi color codes to console output
authorChristian Weiske <cweiske@cweiske.de>
Mon, 27 Jan 2014 05:47:02 +0000 (06:47 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 27 Jan 2014 05:47:02 +0000 (06:47 +0100)
src/bdrem/Renderer/Console.php
src/bdrem/WebText.php

index 01d6eb2e82bc0475034aaccd433cada6a9969cf7..195f53d00f6f81a036db839b739172cdc802c96b 100644 (file)
@@ -3,32 +3,82 @@ namespace bdrem;
 
 class Renderer_Console
 {
 
 class Renderer_Console
 {
+    /**
+     * Use ANSI color codes for output coloring
+     *
+     * @var boolean
+     */
+    public $ansi = true;
+
+    /**
+     * @var \Console_Color2
+     */
+    protected $cc;
+
     public function render($arEvents)
     {
     public function render($arEvents)
     {
+        if ($this->ansi) {
+            $this->cc = new \Console_Color2();
+        }
+
         $tbl = new \Console_Table(
             CONSOLE_TABLE_ALIGN_LEFT,
         $tbl = new \Console_Table(
             CONSOLE_TABLE_ALIGN_LEFT,
-            array('sect' => '', 'rule' => '-', 'vert' => '')
+            array('sect' => '', 'rule' => '-', 'vert' => ''),
+            1, null, $this->ansi
         );
         $tbl->setAlign(0, CONSOLE_TABLE_ALIGN_RIGHT);
         $tbl->setAlign(1, CONSOLE_TABLE_ALIGN_RIGHT);
 
         $tbl->setHeaders(
         );
         $tbl->setAlign(0, CONSOLE_TABLE_ALIGN_RIGHT);
         $tbl->setAlign(1, CONSOLE_TABLE_ALIGN_RIGHT);
 
         $tbl->setHeaders(
-            array('Days', 'Age', 'Name', 'Event', 'Date', 'Day')
+            $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) {
         );
 
         foreach ($arEvents as $event) {
+            $colorCode = null;
+            if ($event->days == 0) {
+                $colorCode = '%R';
+            }
             $tbl->addRow(
             $tbl->addRow(
-                array(
-                    $event->days,
-                    $event->age,
-                    wordwrap($event->title, 30, "\n", true),
-                    wordwrap($event->type, 20, "\n", true),
-                    $event->date,
-                    strftime('%a', strtotime($event->localDate))
+                $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 $tbl->getTable();
     }
                 )
             );
         }
         return $tbl->getTable();
     }
+
+    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;
+    }
 }
 ?>
 }
 ?>
index aaace51145f6cac202e215961f0810137ecb7b73..fe2e5dcdc1f04b879c38e83c976aa1939afb3eab 100644 (file)
@@ -7,6 +7,7 @@ class WebText extends Web
     {
         header('Content-type: text/plain; charset=utf-8');
         $r = new Renderer_Console();
     {
         header('Content-type: text/plain; charset=utf-8');
         $r = new Renderer_Console();
+        $r->ansi = false;
         echo $r->render($arEvents);
     }
 }
         echo $r->render($arEvents);
     }
 }