/wp-content/plugins/jekyll-exporter/vendor/wp-cli/php-cli-tools/lib/cli/Notify.php

https://github.com/lostechies/wordpress · PHP · 185 lines · 78 code · 21 blank · 86 comment · 8 complexity · 1bc4f363ba888bbc273461cccd0bd126 MD5 · raw file

  1. <?php
  2. /**
  3. * PHP Command Line Tools
  4. *
  5. * This source file is subject to the MIT license that is bundled
  6. * with this package in the file LICENSE.
  7. *
  8. * @author James Logsdon <dwarf@girsbrain.org>
  9. * @copyright 2010 James Logsdom (http://girsbrain.org)
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. */
  12. namespace cli;
  13. use cli\Streams;
  14. /**
  15. * The `Notify` class is the basis of all feedback classes, such as Indicators
  16. * and Progress meters. The default behaviour is to refresh output after 100ms
  17. * have passed. This is done to preventing the screen from flickering and keep
  18. * slowdowns from output to a minimum.
  19. *
  20. * The most basic form of Notifier has no maxim, and simply displays a series
  21. * of characters to indicate progress is being made.
  22. */
  23. abstract class Notify {
  24. protected $_current = 0;
  25. protected $_first = true;
  26. protected $_interval;
  27. protected $_message;
  28. protected $_start;
  29. protected $_timer;
  30. /**
  31. * Instatiates a Notification object.
  32. *
  33. * @param string $msg The text to display next to the Notifier.
  34. * @param int $interval The interval in milliseconds between updates.
  35. */
  36. public function __construct($msg, $interval = 100) {
  37. $this->_message = $msg;
  38. $this->_interval = (int)$interval;
  39. }
  40. /**
  41. * This method should be used to print out the Notifier. This method is
  42. * called from `cli\Notify::tick()` after `cli\Notify::$_interval` has passed.
  43. *
  44. * @abstract
  45. * @param boolean $finish
  46. * @see cli\Notify::tick()
  47. */
  48. abstract public function display($finish = false);
  49. /**
  50. * Reset the notifier state so the same instance can be used in multiple loops.
  51. */
  52. public function reset() {
  53. $this->_current = 0;
  54. $this->_first = true;
  55. $this->_start = null;
  56. $this->_timer = null;
  57. }
  58. /**
  59. * Returns the formatted tick count.
  60. *
  61. * @return string The formatted tick count.
  62. */
  63. public function current() {
  64. return number_format($this->_current);
  65. }
  66. /**
  67. * Calculates the time elapsed since the Notifier was first ticked.
  68. *
  69. * @return int The elapsed time in seconds.
  70. */
  71. public function elapsed() {
  72. if (!$this->_start) {
  73. return 0;
  74. }
  75. $elapsed = time() - $this->_start;
  76. return $elapsed;
  77. }
  78. /**
  79. * Calculates the speed (number of ticks per second) at which the Notifier
  80. * is being updated.
  81. *
  82. * @return int The number of ticks performed in 1 second.
  83. */
  84. public function speed() {
  85. static $tick, $iteration = 0, $speed = 0;
  86. if (!$this->_start) {
  87. return 0;
  88. } else if (!$tick) {
  89. $tick = $this->_start;
  90. }
  91. $now = microtime(true);
  92. $span = $now - $tick;
  93. if ($span > 1) {
  94. $iteration++;
  95. $tick = $now;
  96. $speed = ($this->_current / $iteration) / $span;
  97. }
  98. return $speed;
  99. }
  100. /**
  101. * Takes a time span given in seconds and formats it for display. The
  102. * returned string will be in MM:SS form.
  103. *
  104. * @param int $time The time span in seconds to format.
  105. * @return string The formatted time span.
  106. */
  107. public function formatTime($time) {
  108. return floor($time / 60) . ':' . str_pad($time % 60, 2, 0, STR_PAD_LEFT);
  109. }
  110. /**
  111. * Finish our Notification display. Should be called after the Notifier is
  112. * no longer needed.
  113. *
  114. * @see cli\Notify::display()
  115. */
  116. public function finish() {
  117. Streams::out("\r");
  118. $this->display(true);
  119. Streams::line();
  120. }
  121. /**
  122. * Increments are tick counter by the given amount. If no amount is provided,
  123. * the ticker is incremented by 1.
  124. *
  125. * @param int $increment The amount to increment by.
  126. */
  127. public function increment($increment = 1) {
  128. $this->_current += $increment;
  129. }
  130. /**
  131. * Determines whether the display should be updated or not according to
  132. * our interval setting.
  133. *
  134. * @return boolean `true` if the display should be updated, `false` otherwise.
  135. */
  136. public function shouldUpdate() {
  137. $now = microtime(true) * 1000;
  138. if (empty($this->_timer)) {
  139. $this->_start = (int)(($this->_timer = $now) / 1000);
  140. return true;
  141. }
  142. if (($now - $this->_timer) > $this->_interval) {
  143. $this->_timer = $now;
  144. return true;
  145. }
  146. return false;
  147. }
  148. /**
  149. * This method is the meat of all Notifiers. First we increment the ticker
  150. * and then update the display if enough time has passed since our last tick.
  151. *
  152. * @param int $increment The amount to increment by.
  153. * @see cli\Notify::increment()
  154. * @see cli\Notify::shouldUpdate()
  155. * @see cli\Notify::display()
  156. */
  157. public function tick($increment = 1) {
  158. $this->increment($increment);
  159. if ($this->shouldUpdate()) {
  160. Streams::out("\r");
  161. $this->display();
  162. }
  163. }
  164. }