From fc645445bbaf61c7a9c82633c984fce581222952 Mon Sep 17 00:00:00 2001 From: Momchil Bozhinov Date: Mon, 8 Nov 2021 22:53:25 +0200 Subject: [PATCH 1/2] * Backport justify --- cli-graph-ml.class.php | 75 ++++++++++++------------------------------ 1 file changed, 21 insertions(+), 54 deletions(-) diff --git a/cli-graph-ml.class.php b/cli-graph-ml.class.php index 185371b..ff51408 100644 --- a/cli-graph-ml.class.php +++ b/cli-graph-ml.class.php @@ -557,26 +557,6 @@ private function get_up_border(){ return $chr_corner.str_pad( '', $this->count_data*$this->bar_width+2, $chr_line); // +2 = free space left and right } // /get_up_border() - /** - * Get Str Axis X values - * - * @return string $str_axis_x_values - */ - private function get_axis_x_values(){ - return $this->justify(implode(' ', $this->axis_x_values), $this->count_data*$this->bar_width + 2 ); // The left and right margin of the graph will be used - } // /get_axis_x_values() - - /** - * Get Str Axis X separators - * Used after draw down line of chart - * - * @return string $str_axis_x_separators - */ - private function get_axis_x_separators(){ - $arr_separators = array_fill(0, count($this->axis_x_values), '|'); - return $this->justify(implode(' ', $arr_separators), $this->count_data*$this->bar_width); - } // /get_axis_x_separators() - /** * Prepare Graph Lines */ @@ -735,9 +715,9 @@ public function prepare_array_output(){ $this->arr_output[] = $left.$this->get_down_border().$str_padding_right; // Axis X Separators | - $this->arr_output[] = $left.' '.$this->get_axis_x_separators().' '.$str_padding_right; + $this->arr_output[] = $left." ".$this->justify(array_fill(0, count($this->axis_x_values), "|"), -2)." ".$str_padding_right; // Axis X Values - $this->arr_output[] = $left.' '.$this->get_axis_x_values().$str_padding_right; + $this->arr_output[] = $left." ".$this->justify($this->axis_x_values).$str_padding_right; // Axis X Title if( $this->get_cfg_param( 'show_x_axis_title' ) ){ @@ -822,37 +802,24 @@ public function draw( $line_id = null, $do_line_break = true, $prepare_array_out } } // /draw() - /** - * Justify a string - * - * Original: - * https://www.iteramos.com/pregunta/22339/justificar-cadena-de-algoritmo - * - * @param string $string - * @param integer $num_chars - * @return string $justify_string - */ - private function justify($string, $num_chars){ - $s = trim($string); - $l = strlen($s); - - if($l >= $num_chars){ - $s = explode("\n", wordwrap($s, $num_chars)); - $s = $s[0]; - $l = strlen($s); - } - - $c = substr_count($s, ' '); - - if($c === 0) return str_pad($s, $num_chars, ' ', STR_PAD_BOTH); - - $a = ($num_chars-$l+$c)/$c; - $h = floor($a); - $i = ($a-$h)*$c; - $w = explode(' ', $s, $i+1); - $w[$i] = str_replace(' ', str_repeat(' ', $h), $w[$i]); - - return implode(str_repeat(' ', ceil($a)), $w); - } + /** + * Justify axis values + */ + private function justify(array $vals, int $offset = 0) + { + $limit = $this->count_data * $this->bar_width + 4 + $offset; + $s = trim(implode(" ", $vals)); + $l = strlen($s); + + if($l >= $limit){ + $ret = wordwrap($s, $limit); + } else { + $c = count($vals) - 1; + $h = ceil(($limit - $l) / $c); + $ret = str_replace(' ', str_repeat(' ', $h), $s); + } + + return $ret; + } }// /cli_graph_ml From a0441a7e2d0cc11166e66ccb1668170e3b51cb94 Mon Sep 17 00:00:00 2001 From: Momchil Bozhinov Date: Sat, 13 Nov 2021 09:17:35 +0200 Subject: [PATCH 2/2] * Fix colors for Windows --- cli-graph-ml.class.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/cli-graph-ml.class.php b/cli-graph-ml.class.php index ff51408..b9f46ae 100644 --- a/cli-graph-ml.class.php +++ b/cli-graph-ml.class.php @@ -231,6 +231,27 @@ class cli_graph_ml 'reset' => '[0m', ]; // /$text_colors + private $text_colors_win32 = [ + 'lightblue' => '[?1;34m', + 'lightred' => '[?1;31m', + 'lightgreen' => '[?1;32m', + 'lightyellow' => '[?1;33m', + 'lightblack' => '[?1;30m', + 'lightmagenta' => '[?1;35m', + 'lightcyan' => '[?1;36m', + 'lightwhite' => '[?1;37m', + 'blue' => '[0;34m', + 'red' => '[0;31m', + 'green' => '[0;32m', + 'yellow' => '[0;33m', + 'black' => '[0;30m', + 'magenta' => '[0;35m', + 'cyan' => '[0;36m', + 'white' => '[0;37m', + 'orange' => '[38;5;214m', + 'reset' => '[0m' + ]; + /** * Definition of Defaut Table Format values * @@ -289,6 +310,10 @@ public function __construct( $data = null, $axis_x_values = null, $config = null $this->set_axis_x_values( $axis_x_values ); } + if (PHP_OS_FAMILY === "Windows") { # PHP 7.2+ + $this->text_colors = $this->text_colors_win32; + } + $this->Upper_half_block = html_entity_decode('▀', ENT_NOQUOTES, 'UTF-8'); $this->Lower_one_eighth_block = html_entity_decode('▁', ENT_NOQUOTES, 'UTF-8'); $this->Lower_one_quarter_block = html_entity_decode('▂', ENT_NOQUOTES, 'UTF-8');