3

I'm working with the PHP CLI on windows at the moment to write some small desktop command-line apps.

I wanted to know if and how it may be possible to:

  • Clear the screen (cls would be the normal command but exec() won't work with it)
  • Change the color, change the color of parts of the output (seen this in programs before)
  • Make the command line horizontally bigger - things quickly get unreadable

Is any of the above possible from inside a PHP script?

ac2
  • 31
  • 1
  • 2

5 Answers5

2

On Windows, in the standard CLI prompt, you cannot output in colour (as in the answer by Spudley).

You can change the size of the window as a user by right-clicking the command window's title bar and selecting Properties, then ammending values in the Layout tab. I do not think it is possible to ammend the width of the CLI within PHP.

You can check the width of the CLI window on Windows using the function I wrote here

Community
  • 1
  • 1
lyceus
  • 149
  • 1
  • 3
1

See the PHP manual page for working with the commandline

To directly answer each of your bullet points:

There is a comment on that page which gives a function that can clear the screen. I'll quote it here for you:

<?php
  function clearscreen($out = TRUE) {
    $clearscreen = chr(27)."[H".chr(27)."[2J";
    if ($out) print $clearscreen;
    else return $clearscreen;
  }
?>

There's also another comment which explains how to change the colours. Again, I'll quote it:

<?php
echo "\033[31m".$myvar; // red foreground
echo "\033[41m".$myvar; // red background
?>

and to reset:

<?php
echo "\033[0m";
?>

You should read through the rest of that page for a whole load more suggestions on how to manipulate the CLI.

The only part of your question that leave unanswered is the third bullet point. Sadly, I don't believe you'll be able to do this, and I don't think it's possible to horizontally resize the Windows command line window.

Hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307
0

The provided code will not work under Windows because PHP under windows does something to the command window. I am not sure what PHP does but I wrote a simple Freebasic program with only two lines:

   cls
   end

I then compiled it and ran it under a regular command line window. It cleared the screen without any kind of a problem. I then did the following in PHP:

<?php
   echo "This is a test\n";
   system( "cls.exe" );
   exec( "cls.exe" );
   passthru( "cls.exe" );
?>

When I ran the program it did nothing more than just the "This is a test" line. Thus, there is some kind of suppression going on with PHP that looks for and stops any kind of escape sequence from occurring. Until this is fixed in PHP - you will never be able to do a cls, nor use curses, ncurses, or any other library. What has to be done is to integrate something like FreeBasic's windowing methods as some kind of a class (or maybe just a C set of routines) that will open a new window via THAT language's methodologies and use them to do the text window. Then all of the escape sequences will work. Until then - they won't.

What I find weird about this is that PHP was originally written in Perl and Perl will do ncurses on Windows without any problems. Perl will also allow all escape sequences to work. So there is just something being done on the Windows compile that is causing this problem.

0

I created a small backup script with PHP, and from what I can remember, you can print backspace characters to remove content. Not really ideal though.

Just google'd it: http://www.php.net/manual/en/features.commandline.php#77142

Josh
  • 1,261
  • 2
  • 18
  • 34
0

As far as the third question goes, I suggest you witch the default command line to Console 2. It is a great replacement that not only lets you use any width you like (as long as it fits your screen), but also supports command history, tabs, and some UI sugar.

Mchl
  • 61,444
  • 9
  • 118
  • 120