Hoa central
Termcap.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Console\Bin;
38 
39 use Hoa\Console;
40 
50 {
56  protected $options = [
57  ['terminal', Console\GetOption::NO_ARGUMENT, 't'],
58  ['file', Console\GetOption::NO_ARGUMENT, 'f'],
62  ['booleans', Console\GetOption::NO_ARGUMENT, 'b'],
63  ['numbers', Console\GetOption::NO_ARGUMENT, 'n'],
64  ['strings', Console\GetOption::NO_ARGUMENT, 's'],
65  ['help', Console\GetOption::NO_ARGUMENT, 'h'],
66  ['help', Console\GetOption::NO_ARGUMENT, '?']
67  ];
68 
69 
70 
76  public function main()
77  {
78  $tput = Console::getTput();
79 
80  while (false !== $c = $this->getOption($v)) {
81  switch ($c) {
82  case 't':
83  echo $tput->getTerm();
84 
85  return;
86 
87  case 'f':
88  echo $tput->getTerminfo();
89 
90  return;
91 
92  case 'H':
93  echo $tput->has($v) ? 1 : 0;
94 
95  return;
96 
97  case 'c':
98  echo $tput->count($v);
99 
100  return;
101 
102  case 'g':
103  echo $tput->get($v);
104 
105  return;
106 
107  case 'b':
108  $informations = $tput->getInformations();
109  static::format($informations['booleans']);
110 
111  return;
112 
113  case 'n':
114  $informations = $tput->getInformations();
115  static::format($informations['numbers']);
116 
117  return;
118 
119  case 's':
120  $informations = $tput->getInformations();
121  static::format($informations['strings']);
122 
123  return;
124 
125  case '__ambiguous':
126  $this->resolveOptionAmbiguity($v);
127 
128  break;
129 
130  case 'h':
131  case '?':
132  default:
133  return $this->usage();
134  }
135  }
136 
137  return $this->usage();
138  }
139 
145  public function usage()
146  {
147  echo
148  'Usage : console:termcap', "\n",
149  'Options :', "\n",
150  $this->makeUsageOptionsList([
151  't' => 'Get terminal name.',
152  'f' => 'Get path to the terminfo file.',
153  'H' => 'Get value of a boolean capability.',
154  'c' => 'Get value of a number capability.',
155  'g' => 'Get value of a string capability.',
156  'b' => 'Get all boolean capabilites.',
157  'n' => 'Get all number capabilites.',
158  's' => 'Get all string capabilites.',
159  'help' => 'This help.'
160  ]), "\n",
161  'Examples:', "\n",
162  ' $ hoa console:termcap --count max_colors', "\n",
163  ' $ TERM=vt200 hoa console:termcap --has back_color_erase', "\n";
164 
165  return;
166  }
167 
174  public static function format(Array $data)
175  {
176  $max = 0;
177 
178  foreach ($data as $key => $_) {
179  if ($max < ($handle = strlen($key))) {
180  $max = $handle;
181  }
182  }
183 
184  $format = '%-' . ($max + 1) . 's: %s' . "\n";
185 
186  foreach ($data as $key => $value) {
187  printf(
188  $format,
189  $key,
190  is_bool($value)
191  ? ($value ? 'true' : 'false')
192  : (is_string($value)
193  ? str_replace(
194  [
195  "\033",
196  "\n",
197  ord(0xa),
198  "\r",
199  "\b",
200  "\f",
201  "\0"
202  ],
203  [
204  '\e',
205  '\n',
206  '\l',
207  '\r',
208  '\b',
209  '\f',
210  '\0'
211  ],
212  $value
213  )
214  : $value)
215  );
216  }
217 
218  return;
219  }
220 }
221 
222 __halt_compiler();
223 Terminal capabilities.
makeUsageOptionsList(Array $definitions=[])
Definition: Kit.php:149
static format(Array $data)
Definition: Termcap.php:174
static getTput()
Definition: Console.php:306
getOption(&$optionValue, $short=null)
Definition: Kit.php:104
resolveOptionAmbiguity(Array $solutions)
Definition: Kit.php:190