HumHub Documentation (unofficial)

ConsoleHelper
in package

Utilities for console tooling.

Provides the following facilities:

  • Colorize strings using markup (e.g., <info>message</info>, <error>message</error>)
  • Write output to a specified stream, optionally with colorization.
  • Write a line of output to a specified stream, optionally with colorization, using the system EOL sequence..
  • Write an error message to STDERR.

Colorization will only occur when expected sequences are discovered, and then, only if the console terminal allows it.

Essentially, provides the bare minimum to allow you to provide messages to the current console.

Table of Contents

Constants

COLOR_GREEN  = "\x1b[32m"
COLOR_RED  = "\x1b[31m"
COLOR_RESET  = "\x1b[0m"
HIGHLIGHT_ERROR  = 'error'
HIGHLIGHT_INFO  = 'info'

Properties

$eol  : string
$highlightMap  : array<string|int, mixed>
$stderr  : resource
$supportsColor  : bool

Methods

__construct()  : mixed
colorize()  : string
Colorize a string for use with the terminal.
write()  : void
writeErrorMessage()  : void
Emit an error message.
writeLine()  : void
detectColorCapabilities()  : bool
formatNewlines()  : string
Ensure newlines are appropriate for the current terminal.

Constants

Properties

$eol

private string $eol = \PHP_EOL

Exists only for testing.

$highlightMap

private array<string|int, mixed> $highlightMap = [self::HIGHLIGHT_INFO => self::COLOR_GREEN, self::HIGHLIGHT_ERROR => self::COLOR_RED]
Tags
psalm-var

array<ConsoleHelper::HIGHLIGHT_*, ConsoleHelper::COLOR_GREEN|ConsoleHelper::COLOR_RED>

$stderr

private resource $stderr = \STDERR

Exists only for testing.

Methods

__construct()

public __construct([resource $resource = STDOUT ]) : mixed
Parameters
$resource : resource = STDOUT

colorize()

Colorize a string for use with the terminal.

public colorize(string $string) : string

Takes strings formatted as <key>string</key> and formats them per the $highlightMap; if color support is disabled, simply removes the formatting tags.

Parameters
$string : string
Return values
string

write()

public write(string $string[, bool $colorize = true ][, resource $resource = STDOUT ]) : void
Parameters
$string : string
$colorize : bool = true

Whether or not to colorize the string

$resource : resource = STDOUT

Defaults to STDOUT

writeErrorMessage()

Emit an error message.

public writeErrorMessage(string $message) : void

Wraps the message in <error></error>, and passes it to writeLine(), using STDERR as the resource; emits an additional empty line when done, also to STDERR.

Parameters
$message : string

writeLine()

public writeLine(string $string[, bool $colorize = true ][, resource $resource = STDOUT ]) : void
Parameters
$string : string
$colorize : bool = true

Whether or not to colorize the line

$resource : resource = STDOUT

Defaults to STDOUT

detectColorCapabilities()

private detectColorCapabilities([resource $resource = STDOUT ]) : bool
Parameters
$resource : resource = STDOUT
Return values
bool

formatNewlines()

Ensure newlines are appropriate for the current terminal.

private formatNewlines(string $string) : string
Parameters
$string : string
Return values
string

        
On this page

Search results