aboutsummaryrefslogtreecommitdiff
path: root/src/bdrem/Renderer/Console.php
blob: 195f53d00f6f81a036db839b739172cdc802c96b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace bdrem;

class Renderer_Console
{
    /**
     * Use ANSI color codes for output coloring
     *
     * @var boolean
     */
    public $ansi = true;

    /**
     * @var \Console_Color2
     */
    protected $cc;

    public function render($arEvents)
    {
        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) {
            $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 $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;
    }
}
?>