Added CLI functions

This commit is contained in:
Netkas 2022-07-26 18:56:12 -04:00
parent 265cfce65b
commit a3236ba1ee
9 changed files with 295 additions and 14 deletions

View file

@ -0,0 +1,40 @@
<?php
namespace ncc\Abstracts;
abstract class ConsoleColors
{
const Default = "\e[39m";
const Black = "\e[30m";
const Red = "\e[31m";
const Green = "\e[32m";
const Yellow = "\e[33m";
const Blue = "\e[34m";
const Magenta = "\e[35m";
const Cyan = "\e[36m";
const LightGray = "\e[37m";
const DarkGrey = "\e[90m";
const LightRed = "\e[91m";
const LightGreen = "\e[92m";
const LightYellow = "\e[93m";
const LightBlue = "\e[94m";
const LightMagenta = "\e[95m";
const LightCyan = "\e[96m";
const White = "\e[97m";
}

View file

@ -3,6 +3,7 @@
namespace ncc\CLI;
use ncc\Objects\CliHelpSection;
use ncc\Utilities\Console;
class HelpMenu
{
@ -24,9 +25,9 @@
// TODO: Make copyright not hard-coded.
print(\ncc\Utilities\Functions::getBanner(NCC_VERSION_BRANCH . ' ' . NCC_VERSION_NUMBER, 'Copyright (c) 2022-2022 Nosial', $basic_ascii) . PHP_EOL);
print('Usage: ncc COMMAND [options]' . PHP_EOL);
print('Alternative Usage: ncc.php --ncc-cli=COMMAND [options]' . PHP_EOL . PHP_EOL);
print('Nosial Code Compiler / Project Toolkit' . PHP_EOL . PHP_EOL);
Console::out('Usage: ncc COMMAND [options]' . PHP_EOL);
Console::out('Alternative Usage: ncc.php --ncc-cli=COMMAND [options]' . PHP_EOL . PHP_EOL);
Console::out('Nosial Code Compiler / Project Toolkit' . PHP_EOL . PHP_EOL);
self::displayMainOptions();
self::displayManagementCommands();
@ -52,10 +53,10 @@
];
$options_padding = \ncc\Utilities\Functions::detectParametersPadding($options) + 4;
print('Options:' . PHP_EOL);
Console::out('Options:' . PHP_EOL);
foreach($options as $option)
{
print(' ' . $option->toString($options_padding) . PHP_EOL);
Console::out(' ' . $option->toString($options_padding) . PHP_EOL);
}
print(PHP_EOL);
}
@ -75,10 +76,10 @@
];
$commands_padding = \ncc\Utilities\Functions::detectParametersPadding($commands) + 2;
print('Management Commands:' . PHP_EOL);
Console::out('Management Commands:' . PHP_EOL);
foreach($commands as $command)
{
print(' ' . $command->toString($commands_padding) . PHP_EOL);
Console::out(' ' . $command->toString($commands_padding) . PHP_EOL);
}
print(PHP_EOL);
}
@ -96,10 +97,10 @@
];
$commands_padding = \ncc\Utilities\Functions::detectParametersPadding($commands) + 2;
print('Commands:' . PHP_EOL);
Console::out('Commands:' . PHP_EOL);
foreach($commands as $command)
{
print(' ' . $command->toString($commands_padding) . PHP_EOL);
Console::out(' ' . $command->toString($commands_padding) . PHP_EOL);
}
print(PHP_EOL);
}

View file

@ -4,6 +4,7 @@
use Exception;
use ncc\ncc;
use ncc\Utilities\Console;
use ncc\Utilities\Resolver;
class Main
@ -28,7 +29,7 @@
switch(strtolower($args['ncc-cli']))
{
default:
print('Unknown command ' . strtolower($args['ncc-cli']) . PHP_EOL);
Console::out('Unknown command ' . strtolower($args['ncc-cli']) . PHP_EOL);
exit(1);
case 'credential':
@ -43,7 +44,7 @@
}
catch(Exception $e)
{
print('Error: ' . $e->getMessage() . ' (Code: ' . $e->getCode() . ')' . PHP_EOL);
Console::out('Error: ' . $e->getMessage() . ' (Code: ' . $e->getCode() . ')' . PHP_EOL);
exit(1);
}

View file

@ -0,0 +1,65 @@
<?php
namespace ncc\CLI;
use ncc\Abstracts\Scopes;
use ncc\Exceptions\AccessDeniedException;
use ncc\Objects\CliHelpSection;
use ncc\Utilities\Console;
use ncc\Utilities\Resolver;
class ProjectMenu
{
/**
* Displays the main help menu
*
* @param $args
* @return void
* @throws AccessDeniedException
*/
public static function start($args): void
{
if(isset($args['create']))
{
self::createProject($args);
}
self::displayOptions();
exit(0);
}
/**
* @param $args
* @return void
* @throws AccessDeniedException
*/
public static function createProject($args): void
{
Console::out('end' . PHP_EOL);
exit(0);
}
/**
* Displays the main options section
*
* @return void
*/
private static function displayOptions(): void
{
$options = [
new CliHelpSection(['help'], 'Displays this help menu about the value command'),
new CliHelpSection(['create'], 'Creates a new NCC project'),
];
$options_padding = \ncc\Utilities\Functions::detectParametersPadding($options) + 4;
Console::out('Usage: ncc project {command} [options]');
Console::out('Options:' . PHP_EOL);
foreach($options as $option)
{
Console::out(' ' . $option->toString($options_padding) . PHP_EOL);
}
print(PHP_EOL);
}
}

View file

@ -126,6 +126,21 @@
// Finally create project.json
$Project->toFile($this->ProjectPath . DIRECTORY_SEPARATOR . 'project.json');
// And create the project directory for additional assets/resources
$Folders = [
$this->ProjectPath . DIRECTORY_SEPARATOR . 'ncc',
$this->ProjectPath . DIRECTORY_SEPARATOR . 'ncc' . DIRECTORY_SEPARATOR . 'cache',
$this->ProjectPath . DIRECTORY_SEPARATOR . 'ncc' . DIRECTORY_SEPARATOR . 'config',
];
foreach($Folders as $folder)
{
if(file_exists($folder) == false)
{
mkdir($folder);
}
}
// Process options
foreach($options as $option)
{

View file

@ -2,6 +2,9 @@
namespace ncc\Objects;
use ncc\Abstracts\ConsoleColors;
use ncc\Utilities\Console;
class CliHelpSection
{
/**
@ -80,7 +83,7 @@
*
* @return string
*/
public function toString(int $param_padding=0)
public function toString(int $param_padding=0, bool $basic=false)
{
$out = [];
@ -88,11 +91,25 @@
{
if($param_padding > 0)
{
$out[] .= str_pad(implode(' ', $this->Parameters), $param_padding, ' ', STR_PAD_RIGHT);
$result = str_pad(implode(' ', $this->Parameters), $param_padding, ' ', STR_PAD_RIGHT);
if($basic == false)
{
$result = Console::formatColor($result, ConsoleColors::Green);
}
$out[] .= $result;
}
else
{
$out[] .= implode(' ', $this->Parameters);
if($basic)
{
$out[] .= implode(' ', $this->Parameters);
}
else
{
$out[] .= Console::formatColor(implode(' ', $this->Parameters), ConsoleColors::Green);
}
}
}

View file

@ -0,0 +1,112 @@
<?php
namespace ncc\Utilities;
use ncc\Abstracts\ConsoleColors;
class Console
{
/**
* Inline Progress bar, created by dealnews.com.
*
* // TODO: Add non-inline option
* @copyright Copyright (c) 2010, dealnews.com, Inc. All rights reserved.
* @param int $value
* @param int $total
* @param int $size
* @param array $flags
* @return void
*/
public static function inlineProgressBar(int $value, int $total, int $size=38, array $flags=[])
{
static $start_time;
// if we go over our bound, just ignore it
if($value > $total) return;
if(empty($start_time)) $start_time=time();
$now = time();
$perc=(double)($value/$total);
$bar=floor($perc*$size);
$status_bar="\r[ ";
$status_bar.=str_repeat("=", $bar);
if($bar<$size){
$status_bar.=">";
$status_bar.=str_repeat(" ", $size-$bar);
} else {
$status_bar.="=";
}
$disp=number_format($perc*100, 0);
$status_bar.=" ] $disp% $value/$total";
$rate = ($now-$start_time)/$value;
$left = $total - $value;
$eta = round($rate * $left, 2);
$elapsed = $now - $start_time;
$status_bar.= " remaining: ".number_format($eta)." sec. elapsed: ".number_format($elapsed)." sec.";
echo "$status_bar ";
flush();
// when done, send a newline
if($value == $total) {
echo "\n";
}
}
/**
* Simple output function
*
* @param string $message
* @param bool $newline
* @return void
*/
public static function out(string $message, bool $newline=true)
{
if($newline)
{
print($message . PHP_EOL);
return;
}
print($message);
}
/**
* Formats the text to have a different color and returns the formatted value
*
* @param string $input The input of the text value
* @param string $color_code The color code of the escaped character (\e[91m)
* @param bool $persist If true, the formatting will terminate in the default color
* @return string
*/
public static function formatColor(string $input, string $color_code, bool $persist=true): string
{
if($persist)
{
return $color_code . $input . ConsoleColors::Default;
}
return $color_code . $input;
}
/**
* Prints out a warning output
*
* @param string $message
* @param bool $newline
* @return void
*/
public static function outWarning(string $message, bool $newline=true)
{
self::out(self::formatColor(ConsoleColors::Yellow, 'Warning: ') . $message, $newline);
}
}