Aplus Framework Docs

CLI
in package

Class CLI.

Tags
see
https://en.wikipedia.org/wiki/ANSI_escape_code

Table of Contents

$reset  : string
beep()  : void
Performs audible beep alarms.
box()  : void
Writes a message box.
clear()  : void
Clear the terminal screen.
error()  : void
Writes a message to STDERR and optionally exit with a custom code.
getInput()  : string
Get user input.
getWidth()  : int
Get the screen width.
isWindows()  : bool
Tells if it is running on a Windows OS.
liveLine()  : void
Creates a "live line".
newLine()  : void
Prints a new line in the output.
prompt()  : string
Prompt a question.
secret()  : string
Prompt a question with secret answer.
strlen()  : int
Calculate the multibyte length of a text without style characters.
style()  : string
Applies styles to a text.
table()  : void
Creates a well formatted table.
wrap()  : string
Displays text wrapped to a certain width.
write()  : void
Write a text in the output.

Properties

$reset

protected static string $reset = "\x1b[0m"

Methods

beep()

Performs audible beep alarms.

public static beep([int $times = 1 ], int $usleep) : void
Parameters
$times : int = 1

How many times should the beep be played

$usleep : int

Interval in microseconds

Return values
void

box()

Writes a message box.

public static box(array<int, string>|string $lines[, BackgroundColor|string $background = BackgroundColor::black ][, ForegroundColor|string $color = ForegroundColor::white ]) : void
Parameters
$lines : array<int, string>|string

One line as string or multi-lines as array

$background : BackgroundColor|string = BackgroundColor::black

Background color

$color : ForegroundColor|string = ForegroundColor::white

Foreground color

Return values
void

clear()

Clear the terminal screen.

public static clear() : void
Return values
void

error()

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

public static error(string $message[, int|null $exitCode = 1 ]) : void
Parameters
$message : string

The error message

$exitCode : int|null = 1

Set null to do not exit

Return values
void

getInput()

Get user input.

public static getInput([string $prepend = '' ]) : string

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

Parameters
$prepend : string = ''

Text prepended in the input. Used internally to allow multiple lines

Return values
string

Returns the user input

getWidth()

Get the screen width.

public static getWidth([int $default = 80 ]) : int
Parameters
$default : int = 80
Return values
int

isWindows()

Tells if it is running on a Windows OS.

public static isWindows() : bool
Return values
bool

liveLine()

Creates a "live line".

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

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

Parameters
$text : string

The text to be written

$finalize : bool = false

If true the "live line" activity ends, creating a new line after the text

Return values
void

newLine()

Prints a new line in the output.

public static newLine([int $lines = 1 ]) : void
Parameters
$lines : int = 1

Number of lines to be printed

Return values
void

prompt()

Prompt a question.

public static prompt(string $question[, array<int, string>|string|null $options = null ]) : string
Parameters
$question : string

The question to prompt

$options : array<int, string>|string|null = null

Answer options. If an array is set, the default answer is the first value. If is a string, it will be the default.

Return values
string

The answer

strlen()

Calculate the multibyte length of a text without style characters.

public static strlen(string $text) : int
Parameters
$text : string

The text being checked for length

Return values
int

style()

Applies styles to a text.

public static style(string $text[, ForegroundColor|string|null $color = null ][, BackgroundColor|string|null $background = null ][, array<string|int, \Framework\CLI\Styles\Format|string> $formats = [] ]) : string
Parameters
$text : string

The text to be styled

$color : ForegroundColor|string|null = null

Foreground color

$background : BackgroundColor|string|null = null

Background color

$formats : array<string|int, \Framework\CLI\Styles\Format|string> = []

The text formats

Tags
throws
ValueError

For invalid color, background or format

Return values
string

Returns the styled text

table()

Creates a well formatted table.

public static table(array<string|int, array<string|int, scalar|\Stringable>> $tbody[, array<string|int, scalar|\Stringable> $thead = [] ]) : void
Parameters
$tbody : array<string|int, array<string|int, scalar|\Stringable>>

Table body rows

$thead : array<string|int, scalar|\Stringable> = []

Table head fields

Return values
void

wrap()

Displays text wrapped to a certain width.

public static wrap(string $text[, int|null $width = null ]) : string
Parameters
$text : string
$width : int|null = null
Return values
string

Returns the wrapped text

write()

Write a text in the output.

public static write(string $text[, ForegroundColor|string|null $color = null ][, BackgroundColor|string|null $background = null ][, int|null $width = null ]) : void

Optionally with styles and width wrapping.

Parameters
$text : string

The text to be written

$color : ForegroundColor|string|null = null

Foreground color

$background : BackgroundColor|string|null = null

Background color

$width : int|null = null

Width to wrap the text. Null to do not wrap.

Return values
void

Search results