Skip to content

Instantly share code, notes, and snippets.

@thureos
Created September 14, 2018 17:40
Show Gist options
  • Save thureos/6eb3ccb6baa00a35f2b7d2cc0afe9795 to your computer and use it in GitHub Desktop.
Save thureos/6eb3ccb6baa00a35f2b7d2cc0afe9795 to your computer and use it in GitHub Desktop.
Couple functions to create a CLI wizard
<?php
function say($text, $color = null, $default = null)
{
$colors = [
'black' => "30m",'red' => "31m",'green' => "32m",'yellow' => "33m",'blue' => "34m",'magenta' => "35m",
'cyan' => "36m",'lightGray' => "37m",'darkGray' => "90m",'lightRed' => "91m",'lightGreen' => "92m",'lightYellow' => "93m",
'lightBlue' => "94m",'lightMagenta' => "95m",'lightCyan' => "96m",'white' => "97m",'noColor' => "0m",
];
$open = @$colors[$color] ?: '0m';
$close = '0m';
$isQuestion = strpos($text, '?') !== false;
if(!$isQuestion){
echo "\033[" . $open . "{$text} \033[{$close}" . PHP_EOL;
}else{
echo "\033[" . $open . "{$text} [{$default}] : \033[{$close}" . PHP_EOL;
$handle = fopen("php://stdin", "r");
$input = trim(fgets($handle));
fclose($handle);
return $input ?: $default;
}
}
function run($command, $out = true)
{
say("Running: $command", 'yellow');
ob_start();
passthru($command, $return);
$result = ob_get_contents();
ob_end_clean();
if($out) echo $result;
return $result;
}
/******* YOUR WIZARDS STARTS HERE ********/
run('ls -lha', false); // No Output
run('ls -lha');
$groups = run('groups');
say("These are my groups: $groups", 'magenta');
$userName = run('whoami');
run("id -u $userName");
$name = say('What is your name?', 'green', 'Matt Mozzarella');
say("Hello $name");
say("Hello $name", 'black');
say("Hello $name", 'red');
say("Hello $name", 'green');
say("Hello $name", 'yellow');
say("Hello $name", 'blue');
say("Hello $name", 'magenta');
say("Hello $name", 'cyan');
say("Hello $name", 'lightGray');
say("Hello $name", 'darkGray');
say("Hello $name", 'lightRed');
say("Hello $name", 'lightGreen');
say("Hello $name", 'lightYellow');
say("Hello $name", 'lightBlue');
say("Hello $name", 'lightMagenta');
say("Hello $name", 'lightCyan');
say("Hello $name", 'white');
@thureos
Copy link
Author

thureos commented Sep 14, 2018

test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment