2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Pure-PHP ANSI Decoder
9 * If you call read() in Net_SSH2 you may get {@link http://en.wikipedia.org/wiki/ANSI_escape_code ANSI escape codes} back.
10 * They'd look like chr(0x1B) . '[00m' or whatever (0x1B = ESC). They tell a
11 * {@link http://en.wikipedia.org/wiki/Terminal_emulator terminal emulator} how to format the characters, what
12 * color to display them in, etc. File_ANSI is a {@link http://en.wikipedia.org/wiki/VT100 VT100} terminal emulator.
14 * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 * @author Jim Wigginton <terrafrost@php.net>
35 * @copyright MMXII Jim Wigginton
36 * @license http://www.opensource.org/licenses/mit-license.html MIT License
38 * @link htp://phpseclib.sourceforge.net
42 * Pure-PHP ANSI Decoder
44 * @author Jim Wigginton <terrafrost@php.net>
123 * An empty attribute row
131 * The current screen text
139 * The current screen attributes
147 * The current foreground color
155 * The current background color
211 * Default Constructor.
218 $this->setHistory(200);
219 $this->setDimensions(80, 24);
223 * Set terminal width and height
225 * Resets the screen as well
231 function setDimensions($x, $y)
233 $this->max_x
= $x - 1;
234 $this->max_y
= $y - 1;
235 $this->x
= $this->y
= 0;
236 $this->history
= $this->history_attrs
= array();
237 $this->attr_row
= array_fill(0, $this->max_x +
1, '');
238 $this->screen
= array_fill(0, $this->max_y +
1, '');
239 $this->attrs
= array_fill(0, $this->max_y +
1, $this->attr_row
);
240 $this->foreground
= 'white';
241 $this->background
= 'black';
243 $this->underline
= false;
244 $this->blink
= false;
245 $this->reverse
= false;
246 $this->color
= false;
252 * Set the number of lines that should be logged past the terminal height
258 function setHistory($history)
260 $this->max_history
= $history;
266 * @param String $source
269 function loadString($source)
271 $this->setDimensions($this->max_x +
1, $this->max_y +
1);
272 $this->appendString($source);
278 * @param String $source
281 function appendString($source)
283 for ($i = 0; $i < strlen($source); $i++
) {
284 if (strlen($this->ansi
)) {
285 $this->ansi
.= $source[$i];
286 $chr = ord($source[$i]);
287 // http://en.wikipedia.org/wiki/ANSI_escape_code#Sequence_elements
288 // single character CSI's not currently supported
290 case $this->ansi
== "\x1B=":
293 case strlen($this->ansi
) == 2 && $chr >= 64 && $chr <= 95 && $chr != ord('['):
294 case strlen($this->ansi
) > 2 && $chr >= 64 && $chr <= 126:
299 // http://ascii-table.com/ansi-escape-sequences-vt-100.php
300 switch ($this->ansi
) {
302 $this->old_x
= $this->x
;
303 $this->old_y
= $this->y
;
304 $this->x
= $this->y
= 0;
307 $this->history
= array_merge($this->history
, array_slice(array_splice($this->screen
, $this->y +
1), 0, $this->old_y
));
308 $this->screen
= array_merge($this->screen
, array_fill($this->y
, $this->max_y
, ''));
310 $this->history_attrs
= array_merge($this->history_attrs
, array_slice(array_splice($this->attrs
, $this->y +
1), 0, $this->old_y
));
311 $this->attrs
= array_merge($this->attrs
, array_fill($this->y
, $this->max_y
, $this->attr_row
));
313 if (count($this->history
) == $this->max_history
) {
314 array_shift($this->history
);
315 array_shift($this->history_attrs
);
318 $this->screen
[$this->y
] = substr($this->screen
[$this->y
], 0, $this->x
);
320 array_splice($this->attrs
[$this->y
], $this->x +
1);
322 case "\x1B[?1h": // set cursor key to application
326 case preg_match('#\x1B\[(\d+);(\d+)H#', $this->ansi
, $match):
327 $this->old_x
= $this->x
;
328 $this->old_y
= $this->y
;
329 $this->x
= $match[2] - 1;
330 $this->y
= $match[1] - 1;
332 case preg_match('#\x1B\[(\d+)C#', $this->ansi
, $match):
333 $this->old_x
= $this->x
;
336 case preg_match('#\x1B\[(\d+);(\d+)r#', $this->ansi
, $match): // Set top and bottom lines of a window
338 case preg_match('#\x1B\[(\d*(?:;\d*)*)m#', $this->ansi
, $match):
339 $mods = explode(';', $match[1]);
340 foreach ($mods as $mod) {
343 $this->attrs
[$this->y
][$this->x
] = '';
345 if ($this->bold
) $this->attrs
[$this->y
][$this->x
].= '</b>';
346 if ($this->underline
) $this->attrs
[$this->y
][$this->x
].= '</underline>';
347 if ($this->blink
) $this->attrs
[$this->y
][$this->x
].= '</blink>';
348 if ($this->color
) $this->attrs
[$this->y
][$this->x
].= '</span>';
350 if ($this->reverse
) {
351 $temp = $this->background
;
352 $this->background
= $this->foreground
;
353 $this->foreground
= $temp;
356 $this->bold
= $this->underline
= $this->blink
= $this->color
= $this->reverse
= false;
360 $this->attrs
[$this->y
][$this->x
] = '<b>';
365 if (!$this->underline
) {
366 $this->attrs
[$this->y
][$this->x
] = '<u>';
367 $this->underline
= true;
372 $this->attrs
[$this->y
][$this->x
] = '<blink>';
377 $this->reverse
= !$this->reverse
;
378 $temp = $this->background
;
379 $this->background
= $this->foreground
;
380 $this->foreground
= $temp;
381 $this->attrs
[$this->y
][$this->x
] = '<span style="color: ' . $this->foreground
. '; background: ' . $this->background
. '">';
383 $this->attrs
[$this->y
][$this->x
] = '</span>' . $this->attrs
[$this->y
][$this->x
];
388 //$front = $this->reverse ? &$this->background : &$this->foreground;
389 $front = &$this->{ $this->reverse ?
'background' : 'foreground' };
390 //$back = $this->reverse ? &$this->foreground : &$this->background;
391 $back = &$this->{ $this->reverse ?
'foreground' : 'background' };
393 case 30: $front = 'black'; break;
394 case 31: $front = 'red'; break;
395 case 32: $front = 'green'; break;
396 case 33: $front = 'yellow'; break;
397 case 34: $front = 'blue'; break;
398 case 35: $front = 'magenta'; break;
399 case 36: $front = 'cyan'; break;
400 case 37: $front = 'white'; break;
402 case 40: $back = 'black'; break;
403 case 41: $back = 'red'; break;
404 case 42: $back = 'green'; break;
405 case 43: $back = 'yellow'; break;
406 case 44: $back = 'blue'; break;
407 case 45: $back = 'magenta'; break;
408 case 46: $back = 'cyan'; break;
409 case 47: $back = 'white'; break;
412 user_error('Unsupported attribute: ' . $mod);
418 $this->attrs
[$this->y
][$this->x
] = '<span style="color: ' . $this->foreground
. '; background: ' . $this->background
. '">';
420 $this->attrs
[$this->y
][$this->x
] = '</span>' . $this->attrs
[$this->y
][$this->x
];
427 echo "{$this->ansi} unsupported\r\n";
434 switch ($source[$i]) {
439 //if ($this->y < $this->max_y) {
443 while ($this->y
>= $this->max_y
) {
444 $this->history
= array_merge($this->history
, array(array_shift($this->screen
)));
445 $this->screen
[] = '';
447 $this->history_attrs
= array_merge($this->history_attrs
, array(array_shift($this->attrs
)));
448 $this->attrs
[] = $this->attr_row
;
450 if (count($this->history
) >= $this->max_history
) {
451 array_shift($this->history
);
452 array_shift($this->history_attrs
);
459 case "\x0F": // shift
461 case "\x1B": // start ANSI escape code
462 $this->ansi
.= "\x1B";
465 $this->screen
[$this->y
] = substr_replace(
466 $this->screen
[$this->y
],
472 if ($this->x
> $this->max_x
) {
483 * Returns the current screen without preformating
488 function _getScreen()
491 for ($i = 0; $i <= $this->max_y
; $i++
) {
492 for ($j = 0; $j <= $this->max_x +
1; $j++
) {
493 if (isset($this->attrs
[$i][$j])) {
494 $output.= $this->attrs
[$i][$j];
496 if (isset($this->screen
[$i][$j])) {
497 $output.= htmlspecialchars($this->screen
[$i][$j]);
502 return rtrim($output);
506 * Returns the current screen
513 return '<pre style="color: white; background: black" width="' . ($this->max_x +
1) . '">' . $this->_getScreen() . '</pre>';
517 * Returns the current screen and the x previous lines
522 function getHistory()
525 for ($i = 0; $i < count($this->history
); $i++
) {
526 for ($j = 0; $j <= $this->max_x +
1; $j++
) {
527 if (isset($this->history_attrs
[$i][$j])) {
528 $scrollback.= $this->history_attrs
[$i][$j];
530 if (isset($this->history
[$i][$j])) {
531 $scrollback.= htmlspecialchars($this->history
[$i][$j]);
534 $scrollback.= "\r\n";
536 $scrollback.= $this->_getScreen();
538 return '<pre style="color: white; background: black" width="' . ($this->max_x +
1) . '">' . $scrollback . '</pre>';