100 lines
2.5 KiB
PHP
100 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\Histogram\Output;
|
|
|
|
use NoccyLabs\Histogram\Buffer;
|
|
|
|
class TerminalHistogram
|
|
{
|
|
|
|
public static $MaxWidth = 50;
|
|
|
|
private $width;
|
|
|
|
private $max = null;
|
|
|
|
/** @var Buffer */
|
|
private $buffer;
|
|
|
|
private $bars = [
|
|
" ",
|
|
"\u{2581}",
|
|
"\u{2582}",
|
|
"\u{2583}",
|
|
"\u{2584}",
|
|
"\u{2585}",
|
|
"\u{2586}",
|
|
"\u{2587}",
|
|
"\u{2588}",
|
|
];
|
|
|
|
public function __construct(int $width, Buffer $buffer=null)
|
|
{
|
|
$this->width = max(1, min($width, self::$MaxWidth));
|
|
$this->buffer = $buffer ?? new Buffer(50);
|
|
}
|
|
|
|
public function getBuffer(): Buffer
|
|
{
|
|
return $this->buffer;
|
|
}
|
|
|
|
public function setMax(?float $max)
|
|
{
|
|
$this->max = $max;
|
|
}
|
|
|
|
public function getHistogram()
|
|
{
|
|
$max = ($this->max === null)?1.0:$this->max;
|
|
$samples = $this->buffer->getMappedSamples(0.0, $max, 0, 8, $this->width);
|
|
$output = null;
|
|
if (count($samples) < $this->width) {
|
|
$output .= str_repeat(" ", $this->width - count($samples));
|
|
}
|
|
foreach ($samples as $sample) {
|
|
if ($sample === null) {
|
|
$output .= "\u{2a09}";
|
|
} else {
|
|
$output .= $this->bars[floor($sample)];
|
|
}
|
|
}
|
|
return $output;
|
|
}
|
|
|
|
public function getMultiHistogram(int $lines): array
|
|
{
|
|
$max = ($this->max === null)?1.0:$this->max;
|
|
$samples = $this->buffer->getMappedSamples(0.0, $max, 0, 8*$lines, $this->width);
|
|
$result = [];
|
|
$topmin = 8*($lines-1);
|
|
for ($line = 0; $line < $lines; $line++) {
|
|
$rowmin = $topmin - ($line * 8);
|
|
$output = null;
|
|
if (count($samples) < $this->width) {
|
|
$output .= str_repeat(" ", $this->width - count($samples));
|
|
}
|
|
foreach ($samples as $sample) {
|
|
if ($sample === null) {
|
|
$output .= "\u{2a09}";
|
|
} else {
|
|
$remain = $sample - $rowmin;
|
|
if ($remain <= 0) {
|
|
$output.= " ";
|
|
} else {
|
|
$sample = min(8, $remain);
|
|
$output .= $this->bars[floor($sample)];
|
|
}
|
|
}
|
|
}
|
|
$result[] = $output;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function __toString()
|
|
{
|
|
return $this->getHistogram();
|
|
}
|
|
|
|
} |