Skip to content

Commit

Permalink
Merge pull request #12 from hlipnick/git-branch-display
Browse files Browse the repository at this point in the history
Add git branch name to badge display
  • Loading branch information
pxlrbt authored Nov 18, 2024
2 parents 928cceb + 2d3f346 commit e55bca2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ $panel->plugins([
]);
```

### Git Branch

You can enable the display of the current git branch in the badge via `->showGitBranch()`. This requires the `exec()` function to be enabled in your PHP configuration.

```php
use pxlrbt\FilamentEnvironmentIndicator\EnvironmentIndicatorPlugin;
use Filament\Support\Colors\Color;

$panel->plugins([
EnvironmentIndicatorPlugin::make()
->showGitBranch()
]);
```

## Contributing

If you want to contribute to this packages, you may want to test it in a real Filament project:
Expand Down
4 changes: 4 additions & 0 deletions resources/views/badge.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ class="
"
>
{{ $environment }}

@isset($branch)
<code class="text-xs ml-1" style="margin-top: 1px">({{ $branch }})</code>
@endisset
</div>
24 changes: 24 additions & 0 deletions src/EnvironmentIndicatorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Filament\Support\Concerns\EvaluatesClosures;
use Illuminate\Support\Facades\View;
use Illuminate\Support\HtmlString;
use Throwable;

class EnvironmentIndicatorPlugin implements Plugin
{
Expand All @@ -22,6 +23,8 @@ class EnvironmentIndicatorPlugin implements Plugin

public array|Closure|null $color = null;

public bool|Closure|null $showGitBranch = null;

public static function make(): static
{
$plugin = app(static::class);
Expand Down Expand Up @@ -83,6 +86,7 @@ public function register(Panel $panel): void
return View::make('filament-environment-indicator::badge', [
'color' => $this->getColor(),
'environment' => ucfirst(app()->environment()),
'branch' => $this->getGitBranch()
]);
});

Expand Down Expand Up @@ -131,6 +135,13 @@ public function showBorder(bool|Closure $showBorder = true): static
return $this;
}

public function showGitBranch(bool|Closure $showGitBranch = true): static
{
$this->showGitBranch = $showGitBranch;

return $this;
}

public function color(array|Closure $color = Color::Pink): static
{
$this->color = $color;
Expand All @@ -142,4 +153,17 @@ protected function getColor(): array
{
return $this->evaluate($this->color);
}

protected function getGitBranch(): ?string
{
if (! $this->evaluate($this->showGitBranch)) {
return null;
}

try {
return trim(exec('git branch --show-current'));
} catch (Throwable $th) {
return null;
}
}
}

0 comments on commit e55bca2

Please sign in to comment.