Termcap.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Console\Bin;
  36. use Hoa\Console;
  37. /**
  38. * Class Hoa\Console\Bin\Termcap.
  39. *
  40. * Get terminal capabilities.
  41. *
  42. * @copyright Copyright © 2007-2017 Hoa community
  43. * @license New BSD License
  44. */
  45. class Termcap extends Console\Dispatcher\Kit
  46. {
  47. /**
  48. * Options description.
  49. *
  50. * @var array
  51. */
  52. protected $options = [
  53. ['terminal', Console\GetOption::NO_ARGUMENT, 't'],
  54. ['file', Console\GetOption::NO_ARGUMENT, 'f'],
  55. ['has', Console\GetOption::REQUIRED_ARGUMENT, 'H'],
  56. ['count', Console\GetOption::REQUIRED_ARGUMENT, 'c'],
  57. ['get', Console\GetOption::REQUIRED_ARGUMENT, 'g'],
  58. ['booleans', Console\GetOption::NO_ARGUMENT, 'b'],
  59. ['numbers', Console\GetOption::NO_ARGUMENT, 'n'],
  60. ['strings', Console\GetOption::NO_ARGUMENT, 's'],
  61. ['help', Console\GetOption::NO_ARGUMENT, 'h'],
  62. ['help', Console\GetOption::NO_ARGUMENT, '?']
  63. ];
  64. /**
  65. * The entry method.
  66. *
  67. * @return void
  68. */
  69. public function main()
  70. {
  71. $tput = Console::getTput();
  72. while (false !== $c = $this->getOption($v)) {
  73. switch ($c) {
  74. case 't':
  75. echo $tput->getTerm();
  76. return;
  77. case 'f':
  78. echo $tput->getTerminfo();
  79. return;
  80. case 'H':
  81. echo $tput->has($v) ? 1 : 0;
  82. return;
  83. case 'c':
  84. echo $tput->count($v);
  85. return;
  86. case 'g':
  87. echo $tput->get($v);
  88. return;
  89. case 'b':
  90. $informations = $tput->getInformations();
  91. static::format($informations['booleans']);
  92. return;
  93. case 'n':
  94. $informations = $tput->getInformations();
  95. static::format($informations['numbers']);
  96. return;
  97. case 's':
  98. $informations = $tput->getInformations();
  99. static::format($informations['strings']);
  100. return;
  101. case '__ambiguous':
  102. $this->resolveOptionAmbiguity($v);
  103. break;
  104. case 'h':
  105. case '?':
  106. default:
  107. return $this->usage();
  108. }
  109. }
  110. return $this->usage();
  111. }
  112. /**
  113. * The command usage.
  114. *
  115. * @return void
  116. */
  117. public function usage()
  118. {
  119. echo
  120. 'Usage : console:termcap', "\n",
  121. 'Options :', "\n",
  122. $this->makeUsageOptionsList([
  123. 't' => 'Get terminal name.',
  124. 'f' => 'Get path to the terminfo file.',
  125. 'H' => 'Get value of a boolean capability.',
  126. 'c' => 'Get value of a number capability.',
  127. 'g' => 'Get value of a string capability.',
  128. 'b' => 'Get all boolean capabilites.',
  129. 'n' => 'Get all number capabilites.',
  130. 's' => 'Get all string capabilites.',
  131. 'help' => 'This help.'
  132. ]), "\n",
  133. 'Examples:', "\n",
  134. ' $ hoa console:termcap --count max_colors', "\n",
  135. ' $ TERM=vt200 hoa console:termcap --has back_color_erase', "\n";
  136. return;
  137. }
  138. /**
  139. * Format a collection of informations.
  140. *
  141. * @param array $data Data.
  142. * @return void
  143. */
  144. public static function format(array $data)
  145. {
  146. $max = 0;
  147. foreach ($data as $key => $_) {
  148. if ($max < ($handle = strlen($key))) {
  149. $max = $handle;
  150. }
  151. }
  152. $format = '%-' . ($max + 1) . 's: %s' . "\n";
  153. foreach ($data as $key => $value) {
  154. printf(
  155. $format,
  156. $key,
  157. is_bool($value)
  158. ? ($value ? 'true' : 'false')
  159. : (is_string($value)
  160. ? str_replace(
  161. [
  162. "\033",
  163. "\n",
  164. ord(0xa),
  165. "\r",
  166. "\b",
  167. "\f",
  168. "\0"
  169. ],
  170. [
  171. '\e',
  172. '\n',
  173. '\l',
  174. '\r',
  175. '\b',
  176. '\f',
  177. '\0'
  178. ],
  179. $value
  180. )
  181. : $value)
  182. );
  183. }
  184. return;
  185. }
  186. }
  187. __halt_compiler();
  188. Terminal capabilities.