PHP-cli-factorio-updater/scripts/class.TerminalMessage.php

77 lines
2.4 KiB
PHP
Raw Normal View History

2024-11-10 21:58:32 +01:00
<?php
/**
* Namespace for Terminal related classes.
* @package NAE\Terminal.
*/
2024-11-10 21:58:32 +01:00
namespace NAE\Terminal;
/**
* A Class to print messages to the terminal with different styles and colors.
* @author "Marcel Naeve" <php@naeve.info>
* @license MIT License (http://www.opensource.org/licenses/mit)
*/
2024-11-10 21:58:32 +01:00
class TerminalMessage {
/**
* Send a message to the terminal ending with a line break at the end.
* @param string $msg The message to send.
*/
2024-11-10 21:58:32 +01:00
public function send(string $msg) {
echo $msg . PHP_EOL;
}
/**
* Send a title to the terminal with a specified layer and and a double line break at the end.
* @param string $msg The title message to send.
* @param string $layer The layer (default: 0)
*/
2024-11-10 21:58:32 +01:00
public function sendTitle(string $msg, int $layer=0) {
$layerString = "#";
for($i=0; $i<$layer; $i++) {
$layerString .= "#";
}
self::send( "\033[1m[$layerString] $msg\033[0m" . PHP_EOL );
}
/**
* Send a success message to the terminal with a green color and a line break at the end.
* @param string $msg The success message to send.
*/
2024-11-10 21:58:32 +01:00
public function sendSuccess(string $msg) {
self::send( "\033[1;32m[SUCCESS] $msg\033[0m" );
}
/**
* Send an info message to the terminal with a blue color and a line break at the end.
* @param string $msg The info message to send.
*/
2024-11-10 21:58:32 +01:00
public function sendInfo(string $msg) {
self::send( "\033[1;34m[INFO] $msg\033[0m" );
}
/**
* Send an error message to the terminal with a red color and a line break at the end.
* @param string $msg The error message to send.
* @param bool $critical If true, the script will terminate with a non-zero exit code (default: false).
*/
2024-11-10 21:58:32 +01:00
public function sendError(string $msg, bool $critical=false) {
self::send( "\033[1;31m[ERROR] $msg\033[0m" );
if ($critical) {
die(1);
}
}
/**
* Send a warning message to the terminal with a yellow color and a line break at the end.
* @param string $msg The warning message to send.
* @param bool $critical If true, the script will terminate with a non-zero exit code (default: false).
*/
2024-11-10 21:58:32 +01:00
public function sendWarning(string $msg, bool $critical=false) {
self::send( "\033[1;33m[WARNING] $msg\033[0m" );
if ($critical) {
die(1);
}
}
}