]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/CLIFormatter.pm
824c1a26a1833549e4789077739b8387944d52d7
1 package PVE
::CLIFormatter
;
13 sub query_terminal_options
{
19 ($options->{columns
}) = PVE
::PTY
::tcgetsize
(*STDOUT
);
22 $options->{encoding
} = I18N
::Langinfo
::langinfo
(I18N
::Langinfo
::CODESET
());
24 $options->{utf8
} = 1 if $options->{encoding
} eq 'UTF-8';
30 my ($data, $propdef) = @_;
32 return '' if !defined($data);
34 if (defined($propdef)) {
35 if (my $type = $propdef->{type
}) {
36 if ($type eq 'boolean') {
40 if (!defined($data) && defined($propdef->{default})) {
41 return "($propdef->{default})";
43 if (defined(my $renderer = $propdef->{renderer
})) {
44 my $code = PVE
::JSONSchema
::get_renderer
($renderer);
45 die "internal error: unknown renderer '$renderer'" if !$code;
46 return $code->($data);
50 if (my $class = ref($data)) {
51 return to_json
($data, { canonical
=> 1 });
57 # prints a formatted table with a title row.
58 # $data - the data to print (array of objects)
59 # $returnprops -json schema property description
60 # $props_to_print - ordered list of properties to print
62 # - sort_key: can be used to sort after a column, if it isn't set we sort
63 # after the leftmost column (with no undef value in $data) this can be
64 # turned off by passing 0 as sort_key
65 # - border: print with/without table header and asciiart border
66 # - columns: limit output width (if > 0)
67 # - utf8: use utf8 characters for table delimiters
69 sub print_text_table
{
70 my ($data, $returnprops, $props_to_print, $options) = @_;
72 my $sort_key = $options->{sort_key
};
73 my $border = $options->{border
};
74 my $columns = $options->{columns
};
75 my $utf8 = $options->{utf8
};
76 my $encoding = $options->{encoding
} // 'UTF-8';
79 if (defined($sort_key) && $sort_key eq 0) {
81 $sort_key = $props_to_print->[0];
84 if (defined($sort_key)) {
85 my $type = $returnprops->{$sort_key}->{type
} // 'string';
86 if ($type eq 'integer' || $type eq 'number') {
87 @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
89 @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
95 my $borderstring_m = '';
96 my $borderstring_b = '';
97 my $borderstring_t = '';
98 my $formatstring = '';
100 my $column_count = scalar(@$props_to_print);
104 foreach my $entry (@$data) {
109 for (my $i = 0; $i < $column_count; $i++) {
110 my $prop = $props_to_print->[$i];
111 my $propinfo = $returnprops->{$prop} // {};
113 my $text = data_to_text
($entry->{$prop}, $propinfo);
114 my $lines = [ split(/\n/, $text) ];
115 my $linecount = scalar(@$lines);
116 $height = $linecount if $linecount > $height;
119 foreach my $line (@$lines) {
120 my $len = length($line);
121 $width = $len if $len > $width;
124 $rowdata->{$prop} = {
136 for (my $i = 0; $i < $column_count; $i++) {
137 my $prop = $props_to_print->[$i];
138 my $propinfo = $returnprops->{$prop} // {};
140 my $title = $propinfo->{title
} // $prop;
141 my $cutoff = $propinfo->{print_width
} // $propinfo->{maxLength
};
143 # calculate maximal print width and cutoff
144 my $titlelen = length($title);
146 my $longest = $titlelen;
147 foreach my $coldata (@$tabledata) {
148 my $rowdata = $coldata->{rowdata
}->{$prop};
149 $longest = $rowdata->{width
} if $rowdata->{width
} > $longest;
151 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
153 $colopts->{$prop} = {
159 if ($i == 0 && ($column_count == 1)) {
161 $formatstring .= "│ %-${cutoff}s │";
162 $borderstring_t .= "┌─" . ('─' x
$cutoff) . "─┐";
163 $borderstring_m .= "├─" . ('─' x
$cutoff) . "─┤";
164 $borderstring_b .= "└─" . ('─' x
$cutoff) . "─┘";
166 $formatstring .= "| %-${cutoff}s |";
167 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+";
171 $formatstring .= "│ %-${cutoff}s ";
172 $borderstring_t .= "┌─" . ('─' x
$cutoff) . '─';
173 $borderstring_m .= "├─" . ('─' x
$cutoff) . '─';
174 $borderstring_b .= "└─" . ('─' x
$cutoff) . '─';
176 $formatstring .= "| %-${cutoff}s ";
177 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
179 } elsif ($i == ($column_count - 1)) {
181 $formatstring .= "│ %-${cutoff}s │";
182 $borderstring_t .= "┬─" . ('─' x
$cutoff) . "─┐";
183 $borderstring_m .= "┼─" . ('─' x
$cutoff) . "─┤";
184 $borderstring_b .= "┴─" . ('─' x
$cutoff) . "─┘";
186 $formatstring .= "| %-${cutoff}s |";
187 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+";
191 $formatstring .= "│ %-${cutoff}s ";
192 $borderstring_t .= "┬─" . ('─' x
$cutoff) . '─';
193 $borderstring_m .= "┼─" . ('─' x
$cutoff) . '─';
194 $borderstring_b .= "┴─" . ('─' x
$cutoff) . '─';
196 $formatstring .= "| %-${cutoff}s ";
197 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
201 # skip alignment and cutoff on last column
202 $formatstring .= ($i == ($column_count - 1)) ?
"%s" : "%-${cutoff}s ";
206 $borderstring_t = $borderstring_m if !length($borderstring_t);
207 $borderstring_b = $borderstring_m if !length($borderstring_b);
213 print encode
($encoding, substr($text, 0, $columns) . "\n");
215 print encode
($encoding, $text) . "\n";
219 $writeln->($borderstring_t) if $border;
220 my $text = sprintf $formatstring, map { $colopts->{$_}->{title
} } @$props_to_print;
223 foreach my $coldata (@$tabledata) {
224 $writeln->($borderstring_m) if $border;
226 for (my $i = 0; $i < $coldata->{height
}; $i++) {
228 $text = sprintf $formatstring, map {
229 substr($coldata->{rowdata
}->{$_}->{lines
}->[$i] // '', 0, $colopts->{$_}->{cutoff
});
236 $writeln->($borderstring_b) if $border;
239 # prints the result of an API GET call returning an array as a table.
240 # takes formatting information from the results property of the call
241 # if $props_to_print is provided, prints only those columns. otherwise
242 # takes all fields of the results property, with a fallback
243 # to all fields occuring in items of $data.
245 my ($data, $result_schema, $props_to_print, $options) = @_;
247 die "can only print object lists\n"
248 if !($result_schema->{type
} eq 'array' && $result_schema->{items
}->{type
} eq 'object');
250 my $returnprops = $result_schema->{items
}->{properties
};
252 if (!defined($props_to_print)) {
253 $props_to_print = [ sort keys %$returnprops ];
254 if (!scalar(@$props_to_print)) {
256 foreach my $obj (@{$data}) {
257 foreach my $key (keys %{$obj}) {
258 $all_props->{ $key } = 1;
261 $props_to_print = [ sort keys %{$all_props} ];
263 die "unable to detect list properties\n" if !scalar(@$props_to_print);
266 print_text_table
($data, $returnprops, $props_to_print, $options);
269 sub print_api_result
{
270 my ($format, $data, $result_schema, $props_to_print, $options) = @_;
272 if (!defined($options)) {
273 $options = query_terminal_options
({});
275 $options = { %$options }; # copy
278 return if $result_schema->{type
} eq 'null';
280 if ($format eq 'json') {
281 # Note: we always use utf8 encoding for json format
282 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
283 } elsif ($format eq 'text' || $format eq 'plain') {
284 my $encoding = $options->{encoding
} // 'UTF-8';
285 my $type = $result_schema->{type
};
286 if ($type eq 'object') {
287 $props_to_print = [ sort keys %$data ] if !defined($props_to_print);
289 foreach my $key (@$props_to_print) {
290 push @$kvstore, { key
=> $key, value
=> data_to_text
($data->{$key}, $result_schema->{properties
}->{$key}) };
292 my $schema = { type
=> 'array', items
=> { type
=> 'object' }};
293 $options->{border
} = $format eq 'text';
294 print_api_list
($kvstore, $schema, ['key', 'value'], $options);
295 } elsif ($type eq 'array') {
296 return if !scalar(@$data);
297 my $item_type = $result_schema->{items
}->{type
};
298 if ($item_type eq 'object') {
299 $options->{border
} = $format eq 'text';
300 print_api_list
($data, $result_schema, $props_to_print, $options);
302 foreach my $entry (@$data) {
303 print encode
($encoding, data_to_text
($entry) . "\n");
307 print encode
($encoding, "$data\n");
310 die "internal error: unknown output format"; # should not happen