Skip to content

Commit

Permalink
Add support for keys to ->progress()->each()
Browse files Browse the repository at this point in the history
  • Loading branch information
duncan3dc committed Oct 23, 2017
1 parent bc712d5 commit 75e37ff
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
31 changes: 22 additions & 9 deletions src/TerminalObject/Dynamic/Progress.php
Expand Up @@ -134,24 +134,37 @@ public function forceRedraw($force = true)
return $this;
}


/**
* Loop through the data and self manage the progress bar advancement
* Update a progress bar using an iterable.
*
* @param mixed $data Array or any other iterable object
* @param callable $callback
* @param iterable $items Array or any other iterable object
* @param callable $callback A handler to run on each item
*/
public function each($data, callable $callback)
public function each($items, callable $callback = null)
{
if (!$this->total) {
$this->total(count($data));
if ($items instanceof \Traversable) {
$items = iterator_to_array($items);
}

foreach ($data as $item) {
$callback($item);
$this->advance();
$total = count($items);
if (!$total) {
return;
}
$this->total($total);

foreach ($items as $key => $item) {
if ($callback) {
$label = $callback($item, $key);
} else {
$label = null;
}

$this->advance(1, $label);
}
}


/**
* Draw the progress bar, if necessary
*
Expand Down
55 changes: 43 additions & 12 deletions tests/ProgressTest.php
Expand Up @@ -291,23 +291,54 @@ public function it_will_not_force_a_redraw_if_disabled()
}
}

/** @test */
public function it_can_self_manage_progress_bar_while_looping()

public function testEach1()
{
$this->shouldWrite('');
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");

$this->cli->progress()->each([1, 2]);
}
public function testEach2()
{
$this->shouldWrite('');
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(10)} 10%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(20)} 20%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(30)} 30%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(40)} 40%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(60)} 60%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(70)} 70%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(80)} 80%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(90)} 90%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");

$this->cli->progress()->each(range(1, 10), function ($item) {
return true;
$items = [];

$this->cli->progress()->each(["two", "one"], function ($item) use (&$items) {
$items[] = $item;
});

$this->assertSame(["two", "one"], $items);
}
public function testEach3()
{
$this->shouldWrite('');
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(50)} 50%\e[0m");
$this->shouldWrite("\e[m\e[1A\r\e[K{$this->repeat(100)} 100%\e[0m");

$items = [];

$this->cli->progress()->each(["key2" => "two", "key1" => "one"], function ($item, $key) use (&$items) {
$items[$key] = $item;
});

$this->assertSame(["key2" => "two", "key1" => "one"], $items);
}
public function testEach4()
{
$this->shouldWrite('');
$this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(20)} 20%\n\r\e[Kone\e[0m");
$this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(40)} 40%\n\r\e[Ktwo\e[0m");
$this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(60)} 60%\n\r\e[Kthree\e[0m");
$this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(80)} 80%\n\r\e[Kfour\e[0m");
$this->shouldWrite("\e[m\e[2A\r\e[K{$this->repeat(100)} 100%\n\r\e[Kfive\e[0m");

$this->cli->progress()->each(["one", "two", "three", "four", "five"], function ($item) {
return $item;
});
}
}

0 comments on commit 75e37ff

Please sign in to comment.