title: How to get cursor position with PHP-CLI?
body: <p>With a PHP script which runs in CLI mode, I want to get the cursor position in a portable way.</p>
  
  <p>With the code :</p>
  
  <pre><code>// Query Cursor Position
  echo "\033[6n";
  </code></pre>
  
  <p>In the terminal, this code reports the cursor position, as</p>
  
  <pre><code>wb ?&gt; ./script.php 
  ^[[5;1R
  wb ?&gt; ;1R 
  </code></pre>
  
  <p>But, I can't retrieve the two values (row: 5, column: 1) in the code.</p>
  
  <p>After some tests with output buffering :</p>
  
  <pre><code>ob_start();
  echo "\033[6n";
  $s = ob_get_contents();
  file_put_contents('cpos.txt',$s);
  </code></pre>
  
  <p>I've "\033[6n" in the cpos.txt file, not the device answer.</p>
  
  <p>And reading STDIN :</p>
  
  <pre><code>$timeout = 2;
  $sent = false;
  $t = microtime(true);
  $buf = '';
  stream_set_blocking(STDIN,false);
  while(true){
      $buf .= fread(STDIN,8);
      if(!$sent){
          echo "\033[6n";
          $sent = true;
      }
      if($t+$timeout&lt;microtime(true))
          break;
  }
  var_dump($buf);
  </code></pre>
  
  <p>The buffer is empty but the terminal show the device answer :</p>
  
  <pre><code>wb ?&gt; ./script.php 
  ^[[5;1R
  string(0) ""
  wb ?&gt;
  </code></pre>
  
  <p>Is there a way, without curses, to get the cursor position ?</p>