Hoa central
Public Member Functions | Static Public Member Functions | Static Private Attributes | List of all members
Hoa\Console\Window Class Reference
Inheritance diagram for Hoa\Console\Window:

Public Member Functions

 __construct ()
 

Static Public Member Functions

static getInstance ()
 
static setSize ($x, $y)
 
static getSize ()
 
static moveTo ($x, $y)
 
static getPosition ()
 
static scroll ($directions, $repeat=1)
 
static minimize ()
 
static restore ()
 
static raise ()
 
static lower ()
 
static setTitle ($title)
 
static getTitle ()
 
static getLabel ()
 
static refresh ()
 
static copy ($data)
 

Static Private Attributes

static $_instance = null
 

Detailed Description

Class .

Allow to manipulate the window.

We can listen the event channel hoa://Event/Console/Window:resize to detect if the window has been resized. Please, see the constructor documentation to get more informations.

Definition at line 53 of file Window.php.

Constructor & Destructor Documentation

Hoa\Console\Window::__construct ( )

Set the event channel. We need to declare(ticks = 1) in the main script to ensure that the event is fired. Also, we need the pcntl_signal() function enabled.

Returns
void

Definition at line 71 of file Window.php.

72  {
73  Core\Event::register(
74  'hoa://Event/Console/Window:resize',
75  $this
76  );
77 
78  return;
79  }

Member Function Documentation

static Hoa\Console\Window::copy (   $data)
static

Set clipboard value.

Parameters
string$dataData to copy.
Returns
void

Definition at line 523 of file Window.php.

524  {
525  if (OS_WIN) {
526  return;
527  }
528 
529  echo "\033]52;;" . base64_encode($data) . "\033\\";
530 
531  return;
532  }
static Hoa\Console\Window::getInstance ( )
static

Singleton.

Returns

Definition at line 86 of file Window.php.

87  {
88  if (null === static::$_instance) {
89  static::$_instance = new static();
90  }
91 
92  return static::$_instance;
93  }
static Hoa\Console\Window::getLabel ( )
static

Get label.

Returns
string

Definition at line 460 of file Window.php.

461  {
462  if (OS_WIN) {
463  return;
464  }
465 
466  // DECSLPP.
467  echo "\033[20t";
468 
469  $read = [STDIN];
470  $write = [];
471  $except = [];
472  $out = null;
473 
474  if (0 === stream_select($read, $write, $except, 0, 50000)) {
475  return $out;
476  }
477 
478  // Read \033]L<label>\033\
479  fread(STDIN, 3); // skip \033, ] and L.
480 
481  do {
482  $char = fread(STDIN, 1);
483 
484  if ("\033" === $char) {
485  $chaar = fread(STDIN, 1);
486 
487  if ('\\' === $chaar) {
488  break;
489  }
490 
491  $char .= $chaar;
492  }
493 
494  $out .= $char;
495  } while (true);
496 
497  return $out;
498  }
static Hoa\Console\Window::getPosition ( )
static

Get current position (x and y) of the window (in pixels).

Returns
array

Definition at line 223 of file Window.php.

224  {
225  if (OS_WIN) {
226  return;
227  }
228 
229  // DECSLPP.
230  echo "\033[13t";
231 
232  // Read \033[3;x;yt.
233  fread(STDIN, 4); // skip \033, [, 3 and ;.
234 
235  $x = null;
236  $y = null;
237  $handle = &$x;
238 
239  do {
240  $char = fread(STDIN, 1);
241 
242  switch ($char) {
243  case ';':
244  $handle = &$y;
245 
246  break;
247 
248  case 't':
249  break 2;
250 
251  default:
252  $handle .= $char;
253  }
254  } while (true);
255 
256  return [
257  'x' => (int) $x,
258  'y' => (int) $y
259  ];
260  }
static Hoa\Console\Window::getSize ( )
static

Get current size (x and y) of the window.

Returns
array

Definition at line 118 of file Window.php.

119  {
120  if (OS_WIN) {
121  $modecon = explode("\n", ltrim(Processus::execute('mode con')));
122 
123  $_y = trim($modecon[2]);
124  preg_match('#[^:]+:\s*([0-9]+)#', $_y, $matches);
125  $y = (int) $matches[1];
126 
127  $_x = trim($modecon[3]);
128  preg_match('#[^:]+:\s*([0-9]+)#', $_x, $matches);
129  $x = (int) $matches[1];
130 
131  return [
132  'x' => $x,
133  'y' => $y
134  ];
135  }
136 
137  $term = '';
138 
139  if (isset($_SERVER['TERM'])) {
140  $term = 'TERM="' . $_SERVER['TERM'] . '" ';
141  }
142 
143  $command = $term . 'tput cols && ' . $term . 'tput lines';
144  $tput = Processus::execute($command, false);
145 
146  if (!empty($tput)) {
147  list($x, $y) = explode("\n", $tput);
148 
149  return [
150  'x' => intval($x),
151  'y' => intval($y)
152  ];
153  }
154 
155  // DECSLPP.
156  echo "\033[18t";
157 
158  // Read \033[8;y;xt.
159  fread(STDIN, 4); // skip \033, [, 8 and ;.
160 
161  $x = null;
162  $y = null;
163  $handle = &$y;
164 
165  do {
166  $char = fread(STDIN, 1);
167 
168  switch ($char) {
169  case ';':
170  $handle = &$x;
171 
172  break;
173 
174  case 't':
175  break 2;
176 
177  default:
178  if (false === ctype_digit($char)) {
179  break 2;
180  }
181 
182  $handle .= $char;
183  }
184  } while (true);
185 
186  if (null === $x || null === $y) {
187  return [
188  'x' => 0,
189  'y' => 0
190  ];
191  }
192 
193  return [
194  'x' => (int) $x,
195  'y' => (int) $y
196  ];
197  }
static execute($commandLine, $escape=true)
Definition: Processus.php:1165

Here is the call graph for this function:

Here is the caller graph for this function:

static Hoa\Console\Window::getTitle ( )
static

Get title.

Returns
string

Definition at line 415 of file Window.php.

416  {
417  if (OS_WIN) {
418  return;
419  }
420 
421  // DECSLPP.
422  echo "\033[21t";
423 
424  $read = [STDIN];
425  $write = [];
426  $except = [];
427  $out = null;
428 
429  if (0 === stream_select($read, $write, $except, 0, 50000)) {
430  return $out;
431  }
432 
433  // Read \033]l<title>\033\
434  fread(STDIN, 3); // skip \033, ] and l.
435 
436  do {
437  $char = fread(STDIN, 1);
438 
439  if ("\033" === $char) {
440  $chaar = fread(STDIN, 1);
441 
442  if ('\\' === $chaar) {
443  break;
444  }
445 
446  $char .= $chaar;
447  }
448 
449  $out .= $char;
450  } while (true);
451 
452  return $out;
453  }
static Hoa\Console\Window::lower ( )
static

Lower the window to the bottom of the stacking order.

Returns
void

Definition at line 381 of file Window.php.

382  {
383  if (OS_WIN) {
384  return;
385  }
386 
387  echo "\033[6t";
388 
389  return;
390  }
static Hoa\Console\Window::minimize ( )
static

Minimize the window.

Returns
void

Definition at line 332 of file Window.php.

333  {
334  if (OS_WIN) {
335  return;
336  }
337 
338  // DECSLPP.
339  echo "\033[2t";
340 
341  return;
342  }
static Hoa\Console\Window::moveTo (   $x,
  $y 
)
static

Move to X and Y (in pixels).

Parameters
int$xX coordinate.
int$yY coordinate.
Returns
void

Definition at line 206 of file Window.php.

207  {
208  if (OS_WIN) {
209  return;
210  }
211 
212  // DECSLPP.
213  echo "\033[3;" . $x . ";" . $y . "t";
214 
215  return;
216  }
static Hoa\Console\Window::raise ( )
static

Raise the window to the front of the stacking order.

Returns
void

Definition at line 365 of file Window.php.

366  {
367  if (OS_WIN) {
368  return;
369  }
370 
371  echo "\033[5t";
372 
373  return;
374  }
static Hoa\Console\Window::refresh ( )
static

Refresh the window.

Returns
void

Definition at line 505 of file Window.php.

506  {
507  if (OS_WIN) {
508  return;
509  }
510 
511  // DECSLPP.
512  echo "\033[7t";
513 
514  return;
515  }
static Hoa\Console\Window::restore ( )
static

Restore the window (de-minimize).

Returns
void

Definition at line 349 of file Window.php.

350  {
351  if (OS_WIN) {
352  return;
353  }
354 
355  echo "\033[1t";
356 
357  return;
358  }
static Hoa\Console\Window::scroll (   $directions,
  $repeat = 1 
)
static

Scroll whole page. Directions can be: • u, up, ↑ : scroll whole page up; • d, down, ↓ : scroll whole page down. Directions can be concatenated by a single space.

Parameters
string$directionsDirections.
int$repeatHow many times do we scroll? void

Definition at line 273 of file Window.php.

274  {
275  if (OS_WIN) {
276  return;
277  }
278 
279  if (1 > $repeat) {
280  return;
281  } elseif (1 === $repeat) {
282  $handle = explode(' ', $directions);
283  } else {
284  $handle = explode(' ', $directions, 1);
285  }
286 
287  $tput = Console::getTput();
288  $count = ['up' => 0, 'down' => 0];
289 
290  foreach ($handle as $direction) {
291  switch ($direction) {
292  case 'u':
293  case 'up':
294  case '↑':
295  ++$count['up'];
296 
297  break;
298 
299  case 'd':
300  case 'down':
301  case '↓':
302  ++$count['down'];
303 
304  break;
305  }
306  }
307 
308  if (0 < $count['up']) {
309  echo str_replace(
310  '%p1%d',
311  $count['up'] * $repeat,
312  $tput->get('parm_index')
313  );
314  }
315 
316  if (0 < $count['down']) {
317  echo str_replace(
318  '%p1%d',
319  $count['down'] * $repeat,
320  $tput->get('parm_rindex')
321  );
322  }
323 
324  return;
325  }
static getTput()
Definition: Console.php:306

Here is the call graph for this function:

Here is the caller graph for this function:

static Hoa\Console\Window::setSize (   $x,
  $y 
)
static

Set size to X lines and Y columns.

Parameters
int$xX coordinate.
int$yY coordinate.
Returns
void

Definition at line 102 of file Window.php.

103  {
104  if (OS_WIN) {
105  return;
106  }
107 
108  echo "\033[8;" . $y . ";" . $x . "t";
109 
110  return;
111  }
static Hoa\Console\Window::setTitle (   $title)
static

Set title.

Parameters
string$titleTitle.
Returns
void

Definition at line 398 of file Window.php.

399  {
400  if (OS_WIN) {
401  return;
402  }
403 
404  // DECSLPP.
405  echo "\033]0;" . $title . "\033\\";
406 
407  return;
408  }

Member Data Documentation

Hoa\Console\Window::$_instance = null
staticprivate

Definition at line 60 of file Window.php.


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