Skip to content

Instantly share code, notes, and snippets.

@rasteiner
Created October 3, 2022 16:01
Show Gist options
  • Save rasteiner/e2259084902c00acfbef5ac861cdfd40 to your computer and use it in GitHub Desktop.
Save rasteiner/e2259084902c00acfbef5ac861cdfd40 to your computer and use it in GitHub Desktop.
Interactive shell for kirby cli
<?php
namespace rasteiner\kirby\cli\interactive;
class Shell {
public function __autocomplete(string $str): array {
$info = readline_info();
$line = $info['line_buffer'];
$pos = $info['end'];
$line = substr($line, 0, $pos);
$commands = $this->cli->commands();
$commands = ['exit', ... $commands['core'], ... $commands['global'], ... $commands['custom']];
return array_filter($commands, function($command) use ($line) {
return strpos($command, $line) === 0;
});
}
public function __construct(public $cli) {
$cli->info('Welcome to the Kirby Shell');
$cli->br();
$cli->info('Type "exit" to quit');
readline_completion_function([$this, '__autocomplete']);
while (true) {
$line = trim(readline('kirby> '));
if ($line === 'exit') {
break;
}
if ($line === 'i') {
$cli->info('You are already running the interactive shell');
continue;
}
// split line into command and arguments
$parts = explode(' ', $line);
$cli->command(... $parts);
}
}
}
return [
'description' => 'Interactive Kirby Shell',
'args' => [],
'command' => static function ($cli): void {
if(!function_exists('readline')) {
$cli->error('Readline is not available');
return;
}
new Shell($cli);
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment