Passed
Push — main ( 6f590f...2e3c65 )
by Nils
03:37
created

OutputHelper::renderTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Startwind\Forrest\Output;
4
5
use Startwind\Forrest\Command\Command;
6
use Startwind\Forrest\Repository\RepositoryCollection;
7
use Symfony\Component\Console\Helper\QuestionHelper;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\ConfirmationQuestion;
12
use Symfony\Component\Console\Question\Question;
13
14
class OutputHelper
15
{
16
    public static function renderTable(OutputInterface $output, array $headers, array $rows): void
17
    {
18
        $table = new Table($output);
19
        $table->setHeaders($headers);
20
        $table->setRows($rows);
21
        $table->render();
22
    }
23
24
    public static function renderHeader(OutputInterface $output): void
25
    {
26
        $output->writeln([
27
            '',
28
            '<options=bold>Forrest</> - Unifying the Command Line <fg=green>' . FORREST_VERSION . '</>',
29
            // '         by Nils Langner and contributors.',
30
            '',
31
        ]);
32
    }
33
34
    /**
35
     * @param Command[] $commands
36
     */
37
    public static function renderCommands(OutputInterface $output, InputInterface $input, QuestionHelper $questionHelper, array $commands, ?string $repoIdentifier = null, int $maxLength = -1, $askForCommand = false): bool|Command
38
    {
39
        $identifierMaxLength = $maxLength;
40
41
        foreach ($commands as $commandId => $command) {
42
            if ($repoIdentifier) {
43
                $commandIdentifier = RepositoryCollection::createUniqueCommandName($repoIdentifier, $command);
44
            } else {
45
                $commandIdentifier = $commandId;
46
            }
47
            $command->setFullyQualifiedIdentifier($commandIdentifier);
48
            if ($maxLength == -1) {
49
                $identifierMaxLength = max($identifierMaxLength, strlen($commandIdentifier));
50
            }
51
        }
52
53
        uasort($commands, function (Command $a, Command $b) {
54
            if($a->getScore() > -1) {
55
                return$b->getScore() <=> $a->getScore()  ;
56
            }
57
            return $a->getFullyQualifiedIdentifier() <=> $b->getFullyQualifiedIdentifier();
58
        });
59
60
        $number = 1;
61
        $numberPrefix = '';
62
63
        foreach ($commands as $commandId => $command) {
64
            if ($repoIdentifier) {
65
                $commandIdentifier = RepositoryCollection::createUniqueCommandName($repoIdentifier, $command);
66
            } else {
67
                $commandIdentifier = $commandId;
68
            }
69
70
            $spaces = str_repeat(' ', $identifierMaxLength - strlen($commandIdentifier) + 2);
71
72
            if ($askForCommand) {
73
                $numberPrefix = '  ' . $number;
74
                $number++;
75
            }
76
77
            $placeholder = '';
78
79
            if ($number < 100) {
80
                $placeholder = ' ';
81
            }
82
83
            if ($number < 11) {
84
                $placeholder = '  ';
85
            }
86
87
            $output->writeln($numberPrefix . '  <fg=green>' . $commandIdentifier . '</>' . $placeholder . $spaces . $command->getDescription());
88
        }
89
90
        if ($askForCommand) {
91
            return self::askForCommand($output, $input, $questionHelper, $commands);
92
        }
93
94
        return false;
95
    }
96
97
    /**
98
     * @param Command[] $commands
99
     */
100
    private static function askForCommand(OutputInterface $output, InputInterface $input, QuestionHelper $questionHelper, array $commands): bool|Command
101
    {
102
        $output->writeln('');
103
104
        if (count($commands) == 1) {
105
            $commandIdentifier = array_key_first($commands);
106
            $command = array_pop($commands);
107
            if (!$questionHelper->ask($input, $output, new ConfirmationQuestion('  Do you want to run "' . $commandIdentifier . '" (y/n)? ', false))) {
108
                return false;
109
            }
110
        } else {
111
            $commandNumber = 0;
112
            while ($commandNumber < 1 || $commandNumber > count($commands)) {
113
                $commandNumber = (int)$questionHelper->ask($input, $output, new Question('  Which command do you want to run [1-' . count($commands) . ']? '));
114
            }
115
116
            $commandIdentifier = array_keys($commands)[$commandNumber - 1];
117
            $command = $commands[$commandIdentifier];
118
        }
119
120
121
        $command->setFullyQualifiedIdentifier($commandIdentifier);
122
123
        return $command;
124
    }
125
}
126