From 307cda30735caf8f47aaca967ecf8410c05a16c6 Mon Sep 17 00:00:00 2001 From: Oleksii Date: Mon, 4 Dec 2023 14:34:44 +0200 Subject: [PATCH 1/7] ~mv reversal to template class --- src/Spinner/Core/Palette/A/ACharPalette.php | 13 +------- src/Spinner/Core/Palette/A/AStylePalette.php | 4 ++- src/Spinner/Core/Palette/PaletteTemplate.php | 14 +++++++++ src/Spinner/Core/Palette/Rainbow.php | 32 ++------------------ 4 files changed, 20 insertions(+), 43 deletions(-) diff --git a/src/Spinner/Core/Palette/A/ACharPalette.php b/src/Spinner/Core/Palette/A/ACharPalette.php index 537c6e93..609020c5 100644 --- a/src/Spinner/Core/Palette/A/ACharPalette.php +++ b/src/Spinner/Core/Palette/A/ACharPalette.php @@ -19,22 +19,11 @@ abstract class ACharPalette extends APalette implements ICharPalette protected function getEntries(?IPaletteMode $mode = null): Traversable { /** @var string $element */ - foreach ($this->getSequence() as $element) { + foreach ($this->sequence() as $element) { yield $this->createFrame($element); } } - protected function getSequence(): Traversable - { - $sequence = $this->sequence(); - - if ($this->options->getReversed()) { - $sequence = array_reverse(iterator_to_array($sequence)); - } - - yield from $sequence; - } - /** * @return Traversable */ diff --git a/src/Spinner/Core/Palette/A/AStylePalette.php b/src/Spinner/Core/Palette/A/AStylePalette.php index 80f78900..8bfc89d4 100644 --- a/src/Spinner/Core/Palette/A/AStylePalette.php +++ b/src/Spinner/Core/Palette/A/AStylePalette.php @@ -17,9 +17,11 @@ abstract class AStylePalette extends APalette implements IStylePalette { protected function getOptions(?IPaletteMode $mode = null): IPaletteOptions { + $interval = $this->options->getInterval() ?? $this->getInterval($this->extractStylingMode($mode)); + $this->options = new PaletteOptions( - interval: $this->options->getInterval() ?? $this->getInterval($this->extractStylingMode($mode)), + interval: $interval, reversed: $this->options->getReversed(), ); diff --git a/src/Spinner/Core/Palette/PaletteTemplate.php b/src/Spinner/Core/Palette/PaletteTemplate.php index ed24fc4a..79243583 100644 --- a/src/Spinner/Core/Palette/PaletteTemplate.php +++ b/src/Spinner/Core/Palette/PaletteTemplate.php @@ -29,6 +29,9 @@ public function __construct( */ public function getEntries(): Traversable { + if ($this->options->getReversed()) { + return $this->reversed($this->entries); + } return $this->entries; } @@ -36,4 +39,15 @@ public function getOptions(): IPaletteOptions { return $this->options; } + + private function reversed(Traversable $entries): Traversable + { + $sequence = []; + + foreach ($entries as $entry) { + $sequence[] = $entry; + } + + yield from array_reverse($sequence); + } } diff --git a/src/Spinner/Core/Palette/Rainbow.php b/src/Spinner/Core/Palette/Rainbow.php index c7b461c1..02dd82da 100644 --- a/src/Spinner/Core/Palette/Rainbow.php +++ b/src/Spinner/Core/Palette/Rainbow.php @@ -28,14 +28,8 @@ protected function ansi4StyleFrames(): Traversable */ protected function ansi8StyleFrames(): Traversable { - $sequence = $this->ansi8Sequence(); - - if ($this->options->getReversed()) { - $sequence = $this->reverseSequence($sequence); - } - /** @var string $item */ - foreach ($sequence as $item) { + foreach ($this->ansi8Sequence() as $item) { $element = sprintf("\e[38;5;%sm%%s\e[39m", $item); yield $this->createFrame($element); } @@ -79,35 +73,13 @@ private function ansi8Sequence(): Traversable ]; } - /** - * @param Traversable $sequence - * - * @return iterable - */ - private function reverseSequence(Traversable $sequence): iterable - { - /** @var array $s */ - $s = []; - /** @var string $item */ - foreach ($sequence as $item) { - $s[] = $item; - } - return array_reverse($s); - } - /** * @return Traversable */ protected function ansi24StyleFrames(): Traversable { - $sequence = $this->ansi24Sequence(); - - if ($this->options->getReversed()) { - $sequence = $this->reverseSequence($sequence); - } - /** @var string $item */ - foreach ($sequence as $item) { + foreach ($this->ansi24Sequence() as $item) { $element = sprintf("\e[38;2;%sm%%s\e[39m", $item); yield $this->createFrame($element); } From 8ee0620be4e754baa4b629639cdd060b40382e89 Mon Sep 17 00:00:00 2001 From: Oleksii Date: Mon, 4 Dec 2023 15:50:34 +0200 Subject: [PATCH 2/7] ~doc --- doc/usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/usage.md b/doc/usage.md index e5a765cd..64f8dfb0 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -24,7 +24,7 @@ $driver = \AlecRabbit\Spinner\Facade::getDriver(); while (true) { $driver->render(); - usleep(100000); + // do some work } ``` From a048e2af624b2fa09518d79690c3a89024389aba Mon Sep 17 00:00:00 2001 From: Oleksii Date: Mon, 4 Dec 2023 15:52:42 +0200 Subject: [PATCH 3/7] ~doc --- doc/usage.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/doc/usage.md b/doc/usage.md index 64f8dfb0..b57e2775 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -49,13 +49,7 @@ class Dots extends ACharPalette { protected function sequence(): Traversable { // note the width of each element - $a = [' ', '. ', '.. ', '...', ' ..', ' .', ' ']; - - if ($this->options->getReversed()) { - $a = array_reverse($a); - } - - yield from $a; + yield from [' ', '. ', '.. ', '...', ' ..', ' .', ' ']; } } ``` @@ -67,7 +61,7 @@ class Greeny extends AStylePalette { protected function ansi4StyleFrames(): Traversable { yield from [ - $this->createFrame("\e[92m%s\e[39m"), + $this->createFrame("\e[92m%s\e[39m"), // note the ANSI codes ]; } From 8a7d2bc1c158ab54b51fd2c51131e7d8226e762a Mon Sep 17 00:00:00 2001 From: Oleksii Date: Mon, 4 Dec 2023 16:24:53 +0200 Subject: [PATCH 4/7] ~ --- .tools/.report/.build/coverage.txt | 4 +-- .tools/.report/.deptrac/.deptrac.report | 2 +- .tools/.report/.phploc/.src.phploc.report | 32 +++++++++---------- .tools/.report/.phploc/.test.phploc.report | 26 +++++++-------- src/Spinner/Core/Palette/A/ACharPalette.php | 8 +++-- src/Spinner/Core/Palette/A/APalette.php | 1 + src/Spinner/Core/Palette/PaletteTemplate.php | 4 +++ .../Unit/Core/Palette/A/ACharPaletteTest.php | 28 ++++++++++++++++ .../A/Override/ACharPaletteOverride.php | 4 +-- 9 files changed, 72 insertions(+), 37 deletions(-) diff --git a/.tools/.report/.build/coverage.txt b/.tools/.report/.build/coverage.txt index 2481ad42..883202fd 100644 --- a/.tools/.report/.build/coverage.txt +++ b/.tools/.report/.build/coverage.txt @@ -2,6 +2,6 @@ Code Coverage Report Summary: Classes: 100.00% (207/207) - Methods: 100.00% (737/737) - Lines: 100.00% (3718/3718) + Methods: 100.00% (736/736) + Lines: 100.00% (3714/3714) diff --git a/.tools/.report/.deptrac/.deptrac.report b/.tools/.report/.deptrac/.deptrac.report index ec201040..a6748baa 100644 --- a/.tools/.report/.deptrac/.deptrac.report +++ b/.tools/.report/.deptrac/.deptrac.report @@ -5,7 +5,7 @@ Violations 0 Skipped violations 0 Uncovered 177 - Allowed 2543 + Allowed 2545 Warnings 0 Errors 0 -------------------- ------ diff --git a/.tools/.report/.phploc/.src.phploc.report b/.tools/.report/.phploc/.src.phploc.report index 0464e897..f4e3b4c2 100644 --- a/.tools/.report/.phploc/.src.phploc.report +++ b/.tools/.report/.phploc/.src.phploc.report @@ -4,11 +4,11 @@ Directories 77 Files 419 Size - Lines of Code (LOC) 13578 - Comment Lines of Code (CLOC) 739 (5.44%) - Non-Comment Lines of Code (NCLOC) 12839 (94.56%) - Logical Lines of Code (LLOC) 1419 (10.45%) - Classes 1318 (92.88%) + Lines of Code (LOC) 13571 + Comment Lines of Code (CLOC) 737 (5.43%) + Non-Comment Lines of Code (NCLOC) 12834 (94.57%) + Logical Lines of Code (LLOC) 1418 (10.45%) + Classes 1317 (92.88%) Average Class Length 3 Minimum Class Length 0 Maximum Class Length 27 @@ -24,7 +24,7 @@ Size Cyclomatic Complexity Average Complexity per LLOC 0.33 - Average Complexity per Class 2.04 + Average Complexity per Class 2.03 Minimum Class Complexity 1.00 Maximum Class Complexity 21.00 Average Complexity per Method 1.49 @@ -36,12 +36,12 @@ Dependencies Global Constants 0 (0.00%) Global Variables 0 (0.00%) Super-Global Variables 0 (0.00%) - Attribute Accesses 604 - Non-Static 588 (97.35%) + Attribute Accesses 603 + Non-Static 587 (97.35%) Static 16 (2.65%) - Method Calls 758 - Non-Static 681 (89.84%) - Static 77 (10.16%) + Method Calls 755 + Non-Static 678 (89.80%) + Static 77 (10.20%) Structure Namespaces 77 @@ -52,14 +52,14 @@ Structure Concrete Classes 191 (93.17%) Final Classes 70 (36.65%) Non-Final Classes 121 (63.35%) - Methods 968 + Methods 969 Scope - Non-Static Methods 903 (93.29%) + Non-Static Methods 904 (93.29%) Static Methods 65 (6.71%) Visibility - Public Methods 763 (78.82%) - Protected Methods 64 (6.61%) - Private Methods 141 (14.57%) + Public Methods 764 (78.84%) + Protected Methods 64 (6.60%) + Private Methods 141 (14.55%) Functions 37 Named Functions 7 (18.92%) Anonymous Functions 30 (81.08%) diff --git a/.tools/.report/.phploc/.test.phploc.report b/.tools/.report/.phploc/.test.phploc.report index 35029606..db79f496 100644 --- a/.tools/.report/.phploc/.test.phploc.report +++ b/.tools/.report/.phploc/.test.phploc.report @@ -4,11 +4,11 @@ Directories 74 Files 273 Size - Lines of Code (LOC) 30544 + Lines of Code (LOC) 30570 Comment Lines of Code (CLOC) 458 (1.50%) - Non-Comment Lines of Code (NCLOC) 30086 (98.50%) - Logical Lines of Code (LLOC) 6687 (21.89%) - Classes 6679 (99.88%) + Non-Comment Lines of Code (NCLOC) 30112 (98.50%) + Logical Lines of Code (LLOC) 6698 (21.91%) + Classes 6690 (99.88%) Average Class Length 24 Minimum Class Length 0 Maximum Class Length 871 @@ -24,7 +24,7 @@ Size Cyclomatic Complexity Average Complexity per LLOC 0.07 - Average Complexity per Class 2.60 + Average Complexity per Class 2.59 Minimum Class Complexity 1.00 Maximum Class Complexity 16.00 Average Complexity per Method 1.26 @@ -39,9 +39,9 @@ Dependencies Attribute Accesses 28 Non-Static 23 (82.14%) Static 5 (17.86%) - Method Calls 9132 - Non-Static 5989 (65.58%) - Static 3143 (34.42%) + Method Calls 9142 + Non-Static 5994 (65.57%) + Static 3148 (34.43%) Structure Namespaces 74 @@ -52,14 +52,14 @@ Structure Concrete Classes 266 (98.52%) Final Classes 248 (93.23%) Non-Final Classes 18 (6.77%) - Methods 1724 + Methods 1725 Scope - Non-Static Methods 1616 (93.74%) + Non-Static Methods 1617 (93.74%) Static Methods 108 (6.26%) Visibility - Public Methods 1036 (60.09%) - Protected Methods 313 (18.16%) - Private Methods 375 (21.75%) + Public Methods 1037 (60.12%) + Protected Methods 313 (18.14%) + Private Methods 375 (21.74%) Functions 90 Named Functions 2 (2.22%) Anonymous Functions 88 (97.78%) diff --git a/src/Spinner/Core/Palette/A/ACharPalette.php b/src/Spinner/Core/Palette/A/ACharPalette.php index 609020c5..2b376e71 100644 --- a/src/Spinner/Core/Palette/A/ACharPalette.php +++ b/src/Spinner/Core/Palette/A/ACharPalette.php @@ -18,14 +18,18 @@ abstract class ACharPalette extends APalette implements ICharPalette */ protected function getEntries(?IPaletteMode $mode = null): Traversable { - /** @var string $element */ + /** @var string|ICharFrame $element */ foreach ($this->sequence() as $element) { + if ($element instanceof ICharFrame) { + yield $element; + continue; + } yield $this->createFrame($element); } } /** - * @return Traversable + * @return Traversable */ abstract protected function sequence(): Traversable; diff --git a/src/Spinner/Core/Palette/A/APalette.php b/src/Spinner/Core/Palette/A/APalette.php index 35d52b4a..7b17f469 100644 --- a/src/Spinner/Core/Palette/A/APalette.php +++ b/src/Spinner/Core/Palette/A/APalette.php @@ -20,6 +20,7 @@ public function __construct( ) { } + // FIXME (2023-12-04 16:9) [Alec Rabbit]: extract functionality to PaletteTemplateFactory->create($palette) public function getTemplate(?IPaletteMode $mode = null): IPaletteTemplate { return new PaletteTemplate( diff --git a/src/Spinner/Core/Palette/PaletteTemplate.php b/src/Spinner/Core/Palette/PaletteTemplate.php index 79243583..f9f44fd9 100644 --- a/src/Spinner/Core/Palette/PaletteTemplate.php +++ b/src/Spinner/Core/Palette/PaletteTemplate.php @@ -40,6 +40,10 @@ public function getOptions(): IPaletteOptions return $this->options; } + /** + * @param Traversable