123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560 |
- <?php
- class File_ANSI {
-
- var $max_x;
-
- var $max_y;
-
- var $max_history;
-
- var $history;
-
- var $history_attrs;
-
- var $x;
-
- var $y;
-
- var $old_x;
-
- var $old_y;
-
- var $attr_row;
-
- var $screen;
-
- var $attrs;
-
- var $foreground;
-
- var $background;
-
- var $bold;
-
- var $underline;
-
- var $blink;
-
- var $reverse;
-
- var $color;
-
- var $ansi;
-
- function File_ANSI()
- {
- $this->setHistory(200);
- $this->setDimensions(80, 24);
- }
-
- function setDimensions($x, $y)
- {
- $this->max_x = $x - 1;
- $this->max_y = $y - 1;
- $this->x = $this->y = 0;
- $this->history = $this->history_attrs = array();
- $this->attr_row = array_fill(0, $this->max_x + 1, '');
- $this->screen = array_fill(0, $this->max_y + 1, '');
- $this->attrs = array_fill(0, $this->max_y + 1, $this->attr_row);
- $this->foreground = 'white';
- $this->background = 'black';
- $this->bold = false;
- $this->underline = false;
- $this->blink = false;
- $this->reverse = false;
- $this->color = false;
- $this->ansi = '';
- }
-
- function setHistory($history)
- {
- $this->max_history = $history;
- }
-
- function loadString($source)
- {
- $this->setDimensions($this->max_x + 1, $this->max_y + 1);
- $this->appendString($source);
- }
-
- function appendString($source)
- {
- for ($i = 0; $i < strlen($source); $i++) {
- if (strlen($this->ansi)) {
- $this->ansi.= $source[$i];
- $chr = ord($source[$i]);
-
-
- switch (true) {
- case $this->ansi == "\x1B=":
- $this->ansi = '';
- continue 2;
- case strlen($this->ansi) == 2 && $chr >= 64 && $chr <= 95 && $chr != ord('['):
- case strlen($this->ansi) > 2 && $chr >= 64 && $chr <= 126:
- break;
- default:
- continue 2;
- }
-
- switch ($this->ansi) {
- case "\x1B[H":
- $this->old_x = $this->x;
- $this->old_y = $this->y;
- $this->x = $this->y = 0;
- break;
- case "\x1B[J":
- $this->history = array_merge($this->history, array_slice(array_splice($this->screen, $this->y + 1), 0, $this->old_y));
- $this->screen = array_merge($this->screen, array_fill($this->y, $this->max_y, ''));
- $this->history_attrs = array_merge($this->history_attrs, array_slice(array_splice($this->attrs, $this->y + 1), 0, $this->old_y));
- $this->attrs = array_merge($this->attrs, array_fill($this->y, $this->max_y, $this->attr_row));
- if (count($this->history) == $this->max_history) {
- array_shift($this->history);
- array_shift($this->history_attrs);
- }
- case "\x1B[K":
- $this->screen[$this->y] = substr($this->screen[$this->y], 0, $this->x);
- array_splice($this->attrs[$this->y], $this->x + 1);
- break;
- case "\x1B[2K":
- $this->screen[$this->y] = str_repeat(' ', $this->x);
- $this->attrs[$this->y] = $this->attr_row;
- break;
- case "\x1B[?1h":
- case "\x1B[?25h":
- break;
- case "\x1BE":
- $this->_newLine();
- $this->x = 0;
- break;
- default:
- switch (true) {
- case preg_match('#\x1B\[(\d+);(\d+)H#', $this->ansi, $match):
- $this->old_x = $this->x;
- $this->old_y = $this->y;
- $this->x = $match[2] - 1;
- $this->y = $match[1] - 1;
- break;
- case preg_match('#\x1B\[(\d+)C#', $this->ansi, $match):
- $this->old_x = $this->x;
- $x = $match[1] - 1;
- break;
- case preg_match('#\x1B\[(\d+);(\d+)r#', $this->ansi, $match):
- break;
- case preg_match('#\x1B\[(\d*(?:;\d*)*)m#', $this->ansi, $match):
- $mods = explode(';', $match[1]);
- foreach ($mods as $mod) {
- switch ($mod) {
- case 0:
- $this->attrs[$this->y][$this->x] = '';
- if ($this->bold) $this->attrs[$this->y][$this->x].= '</b>';
- if ($this->underline) $this->attrs[$this->y][$this->x].= '</underline>';
- if ($this->blink) $this->attrs[$this->y][$this->x].= '</blink>';
- if ($this->color) $this->attrs[$this->y][$this->x].= '</span>';
- if ($this->reverse) {
- $temp = $this->background;
- $this->background = $this->foreground;
- $this->foreground = $temp;
- }
- $this->bold = $this->underline = $this->blink = $this->color = $this->reverse = false;
- break;
- case 1:
- if (!$this->bold) {
- $this->attrs[$this->y][$this->x] = '<b>';
- $this->bold = true;
- }
- break;
- case 4:
- if (!$this->underline) {
- $this->attrs[$this->y][$this->x] = '<u>';
- $this->underline = true;
- }
- break;
- case 5:
- if (!$this->blink) {
- $this->attrs[$this->y][$this->x] = '<blink>';
- $this->blink = true;
- }
- break;
- case 7:
- $this->reverse = !$this->reverse;
- $temp = $this->background;
- $this->background = $this->foreground;
- $this->foreground = $temp;
- $this->attrs[$this->y][$this->x] = '<span style="color: ' . $this->foreground . '; background: ' . $this->background . '">';
- if ($this->color) {
- $this->attrs[$this->y][$this->x] = '</span>' . $this->attrs[$this->y][$this->x];
- }
- $this->color = true;
- break;
- default:
-
- $front = &$this->{ $this->reverse ? 'background' : 'foreground' };
-
- $back = &$this->{ $this->reverse ? 'foreground' : 'background' };
- switch ($mod) {
- case 30: $front = 'black'; break;
- case 31: $front = 'red'; break;
- case 32: $front = 'green'; break;
- case 33: $front = 'yellow'; break;
- case 34: $front = 'blue'; break;
- case 35: $front = 'magenta'; break;
- case 36: $front = 'cyan'; break;
- case 37: $front = 'white'; break;
- case 40: $back = 'black'; break;
- case 41: $back = 'red'; break;
- case 42: $back = 'green'; break;
- case 43: $back = 'yellow'; break;
- case 44: $back = 'blue'; break;
- case 45: $back = 'magenta'; break;
- case 46: $back = 'cyan'; break;
- case 47: $back = 'white'; break;
- default:
- user_error('Unsupported attribute: ' . $mod);
- $this->ansi = '';
- break 2;
- }
- unset($temp);
- $this->attrs[$this->y][$this->x] = '<span style="color: ' . $this->foreground . '; background: ' . $this->background . '">';
- if ($this->color) {
- $this->attrs[$this->y][$this->x] = '</span>' . $this->attrs[$this->y][$this->x];
- }
- $this->color = true;
- }
- }
- break;
- default:
- user_error("{$this->ansi} unsupported\r\n");
- }
- }
- $this->ansi = '';
- continue;
- }
- switch ($source[$i]) {
- case "\r":
- $this->x = 0;
- break;
- case "\n":
- $this->_newLine();
- break;
- case "\x0F":
- break;
- case "\x1B":
- $this->ansi.= "\x1B";
- break;
- default:
- $this->screen[$this->y] = substr_replace(
- $this->screen[$this->y],
- $source[$i],
- $this->x,
- 1
- );
- if ($this->x > $this->max_x) {
- $this->x = 0;
- $this->y++;
- } else {
- $this->x++;
- }
- }
- }
- }
-
- function _newLine()
- {
-
-
-
- while ($this->y >= $this->max_y) {
- $this->history = array_merge($this->history, array(array_shift($this->screen)));
- $this->screen[] = '';
- $this->history_attrs = array_merge($this->history_attrs, array(array_shift($this->attrs)));
- $this->attrs[] = $this->attr_row;
- if (count($this->history) >= $this->max_history) {
- array_shift($this->history);
- array_shift($this->history_attrs);
- }
- $this->y--;
- }
- $this->y++;
- }
-
- function _getScreen()
- {
- $output = '';
- for ($i = 0; $i <= $this->max_y; $i++) {
- for ($j = 0; $j <= $this->max_x + 1; $j++) {
- if (isset($this->attrs[$i][$j])) {
- $output.= $this->attrs[$i][$j];
- }
- if (isset($this->screen[$i][$j])) {
- $output.= htmlspecialchars($this->screen[$i][$j]);
- }
- }
- $output.= "\r\n";
- }
- return rtrim($output);
- }
-
- function getScreen()
- {
- return '<pre style="color: white; background: black" width="' . ($this->max_x + 1) . '">' . $this->_getScreen() . '</pre>';
- }
-
- function getHistory()
- {
- $scrollback = '';
- for ($i = 0; $i < count($this->history); $i++) {
- for ($j = 0; $j <= $this->max_x + 1; $j++) {
- if (isset($this->history_attrs[$i][$j])) {
- $scrollback.= $this->history_attrs[$i][$j];
- }
- if (isset($this->history[$i][$j])) {
- $scrollback.= htmlspecialchars($this->history[$i][$j]);
- }
- }
- $scrollback.= "\r\n";
- }
- $scrollback.= $this->_getScreen();
- return '<pre style="color: white; background: black" width="' . ($this->max_x + 1) . '">' . $scrollback . '</pre>';
- }
- }
|