Hoa central
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Hoa\Console\Tput Class Reference

Public Member Functions

 __construct ($terminfo=null)
 
 getInformations ()
 
 has ($boolean)
 
 count ($number)
 
 get ($string)
 

Static Public Member Functions

static getTerm ()
 
static getTerminfo ($term=null)
 

Protected Member Functions

 parse ($terminfo)
 

Protected Attributes

 $_informations = []
 

Static Protected Attributes

static $_booleans
 
static $_numbers
 
static $_strings
 

Detailed Description

Class .

Query terminfo database. Resources: • http://man.cx/terminfo(5),http://pubs.opengroup.org/onlinepubs/7908799/xcurses/terminfo.html,

Definition at line 50 of file Tput.php.

Constructor & Destructor Documentation

Hoa\Console\Tput::__construct (   $terminfo = null)

Set stream and parse.

Parameters
string$terminfoTerminfo file.
Returns
void

Definition at line 591 of file Tput.php.

592  {
593  if (null === $terminfo) {
594  $terminfo = static::getTerminfo();
595  }
596 
597  $this->parse($terminfo);
598 
599  return;
600  }
parse($terminfo)
Definition: Tput.php:609

Here is the call graph for this function:

Member Function Documentation

Hoa\Console\Tput::count (   $number)

Get a number value.

Parameters
int$numberNumber.
Returns
int

Definition at line 755 of file Tput.php.

756  {
757  if (!isset($this->_informations['numbers'][$number])) {
758  return 0;
759  }
760 
761  return $this->_informations['numbers'][$number];
762  }
Hoa\Console\Tput::get (   $string)

Get a string value.

Parameters
string$stringString.
Returns
int

Definition at line 770 of file Tput.php.

771  {
772  if (!isset($this->_informations['strings'][$string])) {
773  return null;
774  }
775 
776  return $this->_informations['strings'][$string];
777  }
Hoa\Console\Tput::getInformations ( )

Get all informations.

Returns
array

Definition at line 729 of file Tput.php.

730  {
731  return $this->_informations;
732  }
static Hoa\Console\Tput::getTerm ( )
static

Get current term profile.

Returns
string

Definition at line 784 of file Tput.php.

785  {
786  return
787  isset($_SERVER['TERM'])
788  ? $_SERVER['TERM']
789  : (OS_WIN ? 'windows-ansi' : 'xterm');
790  }
static Hoa\Console\Tput::getTerminfo (   $term = null)
static

Get pathname to the current terminfo.

Parameters
string$termTerm.
Returns
string

Definition at line 798 of file Tput.php.

799  {
800  $paths = [];
801 
802  if (isset($_SERVER['TERMINFO'])) {
803  $paths[] = $_SERVER['TERMINFO'];
804  }
805 
806  if (isset($_SERVER['HOME'])) {
807  $paths[] = $_SERVER['HOME'] . DS . '.terminfo';
808  }
809 
810  if (isset($_SERVER['TERMINFO_DIRS'])) {
811  foreach (explode(':', $_SERVER['TERMINFO_DIRS']) as $path) {
812  $paths[] = $path;
813  }
814  }
815 
816  $paths[] = '/usr/share/terminfo';
817  $paths[] = '/usr/share/lib/terminfo';
818  $paths[] = '/lib/terminfo';
819  $paths[] = '/usr/lib/terminfo';
820  $paths[] = '/usr/local/share/terminfo';
821  $paths[] = '/usr/local/share/lib/terminfo';
822  $paths[] = '/usr/local/lib/terminfo';
823  $paths[] = '/usr/local/ncurses/lib/terminfo';
824  $paths[] = 'hoa://Library/Console/Terminfo';
825 
826  $term = $term ?: static::getTerm();
827  $fileHexa = dechex(ord($term[0])) . DS . $term;
828  $fileAlpha = $term[0] . DS . $term;
829  $pathname = null;
830 
831  foreach ($paths as $path) {
832  if (file_exists($_ = $path . DS . $fileHexa) ||
833  file_exists($_ = $path . DS . $fileAlpha)) {
834  $pathname = $_;
835 
836  break;
837  }
838  }
839 
840  return $pathname;
841  }
Hoa\Console\Tput::has (   $boolean)

Get a boolean value.

Parameters
bool$booleanBoolean.
Returns
bool

Definition at line 740 of file Tput.php.

741  {
742  if (!isset($this->_informations['booleans'][$boolean])) {
743  return false;
744  }
745 
746  return $this->_informations['booleans'][$boolean];
747  }
Hoa\Console\Tput::parse (   $terminfo)
protected

Parse.

Parameters
string$terminfoTerminfo file.
Returns
array
Exceptions

Definition at line 609 of file Tput.php.

610  {
611  if (!file_exists($terminfo)) {
612  throw new Exception(
613  'Terminfo file %s does not exist.',
614  0,
615  $terminfo
616  );
617  }
618 
619  $data = file_get_contents($terminfo);
620  $length = strlen($data);
621  $out = ['file' => $terminfo];
622 
623  $headers = [
624  'data_size' => $length,
625  'header_size' => 12,
626  'magic_number' => (ord($data[ 1]) << 8) | ord($data[ 0]),
627  'names_size' => (ord($data[ 3]) << 8) | ord($data[ 2]),
628  'bool_count' => (ord($data[ 5]) << 8) | ord($data[ 4]),
629  'number_count' => (ord($data[ 7]) << 8) | ord($data[ 6]),
630  'string_count' => (ord($data[ 9]) << 8) | ord($data[ 8]),
631  'string_table_size' => (ord($data[11]) << 8) | ord($data[10]),
632  ];
633  $out['headers'] = $headers;
634 
635 
636  // Names.
637  $i = $headers['header_size'];
638 
639  list($out['name'], $out['description']) =
640  explode('|', substr($data, $i, $headers['names_size'] - 1));
641 
642  // Booleans.
643  $i += $headers['names_size'];
644  $booleans = [];
645  $booleanNames = &static::$_booleans;
646 
647  for (
648  $e = 0, $max = $i + $headers['bool_count'];
649  $i < $max;
650  ++$e, ++$i
651  ) {
652  $booleans[$booleanNames[$e]] = 1 === ord($data[$i]);
653  }
654 
655  $out['booleans'] = $booleans;
656 
657  // Numbers.
658  if (1 === ($i % 2)) {
659  ++$i;
660  }
661 
662  $numbers = [];
663  $numberNames = &static::$_numbers;
664 
665  for (
666  $e = 0, $max = $i + $headers['number_count'] * 2;
667  $i < $max;
668  ++$e, $i += 2
669  ) {
670  $name = $numberNames[$e];
671  $data_i0 = ord($data[$i ]);
672  $data_i1 = ord($data[$i + 1]);
673 
674  if ($data_i1 === 255 && $data_i0 === 255) {
675  $numbers[$name] = -1;
676  } else {
677  $numbers[$name] = ($data_i1 << 8) | $data_i0;
678  }
679  }
680 
681  $out['numbers'] = $numbers;
682 
683  // Strings.
684  $strings = [];
685  $stringNames = &static::$_strings;
686  $ii = $i + $headers['string_count'] * 2;
687 
688  for (
689  $e = 0, $max = $ii;
690  $i < $max;
691  ++$e, $i += 2
692  ) {
693  $name = $stringNames[$e];
694  $data_i0 = ord($data[$i ]);
695  $data_i1 = ord($data[$i + 1]);
696 
697  if ($data_i1 === 255 && $data_i0 === 255) {
698  continue;
699  }
700 
701  $a = ($data_i1 << 8) | $data_i0;
702  $strings[$name] = $a;
703 
704  if (65534 === $a) {
705  continue;
706  }
707 
708  $b = $ii + $a;
709  $c = $b;
710 
711  while ($c < $length && ord($data[$c])) {
712  $c++;
713  }
714 
715  $value = substr($data, $b, $c - $b);
716  $strings[$name] = false !== $value ? $value : null;
717  }
718 
719  $out['strings'] = $strings;
720 
721  return $this->_informations = $out;
722  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Console\Tput::$_booleans
staticprotected

Definition at line 57 of file Tput.php.

Hoa\Console\Tput::$_informations = []
protected

Definition at line 581 of file Tput.php.

Hoa\Console\Tput::$_numbers
staticprotected

Definition at line 110 of file Tput.php.

Hoa\Console\Tput::$_strings
staticprotected

Definition at line 158 of file Tput.php.


The documentation for this class was generated from the following file: