Skip to content

Commit

Permalink
Merge pull request #3 from RobertWesner/development
Browse files Browse the repository at this point in the history
Release v0.3.0
  • Loading branch information
RobertWesner authored Aug 24, 2024
2 parents f8e8e79 + de3e485 commit 9b1f3a7
Show file tree
Hide file tree
Showing 15 changed files with 420 additions and 67 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ AWDY

![](https://github.com/RobertWesner/awdy/actions/workflows/tests.yml/badge.svg)
![](https://raw.githubusercontent.com/RobertWesner/awdy/image-data/coverage.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](../../raw/main/LICENSE.txt)
![](https://img.shields.io/github/v/release/RobertWesner/awdy)
[![License: MIT](https://img.shields.io/github/license/RobertWesner/awdy)](../../raw/main/LICENSE.txt)

</div>

Expand Down Expand Up @@ -108,5 +109,3 @@ Templates are easy to create, have a look at the [official ones](src/Template/Te
### Always with dynamic size

![](readme/3.gif)

## Templates
Binary file modified readme/1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified readme/2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified readme/3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions src/AnsiEscape.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@

namespace RobertWesner\AWDY;

use PHPUnit\Framework\Attributes\CodeCoverageIgnore;

/**
* https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
*
* @codeCoverageIgnore
*/
#[CodeCoverageIgnore]
final class AnsiEscape
{
private const SEQUENCE = "\33[";
Expand Down
8 changes: 3 additions & 5 deletions src/Template/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RobertWesner\AWDY\Template;

use PHPUnit\Framework\Attributes\CodeCoverageIgnore;
// TODO: allow ansi escapes for border parts

class Border
{
Expand Down Expand Up @@ -101,23 +101,20 @@ private function getFirstLineWidth(string $string): int
return strlen(explode(PHP_EOL, $string, 2)[0]);
}

#[CodeCoverageIgnore]
public function horizontal(string $horizontal): static
{
$this->horizontal = $horizontal;

return $this;
}

#[CodeCoverageIgnore]
public function vertical(string $vertical): static
{
$this->vertical = $vertical;

return $this;
}

#[CodeCoverageIgnore]
public function corners(string $topLeft, string $topRight, string $bottomLeft, string $bottomRight): static
{
$this->cornerTopLeft = $topLeft;
Expand All @@ -141,8 +138,9 @@ public function connectFacing(string $left, string $right, string $top, string $

/**
* @param Connection[] $connections
*
* @codeCoverageIgnore
*/
#[CodeCoverageIgnore]
public function connections(array $connections): static
{
$this->connections = $connections;
Expand Down
9 changes: 8 additions & 1 deletion src/Template/Templates/DefaultTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use RobertWesner\AWDY\Template\Facing;
use RobertWesner\AWDY\Template\TemplateInterface;

// TODO: rework this to something actually nice

class DefaultTemplate implements TemplateInterface
{
private Area $logArea;
Expand Down Expand Up @@ -40,7 +42,12 @@ public function __construct()
$buffer->draw(1, 1, '|', AnsiEscape::fg(8));
$progressBarWidth = $buffer->getWidth() - 4;
$progress = $progressBarWidth * $this->progress;
$buffer->draw(2, 1, str_repeat(' ', (int)$progress), AnsiEscape::bg(2));
$buffer->draw(
2,
1,
str_repeat('#', (int)$progress),
AnsiEscape::bg(2) . AnsiEscape::fg(2),
);
$buffer->draw(-2, 1, '|', AnsiEscape::fg(8));
});
}
Expand Down
9 changes: 7 additions & 2 deletions src/Template/Templates/SimpleTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ public function __construct()
$buffer->draw(0, 0, $counter);
}

$buffer->draw(strlen($counter) + 1, 0, '[');
$progressX = 0;
if ($counter !== '') {
$progressX = strlen($counter) + 1;
}

$buffer->draw($progressX, 0, '[');
$progressBarWidth = $buffer->getWidth() - strlen($counter) - 2;
$progress = $progressBarWidth * $this->progress;
$buffer->draw(strlen($counter) + 2, 0, str_repeat('=', (int)$progress) . ($this->progress < 1 ? '>' : ''));
$buffer->draw($progressX + 1, 0, str_repeat('=', (int)$progress) . ($this->progress < 1 ? '>' : ''));
$buffer->draw(-1, 0, ']');
});
}
Expand Down
104 changes: 104 additions & 0 deletions tests/AWDYTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace RobertWesner\AWDY\Tests;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use RobertWesner\AWDY\AWDY;
use RobertWesner\AWDY\Template\Area;
use RobertWesner\AWDY\Template\Border;
use RobertWesner\AWDY\Template\Buffer;
use RobertWesner\AWDY\Template\BufferLogger;
use RobertWesner\AWDY\Template\Connection;
use RobertWesner\AWDY\Template\Templates\SimpleTemplate;

#[CoversClass(AWDY::class)]
#[UsesClass(Area::class)]
#[UsesClass(Border::class)]
#[UsesClass(Buffer::class)]
#[UsesClass(BufferLogger::class)]
#[UsesClass(Connection::class)]
final class AWDYTest extends BaseTest
{
public function test(): void
{
ob_start();
AWDY::setUp(new SimpleTemplate(), 56, 18);
self::assertSame(<<<EOF
.------------------------------------------------------.
| [> ] |
+------------------------------------------------------+
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
'------------------------------------------------------'
EOF, $this->renderAwdyOutput(ob_get_clean()));

// this does not render the entire thing, only the changed area
ob_start();
AWDY::printf('test');
self::assertSame(<<<EOF
test
EOF, $this->renderAwdyOutput(ob_get_clean()));

ob_start();
AWDY::progress(1 / 100, 1, 100);
self::assertSame(<<<EOF
1/100 [> ]
EOF, $this->renderAwdyOutput(ob_get_clean()));

ob_start();
AWDY::progress(33 / 100, 33, 100);
self::assertSame(<<<EOF
33/100 [==============> ]
EOF, $this->renderAwdyOutput(ob_get_clean()));

ob_start();
AWDY::progress(75 / 100, 75, 100);
self::assertSame(<<<EOF
75/100 [================================> ]
EOF, $this->renderAwdyOutput(ob_get_clean()));

ob_start();
AWDY::progress(99 / 100, 99, 100);
self::assertSame(<<<EOF
99/100 [==========================================]
EOF, $this->renderAwdyOutput(ob_get_clean()));

ob_start();
AWDY::progress(1, 100, 100);
self::assertSame(<<<EOF
100/100 [==========================================]
EOF, $this->renderAwdyOutput(ob_get_clean()));
}
}
61 changes: 61 additions & 0 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace RobertWesner\AWDY\Tests;

use PHPUnit\Framework\TestCase;

class BaseTest extends TestCase
{
protected function stripAnsiEscapes(string $value): string
{
return preg_replace('/\\033\[[^A-Za-z]*[A-Za-z]/', '', $value);
}

protected function renderAwdyOutput(string $value): string
{
$segments = preg_split('/(?=\\x1b\[\d+;\d+f)/', $value);

$result = '';
foreach ($segments as &$segment) {
if (
str_starts_with($segment, "\033")
&& preg_match(
'/\\x1b\[(?<y>\d+);(?<x>\d+)f(?<text>.*)/',
$segment,
$matches,
)
) {
$segment = [
'x' => (int)$matches['x'] - 1,
'y' => (int)$matches['y'] - 1,
'text' => $this->stripAnsiEscapes($matches['text']),
];
} else {
$segment = [
'x' => 0,
'y' => 0,
'text' => $this->stripAnsiEscapes($segment),
];
}
}

foreach ($segments as ['x' => $x, 'y' => $y, 'text' => $text]) {
$explodedResult = explode(PHP_EOL, $result);

foreach (explode(PHP_EOL, $text) as $i => $line) {
$explodedResult[$y + $i] = substr_replace(
$explodedResult[$y + $i] ?? str_repeat(' ', $x),
$line,
$x,
strlen($line),
);
}

$result = implode(PHP_EOL, $explodedResult);
}

return $result;
}
}
67 changes: 67 additions & 0 deletions tests/Template/AreaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace RobertWesner\AWDY\Tests\Template;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use RobertWesner\AWDY\AnsiEscape;
use RobertWesner\AWDY\Template\Area;
use RobertWesner\AWDY\Template\Buffer;
use RobertWesner\AWDY\Tests\BaseTest;

#[CoversClass(Area::class)]
#[UsesClass(Buffer::class)]
final class AreaTest extends BaseTest
{
private string $changingValue = 'Hello';

public function test(): void
{
$area = Area::create(5, 2, -6, -3, function (Buffer $buffer) {
$buffer->draw(0, 0, 'A');
$buffer->draw(1, 1, $this->changingValue);
$buffer->draw(-1, 0, 'B');
$buffer->draw(0, -1, 'C');
$buffer->draw(-1, -1, 'D');
});

// dirty by default
ob_start();
$area->render(20, 10);
self::assertSame(
sprintf(
'%sA B%s Hello %s %s %s %sC D',
AnsiEscape::moveTo(6, 3),
AnsiEscape::moveTo(6, 4),
AnsiEscape::moveTo(6, 5),
AnsiEscape::moveTo(6, 6),
AnsiEscape::moveTo(6, 7),
AnsiEscape::moveTo(6, 8),
),
ob_get_clean(),
);

// should not render since it is not dirty
ob_start();
$area->render(80, 32);
self::assertSame('', ob_get_clean());

// mark area as dirty and re-render it
$this->changingValue = 'World';
$area->dirty();
ob_start();
$area->render(20, 10);
self::assertSame(
sprintf(
'%sA B%s World %s %s %s %sC D',
AnsiEscape::moveTo(6, 3),
AnsiEscape::moveTo(6, 4),
AnsiEscape::moveTo(6, 5),
AnsiEscape::moveTo(6, 6),
AnsiEscape::moveTo(6, 7),
AnsiEscape::moveTo(6, 8),
),
ob_get_clean(),
);
}
}
Loading

0 comments on commit 9b1f3a7

Please sign in to comment.