Skip to content
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
5 changes: 4 additions & 1 deletion src/Console/Components/Details.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpTui\Tui\Extension\Core\Widget\ScrollbarWidget;
use PhpTui\Tui\Style\Style;
use PhpTui\Tui\Text\Line;
use PhpTui\Tui\Text\Span;
use PhpTui\Tui\Text\Text;
use PhpTui\Tui\Widget\Widget;
use Tapper\Console\CommandAttributes\KeyPressed;
Expand Down Expand Up @@ -136,7 +137,9 @@ protected function view(Area $area): Widget
];
$infoListItems = array_map(fn ($line) => ListItem::new(Text::fromLine($line)), $info);

$formattedMessage = MessageFormatter::colorizeFormattedJson($log->message);
$formattedMessage = $log->kind === 'wait'
? [Line::fromSpan(Span::styled($log->message, Style::default()->yellow()))]
: MessageFormatter::colorizeFormattedJson($log->message);
$formattedListItems = array_map(fn ($line) => ListItem::new(Text::fromLine($line)), $formattedMessage);

$allItems = [
Expand Down
81 changes: 59 additions & 22 deletions src/Console/Components/LogItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
use PhpTui\Tui\Extension\Core\Widget\ParagraphWidget;
use PhpTui\Tui\Layout\Constraint;
use PhpTui\Tui\Style\Style;
use PhpTui\Tui\Text\Line;
use PhpTui\Tui\Text\Span;
use PhpTui\Tui\Text\Text;
use PhpTui\Tui\Widget\Direction;
use PhpTui\Tui\Widget\Widget;
use Tapper\Console\CommandAttributes\Mouse;
use Tapper\Console\Component;
use Tapper\Console\MessageFormatter;
use Tapper\Console\Palette;
use Tapper\Console\State\LogItem as LogItemState;

class LogItem extends Component
{
public const int HEIGHT = 3;
public const int HEIGHT = 2;

private const int SCROLLBAR_GUTTER = 1;

private ?LogItemState $log = null;

Expand Down Expand Up @@ -65,6 +66,43 @@ public function click(): void
}
}

/**
* @param Span[] $spans
* @return Span[]
*/
private function truncateSpans(array $spans, int $maxWidth, Style $ellipsisStyle): array
{
$totalWidth = array_sum(array_map(fn (Span $span): int => $span->width(), $spans));

if ($totalWidth <= $maxWidth) {
return $spans;
}

$budget = max(0, $maxWidth - 1);
$truncated = [];

foreach ($spans as $span) {
if ($budget <= 0) {
break;
}

if ($span->width() <= $budget) {
$truncated[] = $span;
$budget -= $span->width();

continue;
}

$chars = array_slice(mb_str_split($span->content), 0, $budget);
$truncated[] = Span::styled(implode('', $chars), $span->style);
$budget = 0;
}

$truncated[] = Span::styled('…', $ellipsisStyle);

return $truncated;
}

protected function view(Area $area): Widget
{
if (! $this->log) {
Expand All @@ -77,35 +115,34 @@ protected function view(Area $area): Widget
$mark = $this->appState->cursor === $this->log->id;

$darkGray = Style::default()->darkGray();
$markerColor = RgbColor::fromHex('2a2e42');
$markerColor = RgbColor::fromHex(Palette::SELECTION_BG);
$mStyle = Style::default()->bg($markerColor);

$message = $this->log->message;
$messageWidth = max(0, $area->width - 17 - self::SCROLLBAR_GUTTER);

$firstLine = ParagraphWidget::fromText(
Text::fromLines(
Line::fromSpans(Span::styled("$time", Style::default()->fg(RgbColor::fromHex('7aa2f7')))),
Line::fromSpans(Span::styled('', $darkGray)),
)
$timeColumn = ParagraphWidget::fromSpans(
Span::styled($time, Style::default()->fg(RgbColor::fromHex(Palette::ACCENT))),
);

$wMess = ParagraphWidget::fromSpans(...MessageFormatter::colorizeInlineJson($message));
$wFile = ParagraphWidget::fromSpans(Span::styled(sprintf('↪ %s', $this->log->caller), $darkGray));
$messageSpans = $this->log->kind === 'wait'
? [Span::styled($message, Style::default()->yellow())]
: MessageFormatter::colorizeInlineJson($message);

$wMess = ParagraphWidget::fromSpans(...$this->truncateSpans($messageSpans, $messageWidth, $darkGray));
$wFile = ParagraphWidget::fromSpans(
...$this->truncateSpans([Span::styled(sprintf('↪ %s', $this->log->caller), $darkGray)], $messageWidth, $darkGray),
);

if ($mark) {
$firstLine->style($mStyle);
$timeColumn->style($mStyle);
$wMess->style($mStyle);
$wFile->style($mStyle);
}

$firstLine = GridWidget::default()
->direction(Direction::Vertical)
->constraints(Constraint::length(2), Constraint::length(1))
->widgets($firstLine);

$secondLine = GridWidget::default()
$messageColumn = GridWidget::default()
->direction(Direction::Vertical)
->constraints(Constraint::length(1), Constraint::length(1), Constraint::length(1))
->constraints(Constraint::length(1), Constraint::length(1))
->widgets(
$wMess,
$wFile,
Expand All @@ -117,10 +154,10 @@ protected function view(Area $area): Widget
->direction(Direction::Horizontal)
->constraints(
Constraint::length(17),
Constraint::length($area->width - 17),
Constraint::length($messageWidth),
)->widgets(
$firstLine,
$secondLine,
$timeColumn,
$messageColumn,
),
);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Console/Components/LogList.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public function scrollDown(): void
$this->scroll->scrollDown($this->count, $this->visible);
}

#[KeyPressed('g')]
public function jumpToTop(): void
{
$this->scroll->jump(0, $this->count, $this->visible);
}

#[KeyPressed('G')]
public function jumpToBottom(): void
{
$this->scroll->jump($this->count - 1, $this->count, $this->visible);
}

#[KeyPressed('u', KeyModifiers::CONTROL)]
public function pageUp(): void
{
Expand Down
7 changes: 4 additions & 3 deletions src/Console/Components/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpTui\Tui\Widget\BorderType;
use PhpTui\Tui\Widget\Widget;
use Tapper\Console\Component;
use Tapper\Console\Palette;

class Navigation extends Component
{
Expand All @@ -23,14 +24,14 @@ protected function view(Area $area): Widget

$waiting = $this->appState->pendingWaits > 0;

$buttonStyle = Style::default()->fg(RgbColor::fromHex('7aa2f7'));
$descStyle = Style::default()->fg(RgbColor::fromHex('7aa2f7'));
$buttonStyle = Style::default()->fg(RgbColor::fromHex(Palette::ACCENT));
$descStyle = Style::default()->fg(RgbColor::fromHex(Palette::ACCENT));
$delimiter = Span::styled(' | ', $descStyle);

$instruction = [];

if (! $this->appState->live && ! $this->appState->previewLog) {
$instruction[] = Span::styled('Live: ', $descStyle, $descStyle);
$instruction[] = Span::styled('Live: ', $descStyle);
$instruction[] = Span::styled('esc', $buttonStyle);
$instruction[] = $delimiter;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Console/MessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class MessageFormatter

private const string NULL_COLOR = '2bc3de';

private const string BRACKETS_COLOR = 'c0caf5';
private const string BRACKETS_COLOR = Palette::TEXT_DEFAULT;

private const string PUNCTUATION_COLOR = '89ddff';

private const string ERROR_COLOR = 'f7768e';
private const string ERROR_COLOR = Palette::ERROR;

public static function colorizeInlineJson(string $json): array
{
Expand Down
20 changes: 20 additions & 0 deletions src/Console/Palette.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Tapper\Console;

/**
* Shared hex color slots for the TUI chrome. Widgets should reference these
* by name instead of writing raw hex literals, so the theme lives in one place.
*/
final class Palette
{
public const string ACCENT = '7aa2f7';

public const string TEXT_DEFAULT = 'c0caf5';

public const string SELECTION_BG = '2a2e42';

public const string ERROR = 'f7768e';
}
1 change: 1 addition & 0 deletions src/Console/State/LogItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public function __construct(
public array $trace,
public string $rootDir,
public array $code,
public string $kind = 'log',
) {}
}
20 changes: 20 additions & 0 deletions src/Console/Windows/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
namespace Tapper\Console\Windows;

use PhpTui\Tui\Display\Area;
use PhpTui\Tui\Extension\Core\Widget\BlockWidget;
use PhpTui\Tui\Extension\Core\Widget\GridWidget;
use PhpTui\Tui\Extension\Core\Widget\ParagraphWidget;
use PhpTui\Tui\Layout\Constraint;
use PhpTui\Tui\Layout\Layout;
use PhpTui\Tui\Text\Line;
use PhpTui\Tui\Widget\Direction;
use PhpTui\Tui\Widget\HorizontalAlignment;
use PhpTui\Tui\Widget\Widget;
use React\EventLoop\Loop;
use Tapper\Console\Component;
Expand All @@ -21,6 +25,10 @@

class Main extends Component
{
private const int MIN_WIDTH = 40;

private const int MIN_HEIGHT = 8;

protected array $components = [
Header::class,
Details::class,
Expand Down Expand Up @@ -71,6 +79,18 @@ public function afterInit(): void

protected function view(Area $area): Widget
{
if ($area->width < self::MIN_WIDTH || $area->height < self::MIN_HEIGHT) {
return BlockWidget::default()
->widget(
ParagraphWidget::fromLines(
Line::fromString('Terminal too small')->alignment(HorizontalAlignment::Center),
Line::fromString(sprintf('Resize to at least %dx%d', self::MIN_WIDTH, self::MIN_HEIGHT))
->alignment(HorizontalAlignment::Center)
->darkGray(),
),
);
}

$verticalConstraints = [
Constraint::length(2),
Constraint::length($area->height - 4),
Expand Down
6 changes: 4 additions & 2 deletions src/Console/Windows/Popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PhpTui\Tui\Widget\Widget;
use Tapper\Console\CommandAttributes\KeyPressed;
use Tapper\Console\Component;
use Tapper\Console\Palette;

class Popup extends Component
{
Expand All @@ -40,11 +41,12 @@ public function close(): void

protected function view(Area $area): Widget
{
$keyStyle = Style::default()->fg(RgbColor::fromHex('7aa2f7'));
$descStyle = Style::default()->fg(RgbColor::fromHex('c0caf5'));
$keyStyle = Style::default()->fg(RgbColor::fromHex(Palette::ACCENT));
$descStyle = Style::default()->fg(RgbColor::fromHex(Palette::TEXT_DEFAULT));

$shortcuts = [
['↑ / k', '↓ / j', 'Move cursor'],
['g', 'G', 'Jump to top / bottom'],
['Ctrl+u', 'Ctrl+d', 'Page up / down'],
['space', 'enter', 'Open details'],
['enter', '', 'Continue a paused wait'],
Expand Down
1 change: 1 addition & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function run(): void
$params['trace'],
$params['rootDir'],
$params['code'],
kind: 'wait',
));

self::$id++;
Expand Down
Loading