CLI class

Framework\CLI\CLI

Class CLI.

Defined in libraries/cli/src/CLI.php

Properties #

$reset
-

$reset

protected static string $reset = ''

Methods #

beep()
Performs audible beep alarms.
box()
Writes a message box.
clear()
Clear the terminal screen.
error()
Writes a message to STDERR and optionally exit with a custom code.
getInput()
Get user input.
getWidth()
Get the screen width.
info()
Writes an informational message.
isWindows()
Tells if it is running on a Windows OS.
liveLine()
Creates a "live line".
newLine()
Prints a new line in the output.
path()
Join path segments using the current platform separator.
prompt()
Prompt a question.
secret()
Prompt a question with secret answer.
strlen()
Calculate the multibyte length of a text without style characters.
style()
Applies styles to a text.
success()
Writes a success message.
supportsAnsi()
Tells if the current terminal likely supports ANSI escape sequences.
table()
Creates a well formatted table.
wrap()
Displays text wrapped to a certain width.
write()
Write a text in the output.

beep()

public static function beep(int $times = 1, int $usleep = 0): void

Performs audible beep alarms.

Parameters
  • int $times - How many times should the beep be played
  • int $usleep - Interval in microseconds
Returns void

box()

public static function box(array|string $lines, \Framework\CLI\Styles\BackgroundColor|string $background = \Framework\CLI\Styles\BackgroundColor::black, \Framework\CLI\Styles\ForegroundColor|string $color = \Framework\CLI\Styles\ForegroundColor::white): void

Writes a message box.

Parameters
  • array|string $lines - One line as string or multi-lines as array
  • \Framework\CLI\Styles\BackgroundColor|string $background - Background color
  • \Framework\CLI\Styles\ForegroundColor|string $color - Foreground color
Returns void

clear()

public static function clear(): void

Clear the terminal screen.

Returns void

error()

public static function error(string $message, ?int $exitCode = 1): void

Writes a message to STDERR and optionally exit with a custom code.

Parameters
  • string $message - The error message
  • ?int $exitCode - Set null to do not exit
Returns void

getInput()

public static function getInput(string $prepend = ''): string

Get user input.

NOTE: It is possible pass multiple lines ending each line with a backslash.

Parameters
  • string $prepend - Text prepended in the input. Used internally to allow multiple lines
Returns string - Returns the user input

getWidth()

public static function getWidth(int $default = 80): int

Get the screen width.

Parameters
  • int $default
Returns int

info()

public static function info(string $text): void

Writes an informational message.

Parameters
  • string $text - The message to be written
Returns void

isWindows()

public static function isWindows(): bool

Tells if it is running on a Windows OS.

Returns bool

liveLine()

public static function liveLine(string $text, bool $finalize = false): void

Creates a "live line".

Erase the current line, move the cursor to the beginning of the line and
writes a text.

Parameters
  • string $text - The text to be written
  • bool $finalize - If true the "live line" activity ends, creating a new line after the text
Returns void

newLine()

public static function newLine(int $lines = 1): void

Prints a new line in the output.

Parameters
  • int $lines - Number of lines to be printed
Returns void

path()

public static function path(string ...$segments): string

Join path segments using the current platform separator.

Parameters
  • string $segments
Returns string

prompt()

public static function prompt(string $question, array|string|null $options = NULL): string

Prompt a question.

Parameters
  • string $question - The question to prompt
  • array|string|null $options - Answer options. If an array is set, the default answer is the first value. If is a string, it will be the default.
Returns string - The answer

secret()

public static function secret(string $question): string

Prompt a question with secret answer.

Parameters
  • string $question - The question to prompt
Returns string - The secret answer

strlen()

public static function strlen(string $text): int

Calculate the multibyte length of a text without style characters.

Parameters
  • string $text - The text being checked for length
Returns int

style()

public static function style(string $text, \Framework\CLI\Styles\ForegroundColor|string|null $color = NULL, \Framework\CLI\Styles\BackgroundColor|string|null $background = NULL, array $formats = array ( )): string

Applies styles to a text.

Parameters
  • string $text - The text to be styled
  • \Framework\CLI\Styles\ForegroundColor|string|null $color - Foreground color
  • \Framework\CLI\Styles\BackgroundColor|string|null $background - Background color
  • array $formats - The text formats
Returns string - Returns the styled text
throws
ValueError For invalid color, background or format

success()

public static function success(string $text): void

Writes a success message.

Parameters
  • string $text - The message to be written
Returns void

supportsAnsi()

public static function supportsAnsi(): bool

Tells if the current terminal likely supports ANSI escape sequences.

Returns bool

table()

public static function table(array $tbody, array $thead = array ( )): void

Creates a well formatted table.

Parameters
  • array $tbody - Table body rows
  • array $thead - Table head fields
Returns void

wrap()

public static function wrap(string $text, ?int $width = NULL): string

Displays text wrapped to a certain width.

Parameters
  • string $text
  • ?int $width
Returns string - Returns the wrapped text

write()

public static function write(string $text, \Framework\CLI\Styles\ForegroundColor|string|null $color = NULL, \Framework\CLI\Styles\BackgroundColor|string|null $background = NULL, ?int $width = NULL): void

Write a text in the output.

Optionally with styles and width wrapping.

Parameters
  • string $text - The text to be written
  • \Framework\CLI\Styles\ForegroundColor|string|null $color - Foreground color
  • \Framework\CLI\Styles\BackgroundColor|string|null $background - Background color
  • ?int $width - Width to wrap the text. Null to do not wrap.
Returns void