Luminova Framework

Command Terminal

Last updated: 2024-06-08 11:32:45

The Luminova Terminal class for NovaKit is a powerful utility designed to enhance the command-line interface (CLI) interaction for developers leveraging the Luminova framework. Designed to enhance CLI tool development and application operations within the framework, it offers an array of functionalities and utilities.


  • Class namespace: \Luminova\Command\Terminal

constructor

Initialize command line instance before running any commands.

new Terminal()

Methods

waiting

Show a waiting countdown, intentionally freeze screen while waiting or ask user for a key press to continue.

protected static waiting(int $seconds, bool $countdown = false): void

Examples

Parameters:

ParameterTypeDescription
$secondsintNumber of seconds for waiting
$countdownboolShow waiting countdown

progress

Displays a progress bar on the CLI.

protected static progress(int|bool $progressLine = 1, int|null $progressCount = 10, bool $beep = true): float|int

Progress should be called in a loop otherwise use watcher() method.

Parameters:

ParameterTypeDescription
$progressLineint|boolCurrent loop index number or false to terminate the progress bar
$progressCountint|nullTotal count of progress bar to show or null to on termination
$beepboolBeep when progress is completed, default is true

watcher

Displays a and watch progress bar on the CLI with a callback functions, this method shouldn't be called in a loop, you can pass your callable to execute in $onProgress callback function.

protected static watcher(int $progressCount, ?Closure $onFinish = null, ?Closure $onProgress = null, bool $beep = true): void

Parameters:

ParameterTypeDescription
$progressCountintTotal count of progress bar to show
$onFinish?ClosureExecute callback when progress finished
$onProgress?ClosureExecute callback on each progress step
$beepboolBeep when progress is completed, default is true

This is useful when you just want to display a progress bar and execute next method when it finished


execute

This method allows you to execute a system command.

protected static execute(string $command): mixed

Parameters:

ParameterTypeDescription
$commandstringThe command to execute

Return Value

mixed - Returns the output of command as an array of lines, or STATUS_ERROR on failure


beeps

Trigger beeps a certain number of times.

protected static beeps(int $num = 1): void

Parameters:

ParameterTypeDescription
$numintThe number of times to beep

prompt

Prompt for user to input or select an option, pass options as an array ["YES", "NO].

protected static prompt(string $message, array $options = [], string|null $validations = null, bool $silent = false): string

Parameters:

ParameterTypeDescription
$messagestringPrompt message
$optionsarrayOptions to prompt selection,
$validationsstring|nullValidation rules
$silentboolPrint validation failure message parameter is true (default: false)

Return Value:

string - The user input

You can make a colored options by use the array key for color name ["green" => "YES","red" => "NO"]


chooser

Prompt for multi choice selection, display array index key as the option identifier to select.

protected static chooser(string $text, array $options, bool $required = false): array<string|int,mixed>

Parameters:

ParameterTypeDescription
$textstringDisplay text description for your multiple options
$optionsarrayA list of options ['male' => 'Male', 'female' => 'Female] or ['male', 'female']
$requiredboolRequire user to choose any option else the first array will be return as default

Return Value:

array - The selected array keys and values

Throws:

If you use associative array users will see index key instead.


wrap

Wrap it with padding left and width to a maximum

public static wrap(string|null $string = null, int $max, int $leftPadding): string

Parameters:

ParameterTypeDescription
$stringstring|nullstring to write
$maxintmaximum width
$leftPaddingintleft padding

Return Value:

string - Wrapped message.


card

Create a card text.

public static card(string $text, int|null $padding = null): string

Parameters:

ParameterTypeDescription
$textstringstring to pad
$paddingint|nullmaximum padding

Return Value:

string - Return beautiful card text.


getWidth

Attempts to determine the width of the viewable CLI window.

protected static getWidth(int $default = 80): int

Parameters:

ParameterTypeDescription
$defaultintOptional default width (default: 80)

Return Value:

int - Width or fallback to default


getHeight

Attempts to determine the height of the viewable CLI window.

protected static getHeight(int $default = 24): int

Parameters:

ParameterTypeDescription
$defaultintOptional default height (default: 24)

Return Value:

int - Height or fallback to default


input

Get user input from the shell, after requesting for user to type or select an option.

protected static input(string|null $prompt = null): string

Parameters:

ParameterTypeDescription
$promptstring|nullYou may specify a string to prompt the user after they have typed.

Return Value:

string - Return user inputed string.


validate

Input validation on prompts

protected static validate(string $value, array $rules): bool

Parameters:

ParameterTypeDescription
$valuestringInput value
$rulesarrayValidation rules

success

Display success text on CLI

public static success(string $text, string|null $foreground = 'white', string|null $background = 'green'): void

Parameters:

ParameterTypeDescription
$textstringError message
$foregroundstring|nullForeground color name
$backgroundstring|nullOptional background color name

error

Display error message on CLI

public static error(string $text, string|null $foreground = 'white', string|null $background = 'red'): void

Parameters:

ParameterTypeDescription
$textstringError message
$foregroundstring|nullForeground color name
$backgroundstring|nullOptional background color name

writeln

Print message to CLI in a new line.

public static writeln(string $text = '', string|null $foreground = null, string|null $background = null): void

Parameters:

ParameterTypeDescription
$textstringText to display
$foregroundstring|nullOptional foreground color name
$backgroundstring|nullOptional background color name

write

Print a message to CLI output without a newline.

public static write(string $text = '', string|null $foreground = null, string|null $background = null): void

Parameters:

ParameterTypeDescription
$textstringText to display
$foregroundstring|nullOptional foreground color name
$backgroundstring|nullOptional background color name

print

Echo / output a message to CLI.

public static print(string $text, string|null $foreground = null, string|null $background = null): void

Parameters:

ParameterTypeDescription
$textstringstring to output
$foregroundstring|nullOptional foreground color name
$backgroundstring|nullOptional background color name

fwrite

Write message to resource handler or output text if not in cli mode

protected static fwrite(string $text, resource $handle = STDOUT): void

Parameters:

ParameterTypeDescription
$textstringstring to output or write
$handleresourceresource handler

oops

The oops method can be use to display default error message "Unknown command "foo" not found".

protected static oops(string $command, string|null $color = 'red'): int

Parameters:

ParameterTypeDescription
$commandstringThe executed command.
$colorstring|nullThe command text color (default: red)

Return Value

int - Return status code STATUS_ERROR.


clear

Clears the CLI screen of output

public static clear(): void

flush

Clears CLI output to update new text.

public static flush(): void

color

Returns the given text with the correct color codes for a foreground and optionally a background color.

public static color(string $text, string $foreground, string|null $background = null, int|null $format = null): string

Parameters:

ParameterTypeDescription
$textstringText to color
$foregroundstringForeground color name
$backgroundstring|nullOptional background color name
$formatint|nullOptionally apply text formatting.

Return Value:

string - A colored text if color is supported


newLine

Pring new lines to CLI

public static newLine(int $count = 1): void

Parameters:

ParameterTypeDescription
$countintCount of new lines to create

streamSupports

Checks whether the current stream resource supports or refers to a valid terminal type device.

public static streamSupports(string $function, resource $resource): bool

Parameters:

ParameterTypeDescription
$functionstringFunction name to check
$resourceresourceResource to handle STDIN/STDOUT

Return Value:

bool - if the stream resource is supported


explain

Register command line queries to make available in using methods like getOptions(), getArgument() etc.

public static explain(array $options): void

Parameters:

ParameterTypeDescription
$optionsarrayCommand arguments received in CommandController -> run() method

getArgument

Get command argument by index number

public static getArgument(int $index): mixed

Parameters:

ParameterTypeDescription
$indexintArgument Index position

Return Value:

mixed - Passed argument or null.


getArguments

Get command arguments

public static getArguments(): array

Return Value:

array - Passed arguments.


getCommand

Get the passed command name.

public static getCommand(): string|null

Return Value:

string||null - Command name or null.


getCaller

Get the full passed command, options and arguments as its original form.

public static getCaller(): string|null

Return Value:

string||null - Command string or null.


getOption

Get options value from command arguments.If option flag is passed with an empty value true will be return else default or false.

public static getOption(string $key, string $default = false): mixed

Parameters:

ParameterTypeDescription
$keystringOption key to retrieve
$defaultstringDefault value to return (default: false)

Return Value:

array - Option value or default value.


getOptions

Returns array of command options and arguments.

public static getOptions(): array

Return Value:

array - Options and arguments passed to cli as array.


getQueries

Returns the raw array of requested query command structure.

public static getQueries(): array

This is a complied array of passed query which has been processed for final usage by Luminova framework.

Return Value:

array - Complied structured command queries.


getQuery

Gets complied command queries by it's array key name, keys (arguments, command, caller, options).

public static getQuery(string $name): mixed

Parameters:

ParameterTypeDescription
$namestringQuery key name

Return Value:

mixed - Query values or null if it doesn't exists.


isColorSupported

Check if the your CLI terminal supports colors.On windows you must specify the stream resource (STDIN, STDOUT or STDERR).

public static isColorSupported(resource $resource = STDOUT): bool

Parameters:

ParameterTypeDescription
$resourceresourceThe stream resource STDIN/STDOUT/STDERR, (default: STDOUT)

Return Value:

bool - Return true if supported, false otherwise.


isMacTerminal

Determine whether if the current terminal is mac os terminal.

public static isMacTerminal(): bool

Return Value:

bool - Return true if mac terminal, false otherwise.


isWindowsTerminal

Determine whether if the current terminal is windows os terminal by passing the stream resource.

public static isWindowsTerminal(resource $resource): bool

Parameters:

ParameterTypeDescription
$resourceresourceSTDIN/STDOUT

Return Value:

bool - Return true if windows terminal, false otherwise.


hasCommand

Checks whether framework has the requested command.

public static hasCommand(string $command): bool

Parameters:

ParameterTypeDescription
$commandstringCommand name to check

Return Value:

bool - Return true if command exist, false otherwise.


header

Print NovaKit Command line header information

public static header(): void

isHelp

Check if command is help command.

public static isHelp(string $command): boll

Parameters:

ParameterTypeDescription
$commandstringCommand name to check

Return Value:

bool - Return true if command is help, false otherwise.