Grid.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Laravel\Prompts;
  3. use Illuminate\Support\Collection;
  4. class Grid extends Prompt
  5. {
  6. /**
  7. * The grid items.
  8. *
  9. * @var array<int, string>
  10. */
  11. public array $items;
  12. /**
  13. * The maximum width of the grid.
  14. */
  15. public int $maxWidth;
  16. /**
  17. * Create a new Grid instance.
  18. *
  19. * @param array<int, string>|Collection<int, string> $items
  20. */
  21. public function __construct(array|Collection $items = [], ?int $maxWidth = null)
  22. {
  23. $this->items = $items instanceof Collection ? $items->all() : $items;
  24. $this->maxWidth = $maxWidth ?? static::terminal()->cols() ?: 80;
  25. }
  26. /**
  27. * Display the grid.
  28. */
  29. public function display(): void
  30. {
  31. $this->prompt();
  32. }
  33. /**
  34. * Display the grid.
  35. */
  36. public function prompt(): bool
  37. {
  38. if ($this->items === []) {
  39. return true;
  40. }
  41. $this->capturePreviousNewLines();
  42. $this->state = 'submit';
  43. static::output()->write($this->renderTheme());
  44. return true;
  45. }
  46. /**
  47. * Get the value of the prompt.
  48. */
  49. public function value(): bool
  50. {
  51. return true;
  52. }
  53. }