CLI
in package
Class CLI.
Tags
Table of Contents
- BG_BLACK = 'black'
- Background color "black".
- BG_BLUE = 'blue'
- Background color "blue".
- BG_BRIGHT_BLACK = 'bright_black'
- Background color "bright black".
- BG_BRIGHT_BLUE = 'bright_blue'
- Background color "bright blue".
- BG_BRIGHT_CYAN = 'bright_cyan'
- Background color "bright cyan".
- BG_BRIGHT_GREEN = 'bright_green'
- Background color "bright green".
- BG_BRIGHT_MAGENTA = 'bright_magenta'
- Background color "bright magenta".
- BG_BRIGHT_RED = 'bright_red'
- Background color "bright red".
- BG_BRIGHT_YELLOW = 'bright_yellow'
- Background color "bright yellow".
- BG_CYAN = 'cyan'
- Background color "cyan".
- BG_GREEN = 'green'
- Background color "green".
- BG_MAGENTA = 'magenta'
- Background color "magenta".
- BG_RED = 'red'
- Background color "red".
- BG_WHITE = 'white'
- Background color "white".
- BG_YELLOW = 'yellow'
- Background color "yellow".
- FG_BLACK = 'black'
- Foreground color "black".
- FG_BLUE = 'blue'
- Foreground color "blue".
- FG_BRIGHT_BLACK = 'bright_black'
- Foreground color "bright black".
- FG_BRIGHT_BLUE = 'bright_blue'
- Foreground color "bright blue".
- FG_BRIGHT_CYAN = 'bright_cyan'
- Foreground color "bright cyan".
- FG_BRIGHT_GREEN = 'bright_green'
- Foreground color "bright green".
- FG_BRIGHT_MAGENTA = 'bright_magenta'
- Foreground color "bright magenta".
- FG_BRIGHT_RED = 'bright_red'
- Foreground color "bright red".
- FG_BRIGHT_WHITE = 'bright_white'
- Foreground color "bright white".
- FG_BRIGHT_YELLOW = 'bright_yellow'
- Foreground color "bright yellow".
- FG_CYAN = 'cyan'
- Foreground color "cyan".
- FG_GREEN = 'green'
- Foreground color "green".
- FG_MAGENTA = 'magenta'
- Foreground color "magenta".
- FG_RED = 'red'
- Foreground color "red".
- FG_WHITE = 'white'
- Foreground color "white".
- FG_YELLOW = 'yellow'
- Foreground color "yellow".
- FM_BOLD = 'bold'
- SGR format "bold".
- FM_CONCEAL = 'conceal'
- SGR format "conceal".
- FM_CROSSED_OUT = 'crossed_out'
- SGR format "crossed out".
- FM_DOUBLY_UNDERLINE = 'doubly_underline'
- SGR format "doubly underline".
- FM_ENCIRCLED = 'encircled'
- SGR format "encircled".
- FM_FAINT = 'faint'
- SGR format "faint".
- FM_FRAKTUR = 'fraktur'
- SGR format "fraktur".
- FM_ITALIC = 'italic'
- SGR format "italic".
- FM_PRIMARY_FONT = 'primary_font'
- SGR format "primary font".
- FM_RAPID_BLINK = 'rapid_blink'
- SGR format "rapid blink".
- FM_REVERSE_VIDEO = 'reverse_video'
- SGR format "reverse video".
- FM_SLOW_BLINK = 'slow_blink'
- SGR format "slow blink".
- FM_UNDERLINE = 'underline'
- SGR format "underline".
- $backgroundColors : array<string|int, mixed>
- $foregroundColors : array<string|int, mixed>
- $formats : array<string|int, mixed>
- $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.
Constants
BG_BLACK
Background color "black".
public
string
BG_BLACK
= 'black'
BG_BLUE
Background color "blue".
public
string
BG_BLUE
= 'blue'
BG_BRIGHT_BLACK
Background color "bright black".
public
string
BG_BRIGHT_BLACK
= 'bright_black'
BG_BRIGHT_BLUE
Background color "bright blue".
public
string
BG_BRIGHT_BLUE
= 'bright_blue'
BG_BRIGHT_CYAN
Background color "bright cyan".
public
string
BG_BRIGHT_CYAN
= 'bright_cyan'
BG_BRIGHT_GREEN
Background color "bright green".
public
string
BG_BRIGHT_GREEN
= 'bright_green'
BG_BRIGHT_MAGENTA
Background color "bright magenta".
public
string
BG_BRIGHT_MAGENTA
= 'bright_magenta'
BG_BRIGHT_RED
Background color "bright red".
public
string
BG_BRIGHT_RED
= 'bright_red'
BG_BRIGHT_YELLOW
Background color "bright yellow".
public
string
BG_BRIGHT_YELLOW
= 'bright_yellow'
BG_CYAN
Background color "cyan".
public
string
BG_CYAN
= 'cyan'
BG_GREEN
Background color "green".
public
string
BG_GREEN
= 'green'
BG_MAGENTA
Background color "magenta".
public
string
BG_MAGENTA
= 'magenta'
BG_RED
Background color "red".
public
string
BG_RED
= 'red'
BG_WHITE
Background color "white".
public
string
BG_WHITE
= 'white'
BG_YELLOW
Background color "yellow".
public
string
BG_YELLOW
= 'yellow'
FG_BLACK
Foreground color "black".
public
string
FG_BLACK
= 'black'
FG_BLUE
Foreground color "blue".
public
string
FG_BLUE
= 'blue'
FG_BRIGHT_BLACK
Foreground color "bright black".
public
string
FG_BRIGHT_BLACK
= 'bright_black'
FG_BRIGHT_BLUE
Foreground color "bright blue".
public
string
FG_BRIGHT_BLUE
= 'bright_blue'
FG_BRIGHT_CYAN
Foreground color "bright cyan".
public
string
FG_BRIGHT_CYAN
= 'bright_cyan'
FG_BRIGHT_GREEN
Foreground color "bright green".
public
string
FG_BRIGHT_GREEN
= 'bright_green'
FG_BRIGHT_MAGENTA
Foreground color "bright magenta".
public
string
FG_BRIGHT_MAGENTA
= 'bright_magenta'
FG_BRIGHT_RED
Foreground color "bright red".
public
string
FG_BRIGHT_RED
= 'bright_red'
FG_BRIGHT_WHITE
Foreground color "bright white".
public
string
FG_BRIGHT_WHITE
= 'bright_white'
FG_BRIGHT_YELLOW
Foreground color "bright yellow".
public
string
FG_BRIGHT_YELLOW
= 'bright_yellow'
FG_CYAN
Foreground color "cyan".
public
string
FG_CYAN
= 'cyan'
FG_GREEN
Foreground color "green".
public
string
FG_GREEN
= 'green'
FG_MAGENTA
Foreground color "magenta".
public
string
FG_MAGENTA
= 'magenta'
FG_RED
Foreground color "red".
public
string
FG_RED
= 'red'
FG_WHITE
Foreground color "white".
public
string
FG_WHITE
= 'white'
FG_YELLOW
Foreground color "yellow".
public
string
FG_YELLOW
= 'yellow'
FM_BOLD
SGR format "bold".
public
string
FM_BOLD
= 'bold'
FM_CONCEAL
SGR format "conceal".
public
string
FM_CONCEAL
= 'conceal'
FM_CROSSED_OUT
SGR format "crossed out".
public
string
FM_CROSSED_OUT
= 'crossed_out'
FM_DOUBLY_UNDERLINE
SGR format "doubly underline".
public
string
FM_DOUBLY_UNDERLINE
= 'doubly_underline'
FM_ENCIRCLED
SGR format "encircled".
public
string
FM_ENCIRCLED
= 'encircled'
FM_FAINT
SGR format "faint".
public
string
FM_FAINT
= 'faint'
FM_FRAKTUR
SGR format "fraktur".
public
string
FM_FRAKTUR
= 'fraktur'
FM_ITALIC
SGR format "italic".
public
string
FM_ITALIC
= 'italic'
FM_PRIMARY_FONT
SGR format "primary font".
public
string
FM_PRIMARY_FONT
= 'primary_font'
FM_RAPID_BLINK
SGR format "rapid blink".
public
string
FM_RAPID_BLINK
= 'rapid_blink'
FM_REVERSE_VIDEO
SGR format "reverse video".
public
string
FM_REVERSE_VIDEO
= 'reverse_video'
FM_SLOW_BLINK
SGR format "slow blink".
public
string
FM_SLOW_BLINK
= 'slow_blink'
FM_UNDERLINE
SGR format "underline".
public
string
FM_UNDERLINE
= 'underline'
Properties
$backgroundColors
protected
static array<string|int, mixed>
$backgroundColors
= ['black' => "\x1b[40m", 'red' => "\x1b[41m", 'green' => "\x1b[42m", 'yellow' => "\x1b[43m", 'blue' => "\x1b[44m", 'magenta' => "\x1b[45m", 'cyan' => "\x1b[46m", 'white' => "\x1b[47m", 'bright_black' => "\x1b[100m", 'bright_red' => "\x1b[101m", 'bright_green' => "\x1b[102m", 'bright_yellow' => "\x1b[103m", 'bright_blue' => "\x1b[104m", 'bright_magenta' => "\x1b[105m", 'bright_cyan' => "\x1b[106m", 'bright_white' => "\x1b[107m"]
$foregroundColors
protected
static array<string|int, mixed>
$foregroundColors
= ['black' => "\x1b[0;30m", 'red' => "\x1b[0;31m", 'green' => "\x1b[0;32m", 'yellow' => "\x1b[0;33m", 'blue' => "\x1b[0;34m", 'magenta' => "\x1b[0;35m", 'cyan' => "\x1b[0;36m", 'white' => "\x1b[0;37m", 'bright_black' => "\x1b[0;90m", 'bright_red' => "\x1b[0;91m", 'bright_green' => "\x1b[0;92m", 'bright_yellow' => "\x1b[0;93m", 'bright_blue' => "\x1b[0;94m", 'bright_magenta' => "\x1b[0;95m", 'bright_cyan' => "\x1b[0;96m", 'bright_white' => "\x1b[0;97m"]
$formats
protected
static array<string|int, mixed>
$formats
= ['bold' => "\x1b[1m", 'faint' => "\x1b[2m", 'italic' => "\x1b[3m", 'underline' => "\x1b[4m", 'slow_blink' => "\x1b[5m", 'rapid_blink' => "\x1b[6m", 'reverse_video' => "\x1b[7m", 'conceal' => "\x1b[8m", 'crossed_out' => "\x1b[9m", 'primary_font' => "\x1b[10m", 'fraktur' => "\x1b[20m", 'doubly_underline' => "\x1b[21m", 'encircled' => "\x1b[52m"]
$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[, string $background = CLI::BG_BLACK ][, string $color = CLI::FG_WHITE ]) : void
Parameters
- $lines : array<int, string>|string
-
One line as string or multi-lines as array
- $background : string = CLI::BG_BLACK
-
Background color. One of the BG_* constants
- $color : string = CLI::FG_WHITE
-
Foreground color. One of the FG_* constants
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
secret()
Prompt a question with secret answer.
public
static secret(string $question) : string
Parameters
- $question : string
-
The question to prompt
Tags
Return values
string —The secret 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[, string|null $color = null ][, string|null $background = null ][, array<int, string> $formats = [] ]) : string
Parameters
- $text : string
-
The text to be styled
- $color : string|null = null
-
Foreground color. One of the FG_* constants
- $background : string|null = null
-
Background color. One of the BG_* constants
- $formats : array<int, string> = []
-
The text format. A list of FM_* constants
Tags
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[, string|null $color = null ][, string|null $background = null ][, int|null $width = null ]) : void
Optionally with styles and width wrapping.
Parameters
- $text : string
-
The text to be written
- $color : string|null = null
-
Foreground color. One of the FG_* constants
- $background : string|null = null
-
Background color. One of the BG_* constants
- $width : int|null = null
-
Width to wrap the text. Null to do not wrap.