• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 14544100392

12 Apr 2025 03:39PM UTC coverage: 81.217% (+0.01%) from 81.206%
14544100392

push

github

web-flow
feat(console): add option to use terminal width to render key/values (#1148)

17 of 18 new or added lines in 3 files covered. (94.44%)

11549 of 14220 relevant lines covered (81.22%)

106.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.55
/src/Tempest/Console/src/Components/Renderers/KeyValueRenderer.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Console\Components\Renderers;
6

7
use Stringable;
8
use Tempest\Support\Str\ImmutableString;
9

10
use function Tempest\root_path;
11
use function Tempest\Support\str;
12

13
final readonly class KeyValueRenderer
14
{
15
    public const int MAX_WIDTH = 125;
16

17
    public const int MIN_WIDTH = 3;
18

19
    public function render(Stringable|string $key, null|Stringable|string $value = null, bool $useAvailableWidth = false): string
89✔
20
    {
21
        $key = $this->cleanText($key)->append(' ');
89✔
22
        $value = $this->cleanText($value)->when(
89✔
23
            condition: fn ($s) => $s->stripTags()->length() !== 0,
89✔
24
            callback: fn ($s) => $s->prepend(' '),
89✔
25
        );
89✔
26

27
        $maximumWidth = $useAvailableWidth
89✔
28
            ? $this->getTerminalWidth()
1✔
29
            : self::MAX_WIDTH;
89✔
30

31
        $dotsWidth = ($maximumWidth - $key->stripTags()->length()) - $value->stripTags()->length();
89✔
32

33
        return str()
89✔
34
            ->append($key)
89✔
35
            ->append('<style="fg-gray dim">', str_repeat('.', max(self::MIN_WIDTH, min($dotsWidth, $maximumWidth))), '</style>')
89✔
36
            ->append($value)
89✔
37
            ->toString();
89✔
38
    }
39

40
    private function getTerminalWidth(): int
1✔
41
    {
42
        $width = shell_exec('tput cols');
1✔
43

44
        if ($width === false) {
1✔
NEW
45
            return self::MAX_WIDTH;
×
46
        }
47

48
        return ((int) $width) - 5;
1✔
49
    }
50

51
    private function cleanText(null|Stringable|string $text): ImmutableString
89✔
52
    {
53
        $text = new ImmutableString($text)->trim();
89✔
54

55
        if ($text->length() === 0) {
89✔
56
            return new ImmutableString();
1✔
57
        }
58

59
        return $text
89✔
60
            ->replaceRegex('/\[([^]]+)]/', '<em>[$1]</em>')
89✔
61
            ->when(fn ($s) => $s->endsWith(['.', '?', '!', ':']), fn ($s) => $s->replaceAt(-1, 1, ''))
89✔
62
            ->erase(root_path())
89✔
63
            ->trim();
89✔
64
    }
65
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc