Skip to content

Commit

Permalink
Gh 211: line truncation (#216)
Browse files Browse the repository at this point in the history
* Fix

* Fix truncation
  • Loading branch information
dantleech committed Dec 4, 2023
1 parent 2da2df8 commit 8e0e598
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Refactoring:

Bug fixes:

- Fix put line truncation #211
- Fix block adjacent border renderering #201
- Fix `StyledGrapheme` symbol width #214 @KennedyTedesco
- Fix issue when truncating unicode text #215 @KennedyTedesco
Expand Down
7 changes: 3 additions & 4 deletions src/Display/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ public function area(): Area
public function putLine(Position $position, Line $line, int $width): Position
{
$remainingWidth = $width;
$x = $position->x;
foreach ($line as $span) {
if ($remainingWidth === 0) {
return $position;
Expand All @@ -211,9 +210,9 @@ public function putLine(Position $position, Line $line, int $width): Position
$span->style,
$remainingWidth,
);
$w = max(0, $position->x - $newPosition->x);
$position = $position->withX($newPosition->x);
$remainingWidth -= max(0, $remainingWidth - $position->x);
$width = max(0, $newPosition->x - $position->x);
$position = $newPosition;
$remainingWidth = max(0, $remainingWidth, $width);
}

return $position;
Expand Down
22 changes: 11 additions & 11 deletions tests/Example/snapshot/List.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
│ [q]uit │ events │ canvas │ chart │ list │ table │ blocks │ sprite │ colors │ │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│INFO Event
│INFO Event
│INFO Event1
│INFO Event2
│CRITICAL Event3 │
│ERROR Event4 │
│INFO Event
│INFO Event
│INFO Event5
│INFO Event6
│WARNING Event7 │
│INFO Event
│INFO Event
│INFO Event
│INFO Event8
│INFO Event9
│INFO Event10
│CRITICAL Event11 │
│INFO Event
│INFO Event
│INFO Event
│INFO Event
│INFO Event12
│INFO Event13
│INFO Event14
│INFO Event15
└──────────────────────────────────────────────────────────────────────────────┘
17 changes: 17 additions & 0 deletions tests/Unit/Model/Display/BufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpTui\Tui\Style\Style;
use PhpTui\Tui\Text\Line;
use PHPUnit\Framework\TestCase;
use PhpTui\Tui\Text\Span;

final class BufferTest extends TestCase
{
Expand Down Expand Up @@ -81,6 +82,22 @@ public function testPutLine(): void
], $buffer->toLines());
}

public function testPutLineManySpans(): void
{
$buffer = Buffer::empty(Area::fromDimensions(14, 4));
$buffer->putLine(Position::at(1, 1), Line::fromSpans(
Span::fromString('one'),
Span::fromString('😸'),
Span::fromString('three'),
), 10);
self::assertEquals([
' ',
' one😸three ',
' ',
' ',
], $buffer->toLines());
}

public function testDiffStylesOnly(): void
{
$b1 = Buffer::fromLines(['a']);
Expand Down

0 comments on commit 8e0e598

Please sign in to comment.