Pull to refresh

Работа с ANSI консолью

Reading time 5 min
Views 3.8K
imageЧасто ли нужно сделать програму для консоли? Не так часто, да? А вот я последнее время только этим и занимаюсь… Поэтому сделал класс (на самом деле сделал еще очень давно).
Этот класс позволяет выполнять такие простые операции, как подсветка текста, подсветка бэкграунда буквы или текста, установка курсора на нужную позицию, выбор консоли для вывода и ввод текста в консоль.

Долго трепать языком не стану. Тот, кто зашел под кат явно знает зачем он пришел. А поэтому…Вот код:
  1. <?php
  2. class console
  3. {
  4.   const TARGET_OUTPUT = "php://output";
  5.   const TARGET_STDOUT = "php://stdout";
  6.   const TARGET_STDERR = "php://stderr";
  7.   const TARGET_STDIN = "php://stdin";
  8.   protected static $color = array(
  9.     'gray'     => 30,
  10.     'black'     => 30,
  11.     'red'      => 31,
  12.     'green'     => 32,
  13.     'yellow'    => 33,
  14.     'blue'     => 34,
  15.     'magenta'    => 35,
  16.     'cyan'     => 36,
  17.     'white'     => 37,
  18.     'default'    => 39
  19.   );
  20.   protected static $bgcolor = array(
  21.     'gray'    => 40,
  22.     'black'   => 40,
  23.     'red'    => 41,
  24.     'green'   => 42,
  25.     'yellow'   => 43,
  26.     'blue'    => 44,
  27.     'magenta'  => 45,
  28.     'cyan'    => 46,
  29.     'white'   => 47,
  30.     'default'  => 49,
  31.   );
  32.   protected static $style = array(
  33.     'default'      => '0',
  34.     'bold'       => 1,
  35.     'faint'       => 2,
  36.     'normal'      => 22,
  37.     'italic'      => 3,
  38.     'notitalic'     => 23,
  39.     'underlined'    => 4,
  40.     'doubleunderlined' => 21,
  41.     'notunderlined'   => 24,
  42.     'blink'       => 5,
  43.     'blinkfast'     => 6,
  44.     'noblink'      => 25,
  45.     'negative'     => 7,
  46.     'positive'     => 27,
  47.   );
  48.     private $text      = "";
  49.   // Outputing
  50.   public function draw($text='')
  51.   {
  52.     echo $this->text.$text;
  53.     $this->text = '';
  54.     return $this;
  55.   }
  56.   // Input
  57.   public function readNumeric()
  58.   {
  59.     $stdin = fopen('php://stdin', 'r');
  60.     $line = trim(fgets($stdin));
  61.     fscanf($stdin, "%d\n", $number);
  62.      return $number;
  63.   }
  64.   public function readString()
  65.   {
  66.     $stdin = fopen('php://stdin', 'r');
  67.     $line = trim(fgets($stdin));
  68.      fscanf($stdin, "%s\n", $string);
  69.      return $string;
  70.   }
  71.   // Sound
  72.   public function beep()              { echo "\007"; return $this; }
  73.   public function setSoundHerz($herz=100)      { echo "\033[10;{$herz}]"; return $this; }
  74.   public function setSoundLong($milliseconds=500)  { echo "'033[11;{$milliseconds}]"; return $this; }
  75.   // Cursor position
  76.   public function toPos( $row = 1, $column = 1 )  { echo "\033[{$row};{$column}H"; return $this; }
  77.   public function cursorUp($lines=1)        { echo "\033[{$lines}A"; return $this; }
  78.   public function cursorDown($lines=1)      { echo "\033[{$lines}B"; return $this; }
  79.   public function cursorRight($columns=1)      { echo "\033[{$columns}C"; return $this; }
  80.   public function cursorLeft($columns=1)      { echo "\033[{$columns}D"; return $this; }
  81.   // Text colors
  82.   public function setStyle($style='default')    { $this->text .= "\033[".$this->style[$style]."m"; return $this; }
  83.   public function setColor($color='default')    { $this->text .= "\033[".$this->color[$style]; return $this; }
  84.   public function setBgColor($color='default')  { $this->text .= "\033[".$this->bgcolor[$style]; return $this; }
  85.   // Application
  86.   public function setAppName($name='')      { echo "\033]0;{$name}\007"; return $this; }
  87.   public function setTitle($name='')        { echo "\033]2;{$name}\007"; return $this; }
  88.   public function setIcon($name='')        { echo "\033]1;{$name}\007"; return $this; }
  89.   // Other
  90.   public function clear()              { echo "\033c"; return $this; }
  91.   public function console($num=1)          { echo "\033[12;{$num}]"; return $this; }
  92. }
  93. ?>
* This source code was highlighted with Source Code Highlighter.
Так же скачать это можно с phpclasses.org по этой ссылке: http://www.phpclasses.org/browse/package/4969.html
И посмотреть кросс-пост в моем блоге по адресу: http://alexsnet.ru/2008/11/how-to-work-with-ansi-console/
Спасибо за внимание.
Tags:
Hubs:
+53
Comments 43
Comments Comments 43

Articles