Skip to content

Commit 2bc8b56

Browse files
committed
Loader helper
1 parent f37b0b6 commit 2bc8b56

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lib/Psf/Helpers/Loader.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Loader
4+
*
5+
* @author Piotr Olaszewski
6+
*/
7+
namespace Psf\Helpers;
8+
9+
use Psf\Output\Writer;
10+
11+
class Loader
12+
{
13+
/**
14+
* @var Writer
15+
*/
16+
private $_output;
17+
private $_charSequence = array('|', '/', '-', '\\', '|');
18+
private $_currentSequenceId = 0;
19+
private $_loaderText = 'Loading ';
20+
21+
public function initialize(Writer $output)
22+
{
23+
$this->_output = $output;
24+
}
25+
26+
public function start()
27+
{
28+
if ($this->_currentSequenceId == sizeof($this->_charSequence)) {
29+
$this->_currentSequenceId = 0;
30+
}
31+
$char = $this->_charSequence[$this->_currentSequenceId];
32+
$this->_output->writeMessage(Writer::ANSI_CLEAR_LINE . $this->_loaderText . $char, 1, Writer::CR);
33+
$this->_currentSequenceId++;
34+
}
35+
36+
public function setCharSequence($charSequence)
37+
{
38+
$this->_charSequence = $charSequence;
39+
$this->setCurrentSequenceId(0);
40+
}
41+
42+
public function setCurrentSequenceId($currentSequenceId)
43+
{
44+
$this->_currentSequenceId = $currentSequenceId;
45+
}
46+
47+
public function setLoaderText($loaderText)
48+
{
49+
$this->_loaderText = $loaderText;
50+
}
51+
}

lib/Psf/Output/Writer.php

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Writer
1717
const VERBOSITY_VERBOSE = 2;
1818
const LF = PHP_EOL;
1919
const CR = "\r";
20+
const ANSI_CLEAR_LINE = "\033[K";
2021

2122
private $_streamHandle;
2223
private $_formatters = array();

0 commit comments

Comments
 (0)