4 * Pure-PHP ANSI Decoder
8 * If you call read() in Net_SSH2 you may get {@link http://en.wikipedia.org/wiki/ANSI_escape_code ANSI escape codes} back.
9 * They'd look like chr(0x1B) . '[00m' or whatever (0x1B = ESC). They tell a
10 * {@link http://en.wikipedia.org/wiki/Terminal_emulator terminal emulator} how to format the characters, what
11 * color to display them in, etc. File_ANSI is a {@link http://en.wikipedia.org/wiki/VT100 VT100} terminal emulator.
13 * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 * @author Jim Wigginton <terrafrost@php.net>
34 * @copyright 2012 Jim Wigginton
35 * @license http://www.opensource.org/licenses/mit-license.html MIT License
36 * @link http://phpseclib.sourceforge.net
40 * Pure-PHP ANSI Decoder
43 * @author Jim Wigginton <terrafrost@php.net>
121 * An empty attribute row
129 * The current screen text
137 * The current screen attributes
145 * The current foreground color
153 * The current background color
209 * Default Constructor.
216 $this->setHistory(200);
217 $this->setDimensions(80, 24);
221 * Set terminal width and height
223 * Resets the screen as well
229 function setDimensions($x, $y)
231 $this->max_x = $x - 1;
232 $this->max_y = $y - 1;
233 $this->x = $this->y = 0;
234 $this->history = $this->history_attrs = array();
235 $this->attr_row = array_fill(0, $this->max_x + 1, '');
236 $this->screen = array_fill(0, $this->max_y + 1, '');
237 $this->attrs = array_fill(0, $this->max_y + 1, $this->attr_row);
238 $this->foreground = 'white';
239 $this->background = 'black';
241 $this->underline = false;
242 $this->blink = false;
243 $this->reverse = false;
244 $this->color = false;
250 * Set the number of lines that should be logged past the terminal height
256 function setHistory($history)
258 $this->max_history = $history;
264 * @param String $source
267 function loadString($source)
269 $this->setDimensions($this->max_x + 1, $this->max_y + 1);
270 $this->appendString($source);
276 * @param String $source
279 function appendString($source)
281 for ($i = 0; $i < strlen($source); $i++) {
282 if (strlen($this->ansi)) {
283 $this->ansi.= $source[$i];
284 $chr = ord($source[$i]);
285 // http://en.wikipedia.org/wiki/ANSI_escape_code#Sequence_elements
286 // single character CSI's not currently supported
288 case $this->ansi == "\x1B=":
291 case strlen($this->ansi) == 2 && $chr >= 64 && $chr <= 95 && $chr != ord('['):
292 case strlen($this->ansi) > 2 && $chr >= 64 && $chr <= 126:
297 // http://ascii-table.com/ansi-escape-sequences-vt-100.php
298 switch ($this->ansi) {
299 case "\x1B[H": // Move cursor to upper left corner
300 $this->old_x = $this->x;
301 $this->old_y = $this->y;
302 $this->x = $this->y = 0;
304 case "\x1B[J": // Clear screen from cursor down
305 $this->history = array_merge($this->history, array_slice(array_splice($this->screen, $this->y + 1), 0, $this->old_y));
306 $this->screen = array_merge($this->screen, array_fill($this->y, $this->max_y, ''));
308 $this->history_attrs = array_merge($this->history_attrs, array_slice(array_splice($this->attrs, $this->y + 1), 0, $this->old_y));
309 $this->attrs = array_merge($this->attrs, array_fill($this->y, $this->max_y, $this->attr_row));
311 if (count($this->history) == $this->max_history) {
312 array_shift($this->history);
313 array_shift($this->history_attrs);
315 case "\x1B[K": // Clear screen from cursor right
316 $this->screen[$this->y] = substr($this->screen[$this->y], 0, $this->x);
318 array_splice($this->attrs[$this->y], $this->x + 1);
320 case "\x1B[2K": // Clear entire line
321 $this->screen[$this->y] = str_repeat(' ', $this->x);
322 $this->attrs[$this->y] = $this->attr_row;
324 case "\x1B[?1h": // set cursor key to application
325 case "\x1B[?25h": // show the cursor
327 case "\x1BE": // Move to next line
333 case preg_match('#\x1B\[(\d+);(\d+)H#', $this->ansi, $match): // Move cursor to screen location v,h
334 $this->old_x = $this->x;
335 $this->old_y = $this->y;
336 $this->x = $match[2] - 1;
337 $this->y = $match[1] - 1;
339 case preg_match('#\x1B\[(\d+)C#', $this->ansi, $match): // Move cursor right n lines
340 $this->old_x = $this->x;
343 case preg_match('#\x1B\[(\d+);(\d+)r#', $this->ansi, $match): // Set top and bottom lines of a window
345 case preg_match('#\x1B\[(\d*(?:;\d*)*)m#', $this->ansi, $match): // character attributes
346 $mods = explode(';', $match[1]);
347 foreach ($mods as $mod) {
349 case 0: // Turn off character attributes
350 $this->attrs[$this->y][$this->x] = '';
352 if ($this->bold) $this->attrs[$this->y][$this->x].= '</b>';
353 if ($this->underline) $this->attrs[$this->y][$this->x].= '</u>';
354 if ($this->blink) $this->attrs[$this->y][$this->x].= '</blink>';
355 if ($this->color) $this->attrs[$this->y][$this->x].= '</span>';
357 if ($this->reverse) {
358 $temp = $this->background;
359 $this->background = $this->foreground;
360 $this->foreground = $temp;
363 $this->bold = $this->underline = $this->blink = $this->color = $this->reverse = false;
365 case 1: // Turn bold mode on
367 $this->attrs[$this->y][$this->x] = '<b>';
371 case 4: // Turn underline mode on
372 if (!$this->underline) {
373 $this->attrs[$this->y][$this->x] = '<u>';
374 $this->underline = true;
377 case 5: // Turn blinking mode on
379 $this->attrs[$this->y][$this->x] = '<blink>';
383 case 7: // Turn reverse video on
384 $this->reverse = !$this->reverse;
385 $temp = $this->background;
386 $this->background = $this->foreground;
387 $this->foreground = $temp;
388 $this->attrs[$this->y][$this->x] = '<span style="color: ' . $this->foreground . '; background: ' . $this->background . '">';
390 $this->attrs[$this->y][$this->x] = '</span>' . $this->attrs[$this->y][$this->x];
394 default: // set colors
395 //$front = $this->reverse ? &$this->background : &$this->foreground;
396 $front = &$this->{ $this->reverse ? 'background' : 'foreground' };
397 //$back = $this->reverse ? &$this->foreground : &$this->background;
398 $back = &$this->{ $this->reverse ? 'foreground' : 'background' };
400 case 30: $front = 'black'; break;
401 case 31: $front = 'red'; break;
402 case 32: $front = 'green'; break;
403 case 33: $front = 'yellow'; break;
404 case 34: $front = 'blue'; break;
405 case 35: $front = 'magenta'; break;
406 case 36: $front = 'cyan'; break;
407 case 37: $front = 'white'; break;
409 case 40: $back = 'black'; break;
410 case 41: $back = 'red'; break;
411 case 42: $back = 'green'; break;
412 case 43: $back = 'yellow'; break;
413 case 44: $back = 'blue'; break;
414 case 45: $back = 'magenta'; break;
415 case 46: $back = 'cyan'; break;
416 case 47: $back = 'white'; break;
419 user_error('Unsupported attribute: ' . $mod);
425 $this->attrs[$this->y][$this->x] = '<span style="color: ' . $this->foreground . '; background: ' . $this->background . '">';
427 $this->attrs[$this->y][$this->x] = '</span>' . $this->attrs[$this->y][$this->x];
434 user_error("{$this->ansi} unsupported\r\n");
441 switch ($source[$i]) {
448 case "\x0F": // shift
450 case "\x1B": // start ANSI escape code
451 $this->ansi.= "\x1B";
454 $this->screen[$this->y] = substr_replace(
455 $this->screen[$this->y],
461 if ($this->x > $this->max_x) {
474 * Also update the $this->screen and $this->history buffers
480 //if ($this->y < $this->max_y) {
484 while ($this->y >= $this->max_y) {
485 $this->history = array_merge($this->history, array(array_shift($this->screen)));
486 $this->screen[] = '';
488 $this->history_attrs = array_merge($this->history_attrs, array(array_shift($this->attrs)));
489 $this->attrs[] = $this->attr_row;
491 if (count($this->history) >= $this->max_history) {
492 array_shift($this->history);
493 array_shift($this->history_attrs);
502 * Returns the current screen without preformating
507 function _getScreen()
510 for ($i = 0; $i <= $this->max_y; $i++) {
511 for ($j = 0; $j <= $this->max_x + 1; $j++) {
512 if (isset($this->attrs[$i][$j])) {
513 $output.= $this->attrs[$i][$j];
515 if (isset($this->screen[$i][$j])) {
516 $output.= htmlspecialchars($this->screen[$i][$j]);
521 return rtrim($output);
525 * Returns the current screen
532 return '<pre style="color: white; background: black" width="' . ($this->max_x + 1) . '">' . $this->_getScreen() . '</pre>';
536 * Returns the current screen and the x previous lines
541 function getHistory()
544 for ($i = 0; $i < count($this->history); $i++) {
545 for ($j = 0; $j <= $this->max_x + 1; $j++) {
546 if (isset($this->history_attrs[$i][$j])) {
547 $scrollback.= $this->history_attrs[$i][$j];
549 if (isset($this->history[$i][$j])) {
550 $scrollback.= htmlspecialchars($this->history[$i][$j]);
553 $scrollback.= "\r\n";
555 $scrollback.= $this->_getScreen();
557 return '<pre style="color: white; background: black" width="' . ($this->max_x + 1) . '">' . $scrollback . '</pre>';