forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableOutput.php
More file actions
138 lines (114 loc) · 3.66 KB
/
TableOutput.php
File metadata and controls
138 lines (114 loc) · 3.66 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace kriskbx\gtt\Output;
use Illuminate\Support\Collection;
use kriskbx\gtt\Estimation;
use kriskbx\gtt\Issue;
use kriskbx\gtt\Time;
use Symfony\Component\Console\Helper\Table as ConsoleTable;
use Symfony\Component\Console\Helper\TableSeparator;
class TableOutput extends AbstractOutput
{
/**
* @var ConsoleTable
*/
protected $table;
/**
* Render.
*
* @param Collection $issues
* @param string $title
* @param array $params
*
* @return mixed
*/
public function render(Collection $issues, $title, array $params)
{
$params = array_merge($params, [
'columns' => $this->columns,
'delimiter' => " | ",
'timesDelimiter' => "\n\n",
'break' => "\n",
'beforeHighlight' => "<fg=green>",
'afterHighlight' => "</>",
'beforeHeadline' => "<fg=blue>",
'afterHeadline' => "</>"
]);
$params['displayEstimationFunction'] = function (Estimation $estimation, array $params = []) {
return $this->estimationBar($estimation, $params);
};
$this->createTable();
$firstRow = true;
$issues->each(function (Issue $issue) use (&$firstRow, $params) {
if ( ! $firstRow) {
$this->table->addRow(new TableSeparator());
} else {
$firstRow = false;
}
$this->addToTotal($issue);
$this->table->addRow(explode($params['delimiter'], $issue->toString($params)));
});
// WE NEED SPACES
$this->output->writeln('');
$this->output->writeln('');
$this->output->writeln("<fg=black;bg=blue> {$title} </>");
$this->output->writeln('');
$this->total();
$this->table->render();
$this->output->writeln('');
}
/**
* Create table.
*
* @return ConsoleTable
*/
protected function createTable()
{
$this->table =
(new ConsoleTable($this->output))
->setHeaders(
collect($this->columns)
->map(function ($item) {
return camel_case($item);
})->toArray()
);
}
/**
* Get the estimation bar.
*
* @param Estimation $estimation
* @param array $params
*
* @return string
*/
protected function estimationBar(Estimation $estimation, $params = [])
{
$color = "green";
$percent = $estimation->getTimeEstimate() == 0 ? 1.1 : (int)$estimation->getTotalTimeSpent() / (int)$estimation->getTimeEstimate();
if ($percent > 1) {
$percent = 1;
$color = "red";
}
$string = $estimation->getHumanTotalTimeSpent() . " [";
for ($i = 1; $i < 11; $i++) {
if ($i <= ($percent * 10)) {
$string .= "<fg={$color}>=</>";
} else {
$string .= "<fg=black>-</>";
}
}
$string .= "] " . ($estimation->getHumanTimeEstimate() ?: 'N/A');
return $string;
}
/**
* Print out total stats.
*/
protected function total()
{
$string = "* <fg=blue>Total spent:</> " . Time::humanReadable($this->totalTime) . "\n";
$string .= "* <fg=blue>Total estimate:</> " . Time::humanReadable($this->totalEstimate) . "\n\n";
collect($this->totalTimeByUser)->each(function ($time, $user) use (&$string) {
$string .= "* <fg=blue>{$user}:</> " . Time::humanReadable($time) . "\n";
});
$this->output->write($string . "\n");
}
}