]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/CLIFormatter.pm
1 package PVE
::CLIFormatter
;
11 my ($text, $max) = @_;
14 my @lines = split(/\n/, $text);
15 foreach my $line (@lines) {
16 print encode
('UTF-8', substr($line, 0, $max) . "\n");
19 print encode
('UTF-8', $text);
24 my ($data, $propdef) = @_;
26 if (defined($propdef)) {
27 if (my $type = $propdef->{type
}) {
28 if ($type eq 'boolean') {
32 if (!defined($data) && defined($propdef->{default})) {
33 return "($propdef->{default})";
35 if (defined(my $renderer = $propdef->{renderer
})) {
36 my $code = PVE
::JSONSchema
::get_renderer
($renderer);
37 die "internal error: unknown renderer '$renderer'" if !$code;
38 return $code->($data);
41 return '' if !defined($data);
43 if (my $class = ref($data)) {
44 return to_json
($data, { canonical
=> 1 });
50 # prints a formatted table with a title row.
51 # $data - the data to print (array of objects)
52 # $returnprops -json schema property description
53 # $props_to_print - ordered list of properties to print
55 # - sort_key: can be used to sort after a column, if it isn't set we sort
56 # after the leftmost column (with no undef value in $data) this can be
57 # turned off by passing 0 as sort_key
58 # - border: print with/without table header and asciiart border
59 # - columns: limit output width (if > 0)
60 # - utf8: use utf8 characters for table delimiters
62 sub print_text_table
{
63 my ($data, $returnprops, $props_to_print, $options) = @_;
65 my $sort_key = $options->{sort_key
};
66 my $border = $options->{border
};
67 my $columns = $options->{columns
};
68 my $utf8 = $options->{utf8
};
71 if (defined($sort_key) && $sort_key eq 0) {
78 my $borderstring_m = '';
79 my $borderstring_b = '';
80 my $borderstring_t = '';
81 my $formatstring = '';
83 my $column_count = scalar(@$props_to_print);
85 for (my $i = 0; $i < $column_count; $i++) {
86 my $prop = $props_to_print->[$i];
87 my $propinfo = $returnprops->{$prop} // {};
89 my $title = $propinfo->{title
} // $prop;
90 my $cutoff = $propinfo->{print_width
} // $propinfo->{maxLength
};
92 # calculate maximal print width and cutoff
93 my $titlelen = length($title);
95 my $longest = $titlelen;
96 my $sortable = $autosort;
97 foreach my $entry (@$data) {
98 my $len = length(data_to_text
($entry->{$prop}, $propinfo)) // 0;
99 $longest = $len if $len > $longest;
100 $sortable = 0 if !defined($entry->{$prop});
102 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
103 $sort_key //= $prop if $sortable;
105 $colopts->{$prop} = {
107 default => $propinfo->{default} // '',
112 if ($i == 0 && ($column_count == 1)) {
114 $formatstring .= "│ %-${cutoff}s │\n";
115 $borderstring_t .= "┌─" . ('─' x
$cutoff) . "─┐\n";
116 $borderstring_m .= "├─" . ('─' x
$cutoff) . "─┤\n";
117 $borderstring_b .= "└─" . ('─' x
$cutoff) . "─┘\n";
119 $formatstring .= "| %-${cutoff}s |\n";
120 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+\n";
124 $formatstring .= "│ %-${cutoff}s ";
125 $borderstring_t .= "┌─" . ('─' x
$cutoff) . '─';
126 $borderstring_m .= "├─" . ('─' x
$cutoff) . '─';
127 $borderstring_b .= "└─" . ('─' x
$cutoff) . '─';
129 $formatstring .= "| %-${cutoff}s ";
130 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
132 } elsif ($i == ($column_count - 1)) {
134 $formatstring .= "│ %-${cutoff}s │\n";
135 $borderstring_t .= "┬─" . ('─' x
$cutoff) . "─┐\n";
136 $borderstring_m .= "┼─" . ('─' x
$cutoff) . "─┤\n";
137 $borderstring_b .= "┴─" . ('─' x
$cutoff) . "─┘\n";
139 $formatstring .= "| %-${cutoff}s |\n";
140 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+\n";
144 $formatstring .= "│ %-${cutoff}s ";
145 $borderstring_t .= "┬─" . ('─' x
$cutoff) . '─';
146 $borderstring_m .= "┼─" . ('─' x
$cutoff) . '─';
147 $borderstring_b .= "┴─" . ('─' x
$cutoff) . '─';
149 $formatstring .= "| %-${cutoff}s ";
150 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
154 # skip alignment and cutoff on last column
155 $formatstring .= ($i == ($column_count - 1)) ?
"%s\n" : "%-${cutoff}s ";
159 if (defined($sort_key)) {
160 my $type = $returnprops->{$sort_key}->{type
} // 'string';
161 if ($type eq 'integer' || $type eq 'number') {
162 @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
164 @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
168 $borderstring_t = $borderstring_m if !length($borderstring_t);
169 $borderstring_b = $borderstring_m if !length($borderstring_b);
171 println_max
($borderstring_t, $columns) if $border;
172 my $text = sprintf $formatstring, map { $colopts->{$_}->{title
} } @$props_to_print;
173 println_max
($text, $columns);
175 foreach my $entry (@$data) {
176 println_max
($borderstring_m, $columns) if $border;
177 $text = sprintf $formatstring, map {
178 substr(data_to_text
($entry->{$_}, $returnprops->{$_}) // $colopts->{$_}->{default},
179 0, $colopts->{$_}->{cutoff
});
181 println_max
($text, $columns);
183 println_max
($borderstring_b, $columns) if $border;
186 # prints the result of an API GET call returning an array as a table.
187 # takes formatting information from the results property of the call
188 # if $props_to_print is provided, prints only those columns. otherwise
189 # takes all fields of the results property, with a fallback
190 # to all fields occuring in items of $data.
192 my ($data, $result_schema, $props_to_print, $options) = @_;
194 die "can only print object lists\n"
195 if !($result_schema->{type
} eq 'array' && $result_schema->{items
}->{type
} eq 'object');
197 my $returnprops = $result_schema->{items
}->{properties
};
199 if (!defined($props_to_print)) {
200 $props_to_print = [ sort keys %$returnprops ];
201 if (!scalar(@$props_to_print)) {
203 foreach my $obj (@{$data}) {
204 foreach my $key (keys %{$obj}) {
205 $all_props->{ $key } = 1;
208 $props_to_print = [ sort keys %{$all_props} ];
210 die "unable to detect list properties\n" if !scalar(@$props_to_print);
213 print_text_table
($data, $returnprops, $props_to_print, $options);
216 sub print_api_result
{
217 my ($format, $data, $result_schema, $props_to_print, $options) = @_;
220 $options = { %$options }; # copy
222 return if $result_schema->{type
} eq 'null';
224 if ($format eq 'json') {
225 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
226 } elsif ($format eq 'text' || $format eq 'plain') {
227 my $type = $result_schema->{type
};
228 if ($type eq 'object') {
229 $props_to_print = [ sort keys %$data ] if !defined($props_to_print);
231 foreach my $key (@$props_to_print) {
232 push @$kvstore, { key
=> $key, value
=> data_to_text
($data->{$key}, $result_schema->{properties
}->{$key}) };
234 my $schema = { type
=> 'array', items
=> { type
=> 'object' }};
235 $options->{border
} = $format eq 'text';
236 print_api_list
($kvstore, $schema, ['key', 'value'], $options);
237 } elsif ($type eq 'array') {
238 return if !scalar(@$data);
239 my $item_type = $result_schema->{items
}->{type
};
240 if ($item_type eq 'object') {
241 $options->{border
} = $format eq 'text';
242 print_api_list
($data, $result_schema, $props_to_print, $options);
244 foreach my $entry (@$data) {
245 print data_to_text
($entry) . "\n";
252 die "internal error: unknown output format"; # should not happen