Skip to content

Commit

Permalink
Display the repeat expression for sub-minute events
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Jul 12, 2023
1 parent 07033bd commit a6834e9
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/Illuminate/Console/Scheduling/ScheduleListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ public function handle(Schedule $schedule)

$expressionSpacing = $this->getCronExpressionSpacing($events);

$repeatExpressionSpacing = $this->getRepeatExpressionSpacing($events);

$timezone = new DateTimeZone($this->option('timezone') ?? config('app.timezone'));

$events = $this->sortEvents($events, $timezone);

$events = $events->map(function ($event) use ($terminalWidth, $expressionSpacing, $timezone) {
$events = $events->map(function ($event) use ($terminalWidth, $expressionSpacing, $repeatExpressionSpacing, $timezone) {
$expression = $this->formatCronExpression($event->expression, $expressionSpacing);

$repeatExpression = str_pad($this->getRepeatExpression($event), $repeatExpressionSpacing);

$command = $event->command ?? '';

$description = $event->description ?? '';
Expand Down Expand Up @@ -101,15 +105,16 @@ public function handle(Schedule $schedule)
$hasMutex = $event->mutex->exists($event) ? 'Has Mutex › ' : '';

$dots = str_repeat('.', max(
$terminalWidth - mb_strlen($expression.$command.$nextDueDateLabel.$nextDueDate.$hasMutex) - 8, 0
$terminalWidth - mb_strlen($expression.$repeatExpression.$command.$nextDueDateLabel.$nextDueDate.$hasMutex) - 8, 0
));

// Highlight the parameters...
$command = preg_replace("#(php artisan [\w\-:]+) (.+)#", '$1 <fg=yellow;options=bold>$2</>', $command);

return [sprintf(
' <fg=yellow>%s</> %s<fg=#6C7280>%s %s%s %s</>',
' <fg=yellow>%s</> <fg=#6C7280>%s</> %s<fg=#6C7280>%s %s%s %s</>',
$expression,
$repeatExpression,
$command,
$dots,
$hasMutex,
Expand Down Expand Up @@ -141,6 +146,28 @@ private function getCronExpressionSpacing($events)
return collect($rows[0] ?? [])->keys()->map(fn ($key) => $rows->max($key))->all();
}

/**
* Gets the spacing to be used on each event row.
*
* @param \Illuminate\Support\Collection $events
* @return int
*/
private function getRepeatExpressionSpacing($events)
{
return $events->map(fn ($event) => mb_strlen($this->getRepeatExpression($event)))->max();
}

/**
* Gets the repeat expression for an event.
*
* @param \Illuminate\Console\Scheduling\Event $event
* @return string
*/
private function getRepeatExpression($event)
{
return $event->isRepeatable() ? "{$event->repeatSeconds}s " : '';
}

/**
* Sorts the events by due date if option set.
*
Expand Down

0 comments on commit a6834e9

Please sign in to comment.