Username:
Passowrd:

Classe per messaggi colorati per php-cli

Semplice classe per gestire in modo semplice ma abbastanza variegato l'output e i messaggi all'utente in uno script php-cli

<?php
class CliColors
  {
  var $error = "";
  var $warning = "";
  var $ok = "";

  var $reset = "\033[0m";
  var $str = "\033[%s%s\033[37m\n";

  var $white = "1;37m";
  var $red = "1;31m";
  var $yellow = "1;33m";
  var $green = "1;32m";
  var $blu = "1;34m";
  var $cyan = "1;36m";
  var $purple = "1;35m";
  var $gray = "1;30m";
  var $orange = "0;33m";

  var $errc = "";
  var $warc = "";
  var $okc = "";

  function CliColors($error = "red", $warning = "yellow", $ok = "green")
    {
    $this->check_color($error);
    $this->check_color($warning);
    $this->check_color($ok);

    $this->errc = $this->$error;
    $this->warc = $this->$warning;
    $this->okc = $this->$ok;
    }

  function check_color($color)
    {
    if(empty($this->$color))
      {
      $ret = $this->white;
      trigger_error("Color '$color' not found", E_USER_NOTICE);
      }
    else
      $ret = $this->$color;
    return $ret;
    }

  function color($color, $txt)
    {
    echo sprintf($this->str, $this->check_color($color), $txt);
    echo $this->reset;
    }

  function error($txt, $die = false)
    {
    echo sprintf($this->str, $this->errc, $txt);
    echo $this->reset;
    if($die)
      die();
    }

  function warning($txt)
    {
    echo sprintf($this->str, $this->warc, $txt);
    echo $this->reset;
    }

  function ok($txt)
    {
    echo sprintf($this->str, $this->okc, $txt);
    echo $this->reset;
    }
  }
?>
 
Nickname:

Email:

Website: (http://www.example.com)

Commento: