]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/CLIFormatter.pm
cbe63907241c378dff6e78d4bf201455ccb94829
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 if (defined($propdef)) {
46 if (my $type = $propdef->{type
}) {
47 if ($type eq 'boolean') {
51 if (!defined($data) && defined($propdef->{default})) {
52 return "($propdef->{default})";
54 if (defined(my $renderer = $propdef->{renderer
})) {
55 my $code = PVE
::JSONSchema
::get_renderer
($renderer);
56 die "internal error: unknown renderer '$renderer'" if !$code;
57 return $code->($data);
60 return '' if !defined($data);
62 if (my $class = ref($data)) {
63 return to_json
($data, { canonical
=> 1 });
69 # prints a formatted table with a title row.
70 # $data - the data to print (array of objects)
71 # $returnprops -json schema property description
72 # $props_to_print - ordered list of properties to print
74 # - sort_key: can be used to sort after a column, if it isn't set we sort
75 # after the leftmost column (with no undef value in $data) this can be
76 # turned off by passing 0 as sort_key
77 # - border: print with/without table header and asciiart border
78 # - columns: limit output width (if > 0)
79 # - utf8: use utf8 characters for table delimiters
81 sub print_text_table
{
82 my ($data, $returnprops, $props_to_print, $options) = @_;
84 my $sort_key = $options->{sort_key
};
85 my $border = $options->{border
};
86 my $columns = $options->{columns
};
87 my $utf8 = $options->{utf8
};
88 my $encoding = $options->{encoding
} // 'UTF-8';
91 if (defined($sort_key) && $sort_key eq 0) {
98 my $borderstring_m = '';
99 my $borderstring_b = '';
100 my $borderstring_t = '';
101 my $formatstring = '';
103 my $column_count = scalar(@$props_to_print);
105 for (my $i = 0; $i < $column_count; $i++) {
106 my $prop = $props_to_print->[$i];
107 my $propinfo = $returnprops->{$prop} // {};
109 my $title = $propinfo->{title
} // $prop;
110 my $cutoff = $propinfo->{print_width
} // $propinfo->{maxLength
};
112 # calculate maximal print width and cutoff
113 my $titlelen = length($title);
115 my $longest = $titlelen;
116 my $sortable = $autosort;
117 foreach my $entry (@$data) {
118 my $len = length(data_to_text
($entry->{$prop}, $propinfo)) // 0;
119 $longest = $len if $len > $longest;
120 $sortable = 0 if !defined($entry->{$prop});
122 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
123 $sort_key //= $prop if $sortable;
125 $colopts->{$prop} = {
127 default => $propinfo->{default} // '',
132 if ($i == 0 && ($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) . '-';
152 } elsif ($i == ($column_count - 1)) {
154 $formatstring .= "│ %-${cutoff}s │\n";
155 $borderstring_t .= "┬─" . ('─' x
$cutoff) . "─┐\n";
156 $borderstring_m .= "┼─" . ('─' x
$cutoff) . "─┤\n";
157 $borderstring_b .= "┴─" . ('─' x
$cutoff) . "─┘\n";
159 $formatstring .= "| %-${cutoff}s |\n";
160 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+\n";
164 $formatstring .= "│ %-${cutoff}s ";
165 $borderstring_t .= "┬─" . ('─' x
$cutoff) . '─';
166 $borderstring_m .= "┼─" . ('─' x
$cutoff) . '─';
167 $borderstring_b .= "┴─" . ('─' x
$cutoff) . '─';
169 $formatstring .= "| %-${cutoff}s ";
170 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
174 # skip alignment and cutoff on last column
175 $formatstring .= ($i == ($column_count - 1)) ?
"%s\n" : "%-${cutoff}s ";
179 if (defined($sort_key)) {
180 my $type = $returnprops->{$sort_key}->{type
} // 'string';
181 if ($type eq 'integer' || $type eq 'number') {
182 @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
184 @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
188 $borderstring_t = $borderstring_m if !length($borderstring_t);
189 $borderstring_b = $borderstring_m if !length($borderstring_b);
191 println_max
($borderstring_t, $encoding, $columns) if $border;
192 my $text = sprintf $formatstring, map { $colopts->{$_}->{title
} } @$props_to_print;
193 println_max
($text, $encoding, $columns);
195 foreach my $entry (@$data) {
196 println_max
($borderstring_m, $encoding, $columns) if $border;
197 $text = sprintf $formatstring, map {
198 substr(data_to_text
($entry->{$_}, $returnprops->{$_}) // $colopts->{$_}->{default},
199 0, $colopts->{$_}->{cutoff
});
201 println_max
($text, $encoding, $columns);
203 println_max
($borderstring_b, $encoding, $columns) if $border;
206 # prints the result of an API GET call returning an array as a table.
207 # takes formatting information from the results property of the call
208 # if $props_to_print is provided, prints only those columns. otherwise
209 # takes all fields of the results property, with a fallback
210 # to all fields occuring in items of $data.
212 my ($data, $result_schema, $props_to_print, $options) = @_;
214 die "can only print object lists\n"
215 if !($result_schema->{type
} eq 'array' && $result_schema->{items
}->{type
} eq 'object');
217 my $returnprops = $result_schema->{items
}->{properties
};
219 if (!defined($props_to_print)) {
220 $props_to_print = [ sort keys %$returnprops ];
221 if (!scalar(@$props_to_print)) {
223 foreach my $obj (@{$data}) {
224 foreach my $key (keys %{$obj}) {
225 $all_props->{ $key } = 1;
228 $props_to_print = [ sort keys %{$all_props} ];
230 die "unable to detect list properties\n" if !scalar(@$props_to_print);
233 print_text_table
($data, $returnprops, $props_to_print, $options);
236 sub print_api_result
{
237 my ($format, $data, $result_schema, $props_to_print, $options) = @_;
239 if (!defined($options)) {
240 $options = query_terminal_options
({});
242 $options = { %$options }; # copy
245 return if $result_schema->{type
} eq 'null';
247 if ($format eq 'json') {
248 # Note: we always use utf8 encoding for json format
249 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
250 } elsif ($format eq 'text' || $format eq 'plain') {
251 my $encoding = $options->{encoding
} // 'UTF-8';
252 my $type = $result_schema->{type
};
253 if ($type eq 'object') {
254 $props_to_print = [ sort keys %$data ] if !defined($props_to_print);
256 foreach my $key (@$props_to_print) {
257 push @$kvstore, { key
=> $key, value
=> data_to_text
($data->{$key}, $result_schema->{properties
}->{$key}) };
259 my $schema = { type
=> 'array', items
=> { type
=> 'object' }};
260 $options->{border
} = $format eq 'text';
261 print_api_list
($kvstore, $schema, ['key', 'value'], $options);
262 } elsif ($type eq 'array') {
263 return if !scalar(@$data);
264 my $item_type = $result_schema->{items
}->{type
};
265 if ($item_type eq 'object') {
266 $options->{border
} = $format eq 'text';
267 print_api_list
($data, $result_schema, $props_to_print, $options);
269 foreach my $entry (@$data) {
270 print encode
($encoding, data_to_text
($entry) . "\n");
274 print encode
($encoding, "$data\n");
277 die "internal error: unknown output format"; # should not happen