Skip to content

Commit

Permalink
Sort chat rooms by active users and limit for terminal height
Browse files Browse the repository at this point in the history
  • Loading branch information
joecampo committed Mar 14, 2023
1 parent 23abb64 commit f12997a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions app/Actions/DisplayChatRooms.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Lorisleiva\Actions\Concerns\AsAction;
use NunoMaduro\LaravelConsoleMenu\Menu;
use React\Socket\ConnectionInterface;
use function Termwind\{terminal}; //@codingStandardsIgnoreLine

class DisplayChatRooms
{
Expand All @@ -14,9 +15,13 @@ class DisplayChatRooms
public function handle(ConnectionInterface $connection, Collection $rooms): void
{
with(new Menu('Select a chat room'), function (Menu $menu) use ($rooms, $connection) {
$rooms->each(function ($room) use ($menu) {
$menu->addOption($room['name'], str($room['name'])->padRight(20, ' ')."({$room['people']})");
});
$rooms
->filter(fn ($room) => $room['people'] > 0)
->take(terminal()->height() - 10)
->sortBy('people', SORT_NATURAL, true)
->each(function ($room) use ($menu) {
$menu->addOption($room['name'], str($room['name'])->padRight(20, ' ')."({$room['people']})");
});

with($menu->disableDefaultItems()->open(), function (string $name) use ($connection) {
LaunchChat::run($connection, $name);
Expand Down

0 comments on commit f12997a

Please sign in to comment.