Skip to content

Commit

Permalink
SplFileInfoValue::getDisplayValue: Return short path
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Nov 3, 2024
1 parent ecbe3f6 commit 66c2a45
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
Binary file modified build/kint.phar
Binary file not shown.
3 changes: 3 additions & 0 deletions src/Renderer/Text/SplFileInfoPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public function render(AbstractValue $v): ?string
}

$out .= $this->renderer->renderHeader($v);
if (null !== $v->getDisplayValue()) {
$out .= ':';
}
$out .= ' '.$this->renderer->colorValue($this->renderer->escape($r->getValue())).PHP_EOL;

return $out;
Expand Down
3 changes: 2 additions & 1 deletion src/Value/Representation/SplFileInfoRepresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function __construct(SplFileInfo $fileInfo)
$typename = 'Unknown file';

try {
if (\strlen($path) && $fileInfo->getRealPath()) {
// SplFileInfo::getRealPath will return cwd when path is ''
if ('' !== $path && $fileInfo->getRealPath()) {
$perms = $fileInfo->getPerms();
$size = $fileInfo->getSize();
$owner = $fileInfo->getOwner();
Expand Down
18 changes: 17 additions & 1 deletion src/Value/SplFileInfoValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@

class SplFileInfoValue extends InstanceValue
{
/** @psalm-readonly */
protected string $path;
/** @psalm-readonly */
protected ?int $filesize = null;

public function __construct(ContextInterface $context, SplFileInfo $info)
{
parent::__construct($context, \get_class($info), \spl_object_hash($info), \spl_object_id($info));

$this->path = $info->getPathname();

try {
if (\strlen($info->getPathname()) && $info->getRealPath()) {
// SplFileInfo::getRealPath will return cwd when path is ''
if ('' !== $this->path && $info->getRealPath()) {
$this->filesize = $info->getSize();
}
} catch (RuntimeException $e) {
Expand Down Expand Up @@ -73,4 +78,15 @@ public function getDisplaySize(): ?string

return $size['value'].$size['unit'];
}

public function getDisplayValue(): ?string
{
$shortpath = Utils::shortenPath($this->path);

if ($shortpath !== $this->path) {
return $shortpath;
}

return parent::getDisplayValue();
}
}
5 changes: 5 additions & 0 deletions tests/Value/Representation/SplFileInfoRepresentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ public function fileProvider()
'---------- ',
'Unknown file',
],
'garbage' => [
'asdffffff',
'---------- asdffffff',
'Unknown file',
],
];
}

Expand Down

0 comments on commit 66c2a45

Please sign in to comment.