]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/CLIFormatter.pm
a6c47952fecf16b8d852c2ac1a6b579c836ec262
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 ($text, $encoding, $max) = @_;
33 my @lines = split(/\n/, $text);
34 foreach my $line (@lines) {
35 print encode
($encoding, substr($line, 0, $max) . "\n");
38 print encode
($encoding, $text);
43 my ($data, $propdef) = @_;
45 return '' if !defined($data);
47 if (defined($propdef)) {
48 if (my $type = $propdef->{type
}) {
49 if ($type eq 'boolean') {
53 if (!defined($data) && defined($propdef->{default})) {
54 return "($propdef->{default})";
56 if (defined(my $renderer = $propdef->{renderer
})) {
57 my $code = PVE
::JSONSchema
::get_renderer
($renderer);
58 die "internal error: unknown renderer '$renderer'" if !$code;
59 return $code->($data);
63 if (my $class = ref($data)) {
64 return to_json
($data, { canonical
=> 1 });
70 # prints a formatted table with a title row.
71 # $data - the data to print (array of objects)
72 # $returnprops -json schema property description
73 # $props_to_print - ordered list of properties to print
75 # - sort_key: can be used to sort after a column, if it isn't set we sort
76 # after the leftmost column (with no undef value in $data) this can be
77 # turned off by passing 0 as sort_key
78 # - border: print with/without table header and asciiart border
79 # - columns: limit output width (if > 0)
80 # - utf8: use utf8 characters for table delimiters
82 sub print_text_table
{
83 my ($data, $returnprops, $props_to_print, $options) = @_;
85 my $sort_key = $options->{sort_key
};
86 my $border = $options->{border
};
87 my $columns = $options->{columns
};
88 my $utf8 = $options->{utf8
};
89 my $encoding = $options->{encoding
} // 'UTF-8';
92 if (defined($sort_key) && $sort_key eq 0) {
99 my $borderstring_m = '';
100 my $borderstring_b = '';
101 my $borderstring_t = '';
102 my $formatstring = '';
104 my $column_count = scalar(@$props_to_print);
106 for (my $i = 0; $i < $column_count; $i++) {
107 my $prop = $props_to_print->[$i];
108 my $propinfo = $returnprops->{$prop} // {};
110 my $title = $propinfo->{title
} // $prop;
111 my $cutoff = $propinfo->{print_width
} // $propinfo->{maxLength
};
113 # calculate maximal print width and cutoff
114 my $titlelen = length($title);
116 my $longest = $titlelen;
117 my $sortable = $autosort;
118 foreach my $entry (@$data) {
119 my $len = length(data_to_text
($entry->{$prop}, $propinfo)) // 0;
120 $longest = $len if $len > $longest;
121 $sortable = 0 if !defined($entry->{$prop});
123 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
124 $sort_key //= $prop if $sortable;
126 $colopts->{$prop} = {
128 default => $propinfo->{default} // '',
133 if ($i == 0 && ($column_count == 1)) {
135 $formatstring .= "│ %-${cutoff}s │\n";
136 $borderstring_t .= "┌─" . ('─' x
$cutoff) . "─┐\n";
137 $borderstring_m .= "├─" . ('─' x
$cutoff) . "─┤\n";
138 $borderstring_b .= "└─" . ('─' x
$cutoff) . "─┘\n";
140 $formatstring .= "| %-${cutoff}s |\n";
141 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+\n";
145 $formatstring .= "│ %-${cutoff}s ";
146 $borderstring_t .= "┌─" . ('─' x
$cutoff) . '─';
147 $borderstring_m .= "├─" . ('─' x
$cutoff) . '─';
148 $borderstring_b .= "└─" . ('─' x
$cutoff) . '─';
150 $formatstring .= "| %-${cutoff}s ";
151 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
153 } elsif ($i == ($column_count - 1)) {
155 $formatstring .= "│ %-${cutoff}s │\n";
156 $borderstring_t .= "┬─" . ('─' x
$cutoff) . "─┐\n";
157 $borderstring_m .= "┼─" . ('─' x
$cutoff) . "─┤\n";
158 $borderstring_b .= "┴─" . ('─' x
$cutoff) . "─┘\n";
160 $formatstring .= "| %-${cutoff}s |\n";
161 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+\n";
165 $formatstring .= "│ %-${cutoff}s ";
166 $borderstring_t .= "┬─" . ('─' x
$cutoff) . '─';
167 $borderstring_m .= "┼─" . ('─' x
$cutoff) . '─';
168 $borderstring_b .= "┴─" . ('─' x
$cutoff) . '─';
170 $formatstring .= "| %-${cutoff}s ";
171 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
175 # skip alignment and cutoff on last column
176 $formatstring .= ($i == ($column_count - 1)) ?
"%s\n" : "%-${cutoff}s ";
180 if (defined($sort_key)) {
181 my $type = $returnprops->{$sort_key}->{type
} // 'string';
182 if ($type eq 'integer' || $type eq 'number') {
183 @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
185 @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
189 $borderstring_t = $borderstring_m if !length($borderstring_t);
190 $borderstring_b = $borderstring_m if !length($borderstring_b);
192 println_max
($borderstring_t, $encoding, $columns) if $border;
193 my $text = sprintf $formatstring, map { $colopts->{$_}->{title
} } @$props_to_print;
194 println_max
($text, $encoding, $columns);
196 foreach my $entry (@$data) {
197 println_max
($borderstring_m, $encoding, $columns) if $border;
198 $text = sprintf $formatstring, map {
199 substr(data_to_text
($entry->{$_}, $returnprops->{$_}) // $colopts->{$_}->{default},
200 0, $colopts->{$_}->{cutoff
});
202 println_max
($text, $encoding, $columns);
204 println_max
($borderstring_b, $encoding, $columns) if $border;
207 # prints the result of an API GET call returning an array as a table.
208 # takes formatting information from the results property of the call
209 # if $props_to_print is provided, prints only those columns. otherwise
210 # takes all fields of the results property, with a fallback
211 # to all fields occuring in items of $data.
213 my ($data, $result_schema, $props_to_print, $options) = @_;
215 die "can only print object lists\n"
216 if !($result_schema->{type
} eq 'array' && $result_schema->{items
}->{type
} eq 'object');
218 my $returnprops = $result_schema->{items
}->{properties
};
220 if (!defined($props_to_print)) {
221 $props_to_print = [ sort keys %$returnprops ];
222 if (!scalar(@$props_to_print)) {
224 foreach my $obj (@{$data}) {
225 foreach my $key (keys %{$obj}) {
226 $all_props->{ $key } = 1;
229 $props_to_print = [ sort keys %{$all_props} ];
231 die "unable to detect list properties\n" if !scalar(@$props_to_print);
234 print_text_table
($data, $returnprops, $props_to_print, $options);
237 sub print_api_result
{
238 my ($format, $data, $result_schema, $props_to_print, $options) = @_;
240 if (!defined($options)) {
241 $options = query_terminal_options
({});
243 $options = { %$options }; # copy
246 return if $result_schema->{type
} eq 'null';
248 if ($format eq 'json') {
249 # Note: we always use utf8 encoding for json format
250 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
251 } elsif ($format eq 'text' || $format eq 'plain') {
252 my $encoding = $options->{encoding
} // 'UTF-8';
253 my $type = $result_schema->{type
};
254 if ($type eq 'object') {
255 $props_to_print = [ sort keys %$data ] if !defined($props_to_print);
257 foreach my $key (@$props_to_print) {
258 push @$kvstore, { key
=> $key, value
=> data_to_text
($data->{$key}, $result_schema->{properties
}->{$key}) };
260 my $schema = { type
=> 'array', items
=> { type
=> 'object' }};
261 $options->{border
} = $format eq 'text';
262 print_api_list
($kvstore, $schema, ['key', 'value'], $options);
263 } elsif ($type eq 'array') {
264 return if !scalar(@$data);
265 my $item_type = $result_schema->{items
}->{type
};
266 if ($item_type eq 'object') {
267 $options->{border
} = $format eq 'text';
268 print_api_list
($data, $result_schema, $props_to_print, $options);
270 foreach my $entry (@$data) {
271 print encode
($encoding, data_to_text
($entry) . "\n");
275 print encode
($encoding, "$data\n");
278 die "internal error: unknown output format"; # should not happen