Tricks jokes

Work with ANSI the console

November 20, 2008 · 11:03 дп Alex Snet  -  11 comments

Whether often it is necessary to make програму for the console? Not so it is frequent, yes? And here I lately only also am engaged in it … Therefore have made a class (actually has made still very much for a long time). А вот я последнее время только этим и занимаюсь… Поэтому сделал класс (на самом деле сделал еще очень давно).

This class allows to carry out such simple operations, as text illumination, illumination of a background of the letter or the text, cursor installation on the necessary position, a choice of the console for a conclusion (1~8) and a direct hornbeam of the pressed keys.


<? php 
class console 
{ 
    const TARGET_OUTPUT = "php://output"; ; 
    const TARGET_STDOUT = "php://stdout"; ; 
    const TARGET_STDERR = "php://stderr"; ; 
    const TARGET_STDIN = "php://stdin"; ; 
 
    protected static $color = array ( ( 
        ' grey ' => 30, , 
        ' black ' => 30, , 
        ' red ' => 31, , 
        ' green ' => 32, , 
        ' yellow ' => 33, , 
        ' blue ' => 34, , 
        ' magenta ' => 35, , 
        ' cyan ' => 36, , 
        ' white ' => 37, , 
        ' default ' => 39  
    ); 
 
    protected static $bgcolor = array ( ( 
        ' grey ' => 40, , 
        ' black ' => 40, , 
        ' red ' => 41, , 
        ' green ' => 42, , 
        ' yellow ' => 43, , 
        ' blue ' => 44, , 
        ' magenta ' => 45, , 
        ' cyan ' => 46, , 
        ' white ' => 47, , 
        ' default ' => 49, , 
    ); 
 
    protected static $style = array ( ( 
        ' default ' => ' 0 ', , 
 
        ' bold ' => 1, , 
        ' faint ' => 2, , 
        ' normal ' => 22, , 
 
        ' italic ' => 3, , 
        ' notitalic ' => 23, , 
 
        ' underlined ' => 4, , 
        ' doubleunderlined ' => 21, , 
        ' notunderlined ' => 24, , 
 
        ' blink ' => 5, , 
        ' blinkfast ' => 6, , 
        ' noblink ' => 25, , 
 
        ' negative ' => 7, , 
        ' positive ' => 27, , 
    ); 
 
       private $text = "";   ""; 
 
    //Outputing 
    public function draw ($text = ") $text='') 
    { 
        echo $this-> text. $ text; text.$text; 
        $this-> text = ";  = ''; 
        return $this;  
    } 
 
    //Input 
    public function readNumeric () ) 
    { 
        $stdin = fopen (' php://stdin ', ' r '); ('php://stdin', 'r'); 
        $line = trim (fgets ($stdin)); (fgets($stdin)); 
        fscanf ($stdin, "%d\n", $number); , "%d\n", $number); 
          return $number;  
    } 
    public function readString () ) 
    { 
        $stdin = fopen (' php://stdin ', ' r '); ('php://stdin', 'r'); 
        $line = trim (fgets ($stdin)); (fgets($stdin)); 
          fscanf ($stdin, "%s\n", $string); , "%s\n", $string); 
          return $string;  
    } 
 
    //Sound 
    public function beep () {echo "\007"; return $this;} )                            { echo "\007"; return $this; } 
    public function setSoundHerz ($herz=100) {echo "\033 [10; {$herz}]"; return $this;} $herz=100)            { echo "\033[10;{$herz}]"; return $this; } 
    public function setSoundLong ($milliseconds=500) {echo "' 033 [11; {$milliseconds}]"; return $this;} $milliseconds=500)    { echo "'033[11;{$milliseconds}]"; return $this; } 
 
    //Cursor position 
    public function toPos ($row = 1, $column = 1) {echo "\033 [{$row}; {$column} H"; return $this;} $row = 1, $column = 1 )    { echo "\033[{$row};{$column}H"; return $this; }
    public function cursorUp ($lines=1) {echo "\033 [{$lines} A"; return $this;} $lines=1)                { echo "\033[{$lines}A"; return $this; } 
    public function cursorDown ($lines=1) {echo "\033 [{$lines} B"; return $this;} $lines=1)            { echo "\033[{$lines}B"; return $this; } 
    public function cursorRight ($columns=1) {echo "\033 [{$columns} C"; return $this;} $columns=1)            { echo "\033[{$columns}C"; return $this; } 
    public function cursorLeft ($columns=1) {echo "\033 [{$columns} D"; return $this;} $columns=1)            { echo "\033[{$columns}D"; return $this; } 
 
    //Text colours 
    public function setStyle ($style ='default ') {$this-> text. = "\033 [". $this-> style [$style]. "m"; return $this;} $style='default')        { $this->text .= "\033[".$this->style[$style]."m"; return $this; } 
    public function setColor ($color ='default ') {$this-> text. = "\033 [". $this-> colour [$style]; return $this;} $color='default')        { $this->text .= "\033[".$this->color[$style]; return $this; } 
    public function setBgColor ($color ='default ') {$this-> text. = "\033 [". $this-> bgcolor [$style]; return $this;} $color='default')    { $this->text .= "\033[".$this->bgcolor[$style]; return $this; } 
 
    //Application 
    public function setAppName ($name = ") {echo" \033] 0; {$name} \007 "; return $this;} $name='')            { echo "\033]0;{$name}\007"; return $this; } 
    public function setTitle ($name = ") {echo" \033] 2; {$name} \007 "; return $this;} $name='')                { echo "\033]2;{$name}\007"; return $this; } 
    public function setIcon ($name = ") {echo" \033] 1; {$name} \007 "; return $this;} $name='')                { echo "\033]1;{$name}\007"; return $this; } 
 
    //Other 
    public function clear () {echo "\033c"; return $this;} )                            { echo "\033c"; return $this; } 
    public function console ($num=1) {echo "\033 [12; {$num}]"; return $this;} $num=1)                    { echo "\033[12;{$num}]"; return $this; } 
} 
?>

As to download it it is possible with phpclasses.org under this reference: http://www.phpclasses.org/browse/package/4969.html
Thanks for attention.

Tags: , , ,

And still I wrote about:
None Found

11 comments → “Work with ANSI the console”


  1. surger

    20 Aching, 2008

    Even nostalgia on terminals has punched …
    Спасиба, камрад

    To answer

  2. Alex Snet

    20 Aching, 2008

    Is always glad to help! =)

    To answer

  3. wtask

    16 Deck, 2008

    Thanks, we will look. Periodically are required console скриптики.

    To answer

  4. Emilis

    02 Janv, 2009

    Can be at you eats also an example how to use this class?

    To answer

  5. Alex Snet

    02 Janv, 2009

    Emilis:
    Can be at you eats also an example how to use this class?

    Yes, certainly. And what exactly to you is not clear?

    To answer

  6. Pepe

    20 Mar, 2009

    Hi

    Do you have an example how to use this class?
    Thanks.
    Pepe

    To answer

  7. Val

    29 Okt, 2009

    Good afternoon; and you did not try to use this class under windows?

    To answer

    • Alex Snet

      03 Aching, 2009

      No, did not try. To tell the truth, I doubt that under виндовс will go console ASCII commands.

      To answer

      • Val

        03 Aching, 2009

        Correctly doubt — console commands from пхп on Windows do not work.

        So clear the screen from пхп I could only by a call of a corresponding method from dllины.

        What is amazing, in a python under the same Windows works subprocess.call ([' cmd ', '/c ', ' cls '])

        And in пхп – is not present.

        To answer

        • Alex Snet

          04 Aching, 2009

          Val, all of you таки confuse a little. In the given code there is no call of other appendices. Here we simply print спецсимволы which carry out this or that operation. Здесь мы просто печатаем спецсимволы, которые выполняют ту или иную операцию.
          About a call of console appendices – at php there are many variants as it becomes. For example: exec, shell_exec, system, etc … exec, shell_exec, system, etc…
          Therefore quite probably that with one command will not work, and with another quite.

          To answer

  8. Val

    04 Aching, 2009

    Alex, I have methodically touched all these functions in php and all the ways long "in a simple way" to cause cls: and command/c cls, and cmd/c cls, and directly to print chr (27), “[H”, chr (27), “[2J” and in general all.

    Do not work. I.e. in general — they give out on the console correct ASCII commands, and I can observe them: ← [2J, ♀ ←[2J, ♀

    The Internet has made happy me with the following: “Windows NT does not support ANSI.SYS escape sequences in Win32 Console applications”

    php.exe — it is obvious win32 console app, therefore does not work.

    Question in why in python.exe works.

    To answer

To leave the trace.

1 2 3 4 5 6 7 8 9 10