Skip to content

Commit

Permalink
Improved listing environments
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc45 committed May 8, 2024
1 parent 850eb0c commit 4e3645f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Binary file modified qit
Binary file not shown.
18 changes: 17 additions & 1 deletion src/src/Commands/Environment/ListEnvironmentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace QIT_CLI\Commands\Environment;

use QIT_CLI\App;
use QIT_CLI\Environment\EnvironmentMonitor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Terminal;
use function QIT_CLI\format_elapsed_time;

class ListEnvironmentCommand extends Command {
Expand Down Expand Up @@ -55,8 +57,18 @@ protected function execute( InputInterface $input, OutputInterface $output ): in

$output->writeln( '<info>Running environments:</info>' );

$terminal_width = App::make( Terminal::class )->getWidth();
$longest_header = 0;
$definitions = [];

foreach ( $running as $environment ) {
foreach ( $environment as $k => $v ) {
if ( strlen( $k ) > $longest_header ) {
$longest_header = strlen( $k );
}
}
}

foreach ( $running as $environment ) {
$elapsed = format_elapsed_time( time() - $environment->created_at );

Expand All @@ -73,12 +85,16 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
if ( is_array( $v ) ) {
// "implode" only works on flat arrays, otherwise we need "print_r".
$is_flat = count( array_filter( $v, 'is_array' ) ) === 0;
$v = $is_flat ? implode( ', ', $v ) : print_r( $v, true );
$v = $is_flat ? implode( "\n", $v ) : json_encode( $v, JSON_UNESCAPED_SLASHES );
}
// Check if "field" option is set, and only add if matches.
if ( $input->getOption( 'field' ) && $input->getOption( 'field' ) !== $k ) {
continue;
}

// Wrap "$v" by terminal width - longest header - 10 (for padding).
$v = wordwrap( $v, $terminal_width - $longest_header - 10, "\n", true );

$definitions[] = [ ucwords( $k ) => $v ];
}

Expand Down

0 comments on commit 4e3645f

Please sign in to comment.