Console

Type
Class
Namespace
craft\helpers
Inherits
craft\helpers\Console » yii\helpers\Console (opens new window) » yii\helpers\BaseConsole (opens new window)
Since
3.0.0

Console helper

View source (opens new window)

# Public Methods

Method Description
ansiColorizedSubstr() (opens new window) Returns the portion with ANSI color codes of string specified by the start and length parameters.
ansiFormat() (opens new window) Will return a string formatted with the given ANSI style.
ansiFormatCode() (opens new window) Returns the ANSI format code.
ansiStrlen() (opens new window) Returns the length of the string without ANSI color codes.
ansiStrwidth() (opens new window) Returns the width of the string without ANSI color codes.
ansiToHtml() (opens new window) Converts an ANSI formatted string to HTML.
beginAnsiFormat() (opens new window) Echoes an ANSI format code that affects the formatting of any text that is printed afterwards.
clearLine() (opens new window) Clears the line, the cursor is currently on by sending ANSI control code EL with argument 2 to the terminal.
clearLineAfterCursor() (opens new window) Clears text from cursor position to the end of the line by sending ANSI control code EL with argument 0 to the terminal.
clearLineBeforeCursor() (opens new window) Clears text from cursor position to the beginning of the line by sending ANSI control code EL with argument 1 to the terminal.
clearScreen() (opens new window) Clears entire screen content by sending ANSI control code ED with argument 2 to the terminal.
clearScreenAfterCursor() (opens new window) Clears text from cursor to the end of the screen by sending ANSI control code ED with argument 0 to the terminal.
clearScreenBeforeCursor() (opens new window) Clears text from cursor to the beginning of the screen by sending ANSI control code ED with argument 1 to the terminal.
confirm() (opens new window) Asks user to confirm by typing y or n.
endAnsiFormat() (opens new window) Resets any ANSI format set by previous method beginAnsiFormat() (opens new window) Any output after this will have default text format.
endProgress() (opens new window) Ends a progress bar that has been started by startProgress() (opens new window).
ensureProjectConfigFileExists() Ensures that the project config YAML files exist if they’re supposed to
error() (opens new window) Prints text to STDERR appended with a carriage return (PHP_EOL).
errorSummary() (opens new window) Generates a summary of the validation errors.
escape() (opens new window) Escapes % so they don't get interpreted as color codes when the string is parsed by renderColoredString() (opens new window).
getScreenSize() (opens new window) Returns terminal screen size.
hideCursor() (opens new window) Hides the cursor by sending ANSI DECTCEM code ?25l to the terminal.
input() (opens new window) Asks the user for input. Ends when the user types a carriage return (PHP_EOL). Optionally, It also provides a prompt.
isColorEnabled() Returns whether color is enabled.
isRunningOnWindows() (opens new window) Returns true if the console is running on windows.
markdownToAnsi() Converts Markdown to be better readable in console environments by applying some ANSI format.
moveCursorBackward() (opens new window) Moves the terminal cursor backward by sending ANSI control code CUB to the terminal.
moveCursorDown() (opens new window) Moves the terminal cursor down by sending ANSI control code CUD to the terminal.
moveCursorForward() (opens new window) Moves the terminal cursor forward by sending ANSI control code CUF to the terminal.
moveCursorNextLine() (opens new window) Moves the terminal cursor to the beginning of the next line by sending ANSI control code CNL to the terminal.
moveCursorPrevLine() (opens new window) Moves the terminal cursor to the beginning of the previous line by sending ANSI control code CPL to the terminal.
moveCursorTo() (opens new window) Moves the cursor to an absolute position given as column and row by sending ANSI control code CUP or CHA to the terminal.
moveCursorUp() (opens new window) Moves the terminal cursor up by sending ANSI control code CUU to the terminal.
output() (opens new window) Prints text to STDOUT appended with a carriage return (PHP_EOL).
outputCommand() Outputs a terminal command.
outputWarning() Outputs a warning.
prompt() (opens new window) Prompts the user for input and validates it.
renderColoredString() (opens new window) Converts a string to ansi formatted by replacing patterns like %y (for yellow) with ansi control codes.
restoreCursorPosition() (opens new window) Restores the cursor position saved with saveCursorPosition() (opens new window) by sending ANSI control code RCP to the terminal.
saveCursorPosition() (opens new window) Saves the current cursor position by sending ANSI control code SCP to the terminal.
scrollDown() (opens new window) Scrolls whole page down by sending ANSI control code SD to the terminal.
scrollUp() (opens new window) Scrolls whole page up by sending ANSI control code SU to the terminal.
select() (opens new window) Gives the user an option to choose from. Giving '?' as an input will show a list of options to choose from and their explanations.
showCursor() (opens new window) Will show a cursor again when it has been hidden by hideCursor() (opens new window) by sending ANSI DECTCEM code ?25h to the terminal.
startProgress() (opens new window) Starts display of a progress bar on screen.
stderr() (opens new window) Prints a string to STDERR.
stdin() (opens new window) Gets input from STDIN and returns a string right-trimmed for EOLs.
stdout() Prints a string to STDOUT.
streamSupportsAnsiColors() (opens new window) Returns true if the stream supports colorization. ANSI colors are disabled if not supported by the stream.
stripAnsiFormat() (opens new window) Strips ANSI control codes from a string.
table() Outputs a table.
updateProgress() (opens new window) Updates a progress bar that has been started by startProgress() (opens new window).
wrapText() (opens new window) Word wrap text with indentation to fit the screen size.
xtermBgColor() (opens new window) Returns the ansi format code for xterm background color.
xtermFgColor() (opens new window) Returns the ansi format code for xterm foreground color.

# ensureProjectConfigFileExists()

Since
3.5.0

Ensures that the project config YAML files exist if they’re supposed to

View source (opens new window)

# isColorEnabled()

Since
3.0.38

Returns whether color is enabled.

View source (opens new window)

Returns

boolean (opens new window)

# markdownToAnsi()

Converts Markdown to be better readable in console environments by applying some ANSI format.

View source (opens new window)

Arguments

Returns

string (opens new window) – The parsed result as ANSI formatted string.

# outputCommand()

Since
3.0.38

Outputs a terminal command.

View source (opens new window)

Arguments

# outputWarning()

Since
3.0.38

Outputs a warning.

View source (opens new window)

Arguments

# stdout()

Prints a string to STDOUT.

You may optionally format the string with ANSI codes by passing additional parameters using the constants defined in yii\helpers\Console (opens new window). Example:

Console::stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);

View source (opens new window)

Arguments

Returns

integer (opens new window), false (opens new window) – Number of bytes printed or false on error

# table()

Since
3.7.23

Outputs a table.

$data should be set to an array of nested arrays. Each nested array should contain values for the same keys found in $headers.

Header and data values can be expressed as a string (the raw value), or an array that begins with the raw value, followed by any of the following keys:

$options supports the following:

  • maxSize – The maximum number of characters to show within each cell (defaults to 80).
  • rowPrefix - any characters that should be output before each row (defaults to four spaces)
  • `rowSuffix – any characters that should be output after each row
  • colors – Whether to format cells per their format keys (defaults to streamSupportsAnsiColors() (opens new window)).

View source (opens new window)

Arguments

Throws

# Constants

Constant Description
BG_BLACK
BG_BLUE
BG_CYAN
BG_GREEN
BG_GREY
BG_PURPLE
BG_RED
BG_YELLOW
BLINK
BOLD
CONCEALED
CROSSED_OUT
ENCIRCLED
FG_BLACK
FG_BLUE
FG_CYAN
FG_GREEN
FG_GREY
FG_PURPLE
FG_RED
FG_YELLOW
FRAMED
ITALIC
NEGATIVE
NORMAL
OVERLINED
RESET
UNDERLINE