]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/CLIFormatter.pm
1 package PVE
::CLIFormatter
;
12 my @lines = split(/\n/, $text);
13 foreach my $line (@lines) {
14 print substr($line, 0, $max) . "\n";
22 my ($data, $propdef) = @_;
24 if (defined($propdef)) {
25 if (my $type = $propdef->{type
}) {
26 if ($type eq 'boolean') {
30 if (!defined($data) && defined($propdef->{default})) {
31 return "($propdef->{default})";
33 if (defined(my $renderer = $propdef->{renderer
})) {
34 my $code = PVE
::JSONSchema
::get_renderer
($renderer);
35 die "internal error: unknown renderer '$renderer'" if !$code;
36 return $code->($data);
39 return '' if !defined($data);
41 if (my $class = ref($data)) {
42 return to_json
($data, { canonical
=> 1 });
48 # prints a formatted table with a title row.
49 # $data - the data to print (array of objects)
50 # $returnprops -json schema property description
51 # $props_to_print - ordered list of properties to print
52 # $sort_key can be used to sort after a column, if it isn't set we sort
53 # after the leftmost column (with no undef value in $data) this can be
54 # turned off by passing 0 as $sort_key
55 # $border - print with/without table header and asciiart border
56 sub print_text_table
{
57 my ($data, $returnprops, $props_to_print, $sort_key, $border, $columns) = @_;
60 if (defined($sort_key) && $sort_key eq 0) {
67 my $borderstring = '';
68 my $formatstring = '';
70 my $column_count = scalar(@$props_to_print);
72 for (my $i = 0; $i < $column_count; $i++) {
73 my $prop = $props_to_print->[$i];
74 my $propinfo = $returnprops->{$prop} // {};
76 my $title = $propinfo->{title
} // $prop;
77 my $cutoff = $propinfo->{print_width
} // $propinfo->{maxLength
};
79 # calculate maximal print width and cutoff
80 my $titlelen = length($title);
82 my $longest = $titlelen;
83 my $sortable = $autosort;
84 foreach my $entry (@$data) {
85 my $len = length(data_to_text
($entry->{$prop}, $propinfo)) // 0;
86 $longest = $len if $len > $longest;
87 $sortable = 0 if !defined($entry->{$prop});
89 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
90 $sort_key //= $prop if $sortable;
94 default => $propinfo->{default} // '',
99 if ($i == ($column_count - 1)) {
100 $formatstring .= "| %-${cutoff}s |\n";
101 $borderstring .= "+-" . ('-' x
$cutoff) . "-+\n";
103 $formatstring .= "| %-${cutoff}s ";
104 $borderstring .= "+-" . ('-' x
$cutoff) . '-';
107 # skip alignment and cutoff on last column
108 $formatstring .= ($i == ($column_count - 1)) ?
"%s\n" : "%-${cutoff}s ";
112 if (defined($sort_key)) {
113 my $type = $returnprops->{$sort_key}->{type
} // 'string';
114 if ($type eq 'integer' || $type eq 'number') {
115 @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
117 @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
121 println_max
($borderstring, $columns) if $border;
122 my $text = sprintf $formatstring, map { $colopts->{$_}->{title
} } @$props_to_print;
123 println_max
($text, $columns);
125 foreach my $entry (@$data) {
126 println_max
($borderstring, $columns) if $border;
127 $text = sprintf $formatstring, map {
128 substr(data_to_text
($entry->{$_}, $returnprops->{$_}) // $colopts->{$_}->{default},
129 0, $colopts->{$_}->{cutoff
});
131 println_max
($text, $columns);
133 println_max
($borderstring, $columns) if $border;
136 # prints the result of an API GET call returning an array as a table.
137 # takes formatting information from the results property of the call
138 # if $props_to_print is provided, prints only those columns. otherwise
139 # takes all fields of the results property, with a fallback
140 # to all fields occuring in items of $data.
142 my ($data, $result_schema, $props_to_print, $sort_key, $border, $columns) = @_;
144 die "can only print object lists\n"
145 if !($result_schema->{type
} eq 'array' && $result_schema->{items
}->{type
} eq 'object');
147 my $returnprops = $result_schema->{items
}->{properties
};
149 if (!defined($props_to_print)) {
150 $props_to_print = [ sort keys %$returnprops ];
151 if (!scalar(@$props_to_print)) {
153 foreach my $obj (@{$data}) {
154 foreach my $key (keys %{$obj}) {
155 $all_props->{ $key } = 1;
158 $props_to_print = [ sort keys %{$all_props} ];
160 die "unable to detect list properties\n" if !scalar(@$props_to_print);
163 print_text_table
($data, $returnprops, $props_to_print, $sort_key, $border, $columns);
166 sub print_api_result
{
167 my ($format, $data, $result_schema, $props_to_print, $sort_key, $columns) = @_;
169 return if $result_schema->{type
} eq 'null';
171 if ($format eq 'json') {
172 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
173 } elsif ($format eq 'text' || $format eq 'plain') {
174 my $type = $result_schema->{type
};
175 if ($type eq 'object') {
176 $props_to_print = [ sort keys %$data ] if !defined($props_to_print);
178 foreach my $key (@$props_to_print) {
179 push @$kvstore, { key
=> $key, value
=> data_to_text
($data->{$key}, $result_schema->{properties
}->{$key}) };
181 my $schema = { type
=> 'array', items
=> { type
=> 'object' }};
182 print_api_list
($kvstore, $schema, ['key', 'value'], 0, $format eq 'text', $columns);
183 } elsif ($type eq 'array') {
184 return if !scalar(@$data);
185 my $item_type = $result_schema->{items
}->{type
};
186 if ($item_type eq 'object') {
187 print_api_list
($data, $result_schema, $props_to_print, $sort_key, $format eq 'text', $columns);
189 foreach my $entry (@$data) {
190 print data_to_text
($entry) . "\n";
197 die "internal error: unknown output format"; # should not happen