]>
git.proxmox.com Git - pve-client.git/blob - PVE/APIClient/CLIFormatter.pm
add7a3a615292d1c12cccd48108a30e43d4a24db
1 package PVE
::APIClient
::CLIFormatter
;
7 use PVE
::APIClient
::JSONSchema
;
8 use PVE
::APIClient
::PTY
;
13 sub query_terminal_options
{
19 ($options->{columns
}) = PVE
::APIClient
::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
::APIClient
::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';
78 if (!defined($sort_key) || $sort_key eq 0) {
79 $sort_key = $props_to_print->[0];
82 if (defined($sort_key)) {
83 my $type = $returnprops->{$sort_key}->{type
} // 'string';
84 if ($type eq 'integer' || $type eq 'number') {
85 @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
87 @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
93 my $borderstring_m = '';
94 my $borderstring_b = '';
95 my $borderstring_t = '';
96 my $formatstring = '';
98 my $column_count = scalar(@$props_to_print);
102 foreach my $entry (@$data) {
107 for (my $i = 0; $i < $column_count; $i++) {
108 my $prop = $props_to_print->[$i];
109 my $propinfo = $returnprops->{$prop} // {};
111 my $text = data_to_text
($entry->{$prop}, $propinfo);
112 my $lines = [ split(/\n/, $text) ];
113 my $linecount = scalar(@$lines);
114 $height = $linecount if $linecount > $height;
117 foreach my $line (@$lines) {
118 my $len = length($line);
119 $width = $len if $len > $width;
122 $rowdata->{$prop} = {
134 for (my $i = 0; $i < $column_count; $i++) {
135 my $prop = $props_to_print->[$i];
136 my $propinfo = $returnprops->{$prop} // {};
138 my $title = $propinfo->{title
} // $prop;
139 my $cutoff = $propinfo->{print_width
} // $propinfo->{maxLength
};
141 # calculate maximal print width and cutoff
142 my $titlelen = length($title);
144 my $longest = $titlelen;
145 foreach my $coldata (@$tabledata) {
146 my $rowdata = $coldata->{rowdata
}->{$prop};
147 $longest = $rowdata->{width
} if $rowdata->{width
} > $longest;
149 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
151 $colopts->{$prop} = {
157 if ($i == 0 && ($column_count == 1)) {
159 $formatstring .= "│ %-${cutoff}s │";
160 $borderstring_t .= "┌─" . ('─' x
$cutoff) . "─┐";
161 $borderstring_m .= "├─" . ('─' x
$cutoff) . "─┤";
162 $borderstring_b .= "└─" . ('─' x
$cutoff) . "─┘";
164 $formatstring .= "| %-${cutoff}s |";
165 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+";
169 $formatstring .= "│ %-${cutoff}s ";
170 $borderstring_t .= "┌─" . ('─' x
$cutoff) . '─';
171 $borderstring_m .= "├─" . ('─' x
$cutoff) . '─';
172 $borderstring_b .= "└─" . ('─' x
$cutoff) . '─';
174 $formatstring .= "| %-${cutoff}s ";
175 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
177 } elsif ($i == ($column_count - 1)) {
179 $formatstring .= "│ %-${cutoff}s │";
180 $borderstring_t .= "┬─" . ('─' x
$cutoff) . "─┐";
181 $borderstring_m .= "┼─" . ('─' x
$cutoff) . "─┤";
182 $borderstring_b .= "┴─" . ('─' x
$cutoff) . "─┘";
184 $formatstring .= "| %-${cutoff}s |";
185 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+";
189 $formatstring .= "│ %-${cutoff}s ";
190 $borderstring_t .= "┬─" . ('─' x
$cutoff) . '─';
191 $borderstring_m .= "┼─" . ('─' x
$cutoff) . '─';
192 $borderstring_b .= "┴─" . ('─' x
$cutoff) . '─';
194 $formatstring .= "| %-${cutoff}s ";
195 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
199 # skip alignment and cutoff on last column
200 $formatstring .= ($i == ($column_count - 1)) ?
"%s" : "%-${cutoff}s ";
204 $borderstring_t = $borderstring_m if !length($borderstring_t);
205 $borderstring_b = $borderstring_m if !length($borderstring_b);
211 print encode
($encoding, substr($text, 0, $columns) . "\n");
213 print encode
($encoding, $text) . "\n";
217 $writeln->($borderstring_t) if $border;
218 my $text = sprintf $formatstring, map { $colopts->{$_}->{title
} } @$props_to_print;
221 foreach my $coldata (@$tabledata) {
222 $writeln->($borderstring_m) if $border;
224 for (my $i = 0; $i < $coldata->{height
}; $i++) {
226 $text = sprintf $formatstring, map {
227 substr($coldata->{rowdata
}->{$_}->{lines
}->[$i] // '', 0, $colopts->{$_}->{cutoff
});
234 $writeln->($borderstring_b) if $border;
237 # prints the result of an API GET call returning an array as a table.
238 # takes formatting information from the results property of the call
239 # if $props_to_print is provided, prints only those columns. otherwise
240 # takes all fields of the results property, with a fallback
241 # to all fields occuring in items of $data.
243 my ($data, $result_schema, $props_to_print, $options) = @_;
245 die "can only print object lists\n"
246 if !($result_schema->{type
} eq 'array' && $result_schema->{items
}->{type
} eq 'object');
248 my $returnprops = $result_schema->{items
}->{properties
};
250 if (!defined($props_to_print)) {
251 $props_to_print = [ sort keys %$returnprops ];
252 if (!scalar(@$props_to_print)) {
254 foreach my $obj (@{$data}) {
255 foreach my $key (keys %{$obj}) {
256 $all_props->{ $key } = 1;
259 $props_to_print = [ sort keys %{$all_props} ];
261 die "unable to detect list properties\n" if !scalar(@$props_to_print);
264 print_text_table
($data, $returnprops, $props_to_print, $options);
267 sub print_api_result
{
268 my ($format, $data, $result_schema, $props_to_print, $options) = @_;
270 if (!defined($options)) {
271 $options = query_terminal_options
({});
273 $options = { %$options }; # copy
276 return if $result_schema->{type
} eq 'null';
278 if ($format eq 'json') {
279 # Note: we always use utf8 encoding for json format
280 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
281 } elsif ($format eq 'text' || $format eq 'plain') {
282 my $encoding = $options->{encoding
} // 'UTF-8';
283 my $type = $result_schema->{type
};
284 if ($type eq 'object') {
285 $props_to_print = [ sort keys %$data ] if !defined($props_to_print);
287 foreach my $key (@$props_to_print) {
288 push @$kvstore, { key
=> $key, value
=> data_to_text
($data->{$key}, $result_schema->{properties
}->{$key}) };
290 my $schema = { type
=> 'array', items
=> { type
=> 'object' }};
291 $options->{border
} = $format eq 'text';
292 print_api_list
($kvstore, $schema, ['key', 'value'], $options);
293 } elsif ($type eq 'array') {
294 return if !scalar(@$data);
295 my $item_type = $result_schema->{items
}->{type
};
296 if ($item_type eq 'object') {
297 $options->{border
} = $format eq 'text';
298 print_api_list
($data, $result_schema, $props_to_print, $options);
300 foreach my $entry (@$data) {
301 print encode
($encoding, data_to_text
($entry) . "\n");
305 print encode
($encoding, "$data\n");
308 die "internal error: unknown output format"; # should not happen