Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation and add tests badge #2

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml → .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: PHP Composer
name: tests

on:
push:
Expand Down
48 changes: 44 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ AWDY

<div align="center">

![coverage](https://raw.githubusercontent.com/RobertWesner/awdy/image-data/coverage.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](../../raw/main/LICENSE.txt)
![](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)

</div>

Expand Down Expand Up @@ -53,6 +54,47 @@ while (true) {
}
```

## Templates

### JustProgress

```
00:00:23 418/1337 [==========> ] Memory[00.12%]: 15659904/134217728
```

### Simple

```
.----------------------------------------------------------.
| 13370/13370 [==========================================] |
+----------------------------------------------------------+
| 0 is your lucky number! |
| 777 is your lucky number! |
| 1554 is your lucky number! |
| 2331 is your lucky number! |
| 3108 is your lucky number! |
| 3885 is your lucky number! |
| 4662 is your lucky number! |
| 5439 is your lucky number! |
| 6216 is your lucky number! |
| 6993 is your lucky number! |
| 7770 is your lucky number! |
| 8547 is your lucky number! |
| 9324 is your lucky number! |
| 10101 is your lucky number! |
| 10878 is your lucky number! |
| 11655 is your lucky number! |
| 12432 is your lucky number! |
| 13209 is your lucky number! |
'----------------------------------------------------------'
```

## Create your own template

Templates are easy to create, have a look at the [official ones](src/Template/Templates).

[//]: # (I should create a wiki page for templating)

## Demo

### Keeping it simple
Expand All @@ -68,5 +110,3 @@ while (true) {
![](readme/3.gif)

## Templates

Templates are easy to create, have a look at the [official ones](src/Template/Templates).
40 changes: 40 additions & 0 deletions demo/progress-only.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use RobertWesner\AWDY\AWDY;
use RobertWesner\AWDY\Template\Templates\JustProgressTemplate;

require __DIR__ . '/../vendor/autoload.php';

const LIMIT = 1337;
const CLEAR_UP_MEM_AFTER = 10;

AWDY::setUp(new JustProgressTemplate());

$memorySpam = [];

$i = 0;
while (true) {
if ($i >= LIMIT) {
break;
}

$memorySpam[] = str_repeat('fill memory up real good', rand(1, 100000));

usleep(rand(1000, 100000));

$i++;

if (($i % CLEAR_UP_MEM_AFTER) === 0) {
$clean = rand(1, CLEAR_UP_MEM_AFTER * 2);
for ($memCounter = 0; $memCounter < $clean; $memCounter++) {
if (isset($memorySpam[$memCounter])) {
unset($memorySpam[$memCounter]);
}
}
$memorySpam = array_values($memorySpam);

gc_collect_cycles();
}

AWDY::progress($i / LIMIT, $i, LIMIT);
}
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<arg name="extensions" value="php"/>

<rule ref="PSR12" />
<rule ref="Generic.PHP.RequireStrictTypes"/>

<file>src/</file>
<file>tests/</file>
Expand Down
100 changes: 100 additions & 0 deletions src/Template/Templates/JustProgressTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace RobertWesner\AWDY\Template\Templates;

use RobertWesner\AWDY\Template\Area;
use RobertWesner\AWDY\Template\Border;
use RobertWesner\AWDY\Template\Buffer;
use RobertWesner\AWDY\Template\TemplateInterface;

/**
* No logging. Instead, has time elapsed and memory usage.
*/
class JustProgressTemplate implements TemplateInterface
{
private Area $progressArea;

private int $beginTime;

private float $progress = 0;
private int $current = 0;
private int $total = 0;

public function __construct(int $progressBarWidth = 32)
{
$this->beginTime = time();

$memoryLimit = 0;
$limit = ini_get('memory_limit');
if ($limit >= 0) {
$units = ['K' => 1, 'M' => 2, 'G' => 3];
$unit = strtoupper(substr($limit, -1));
$memoryLimit = (int)substr($limit, 0, -1) * pow(1024, $units[$unit] ?? 0);
}

$this->progressArea = Area::create(0, 0, -1, 0, function (
Buffer $buffer,
) use (
$progressBarWidth,
$memoryLimit,
) {
$timePassed = date('H:i:s', time() - $this->beginTime);
$buffer->draw(0, 0, $timePassed);

$counter = '';
if ($this->total !== 0) {
$counter = str_pad((string)$this->current, strlen((string)$this->total), ' ', STR_PAD_LEFT)
. '/' . $this->total;
$buffer->draw(strlen($timePassed) + 1, 0, $counter);
}

$progressX = strlen($timePassed) + 1;
if ($counter !== '') {
$progressX += strlen($counter) + 1;
}

$buffer->draw($progressX, 0, '[');
$progress = $progressBarWidth * $this->progress;
$buffer->draw($progressX + 1, 0, str_repeat('=', (int)$progress) . ($this->progress < 1 ? '>' : ''));
$buffer->draw($progressX + $progressBarWidth + 2, 0, ']');

$memoryInformation = '';
if ($memoryLimit > 0) {
$memoryUsage = memory_get_usage();
$memoryInformation = sprintf(
'Memory[%05.2f%%]: %s/%d',
$memoryUsage / $memoryLimit,
str_pad((string)$memoryUsage, strlen((string)$memoryLimit), ' ', STR_PAD_LEFT),
$memoryLimit,
);
}
$buffer->draw($progressX + $progressBarWidth + 4, 0, $memoryInformation);
});
}

public function getBorder(): Border
{
return Border::create();
}

public function getAreas(): array
{
return [
$this->progressArea,
];
}

public function handleEcho(string $echo): void
{
}

public function handleProgress(float $progress, int $current = 0, int $total = 0): void
{
$this->progress = $progress;
$this->current = $current;
$this->total = $total;
$this->progressArea->dirty();
}
}
2 changes: 2 additions & 0 deletions tests/Template/BorderTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace RobertWesner\AWDY\Tests\Template;

use PHPUnit\Framework\Attributes\CoversClass;
Expand Down
63 changes: 63 additions & 0 deletions tests/Template/BufferLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace RobertWesner\AWDY\Tests\Template;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use RobertWesner\AWDY\Template\AbsoluteCoordinateTrait;
use RobertWesner\AWDY\Template\Buffer;
use RobertWesner\AWDY\Template\BufferLogger;

#[CoversClass(BufferLogger::class)]
#[UsesClass(AbsoluteCoordinateTrait::class)]
#[UsesClass(Buffer::class)]
class BufferLoggerTest extends TestCase
{
private function getRendered(BufferLogger $logger): string
{
$buffer = new Buffer(12, 4);
$logger->renderTo($buffer);

return (string)$buffer;
}

public function test(): void
{
$logger = new BufferLogger();
self::assertSame(<<<EOF




EOF, $this->getRendered($logger));

$logger->append('Test ');
self::assertSame(<<<EOF
Test



EOF, $this->getRendered($logger));

$logger->append('1');
$logger->append('33');
$logger->append('.7' . PHP_EOL);
self::assertSame(<<<EOF
Test 133.7



EOF, $this->getRendered($logger));

$logger->append('This is a very long text and should break into a new line.');
self::assertSame(<<<EOF
ry long text
and should
break into a
new line.
EOF, $this->getRendered($logger));
}
}
2 changes: 2 additions & 0 deletions tests/Template/BufferTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace RobertWesner\AWDY\Tests\Template;

use PHPUnit\Framework\Attributes\CoversClass;
Expand Down
41 changes: 41 additions & 0 deletions tests/Template/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace RobertWesner\AWDY\Tests\Template;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use RobertWesner\AWDY\Template\Connection;
use RobertWesner\AWDY\Template\Facing;

#[CoversClass(Connection::class)]
class ConnectionTest extends TestCase
{
public function test(): void
{
$connection = Connection::horizontal()
->begin(0, 3, Facing::RIGHT)
->end(-5, 3, Facing::LEFT);

self::assertSame(Connection::TYPE_HORIZONTAL, $connection->type);
self::assertSame(0, $connection->beginX);
self::assertSame(3, $connection->beginY);
self::assertSame(Facing::RIGHT, $connection->beginFacing);
self::assertSame(-5, $connection->endX);
self::assertSame(3, $connection->endY);
self::assertSame(Facing::LEFT, $connection->endFacing);

$connection = Connection::vertical()
->begin(8, 10, Facing::BOTTOM)
->end(8, 30, Facing::ALL);

self::assertSame(Connection::TYPE_VERTICAL, $connection->type);
self::assertSame(8, $connection->beginX);
self::assertSame(10, $connection->beginY);
self::assertSame(Facing::BOTTOM, $connection->beginFacing);
self::assertSame(8, $connection->endX);
self::assertSame(30, $connection->endY);
self::assertSame(Facing::ALL, $connection->endFacing);
}
}
Loading