use strict;
use warnings;
+use I18N::Langinfo;
+
use PVE::JSONSchema;
+use PVE::PTY;
use JSON;
+use utf8;
+use Encode;
+
+sub query_terminal_options {
+ my ($options) = @_;
+
+ $options //= {};
+
+ if (-t STDOUT) {
+ ($options->{columns}) = PVE::PTY::tcgetsize(*STDOUT);
+ }
+
+ $options->{encoding} = I18N::Langinfo::langinfo(I18N::Langinfo::CODESET());
+
+ $options->{utf8} = 1 if $options->{encoding} eq 'UTF-8';
+
+ return $options;
+}
sub println_max {
- my ($text, $max) = @_;
+ my ($text, $encoding, $max) = @_;
if ($max) {
my @lines = split(/\n/, $text);
foreach my $line (@lines) {
- print substr($line, 0, $max) . "\n";
+ print encode($encoding, substr($line, 0, $max) . "\n");
}
} else {
- print $text;
+ print encode($encoding, $text);
}
}
my $border = $options->{border};
my $columns = $options->{columns};
my $utf8 = $options->{utf8};
+ my $encoding = $options->{encoding} // 'UTF-8';
my $autosort = 1;
if (defined($sort_key) && $sort_key eq 0) {
my $colopts = {};
- my $borderstring = '';
+ my $borderstring_m = '';
+ my $borderstring_b = '';
+ my $borderstring_t = '';
my $formatstring = '';
my $column_count = scalar(@$props_to_print);
};
if ($border) {
- if ($i == ($column_count - 1)) {
- $formatstring .= "| %-${cutoff}s |\n";
- $borderstring .= "+-" . ('-' x $cutoff) . "-+\n";
+ if ($i == 0 && ($column_count == 1)) {
+ if ($utf8) {
+ $formatstring .= "│ %-${cutoff}s │\n";
+ $borderstring_t .= "┌─" . ('─' x $cutoff) . "─┐\n";
+ $borderstring_m .= "├─" . ('─' x $cutoff) . "─┤\n";
+ $borderstring_b .= "└─" . ('─' x $cutoff) . "─┘\n";
+ } else {
+ $formatstring .= "| %-${cutoff}s |\n";
+ $borderstring_m .= "+-" . ('-' x $cutoff) . "-+\n";
+ }
+ } elsif ($i == 0) {
+ if ($utf8) {
+ $formatstring .= "│ %-${cutoff}s ";
+ $borderstring_t .= "┌─" . ('─' x $cutoff) . '─';
+ $borderstring_m .= "├─" . ('─' x $cutoff) . '─';
+ $borderstring_b .= "└─" . ('─' x $cutoff) . '─';
+ } else {
+ $formatstring .= "| %-${cutoff}s ";
+ $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
+ }
+ } elsif ($i == ($column_count - 1)) {
+ if ($utf8) {
+ $formatstring .= "│ %-${cutoff}s │\n";
+ $borderstring_t .= "┬─" . ('─' x $cutoff) . "─┐\n";
+ $borderstring_m .= "┼─" . ('─' x $cutoff) . "─┤\n";
+ $borderstring_b .= "┴─" . ('─' x $cutoff) . "─┘\n";
+ } else {
+ $formatstring .= "| %-${cutoff}s |\n";
+ $borderstring_m .= "+-" . ('-' x $cutoff) . "-+\n";
+ }
} else {
- $formatstring .= "| %-${cutoff}s ";
- $borderstring .= "+-" . ('-' x $cutoff) . '-';
+ if ($utf8) {
+ $formatstring .= "│ %-${cutoff}s ";
+ $borderstring_t .= "┬─" . ('─' x $cutoff) . '─';
+ $borderstring_m .= "┼─" . ('─' x $cutoff) . '─';
+ $borderstring_b .= "┴─" . ('─' x $cutoff) . '─';
+ } else {
+ $formatstring .= "| %-${cutoff}s ";
+ $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
+ }
}
} else {
# skip alignment and cutoff on last column
}
}
- println_max($borderstring, $columns) if $border;
+ $borderstring_t = $borderstring_m if !length($borderstring_t);
+ $borderstring_b = $borderstring_m if !length($borderstring_b);
+
+ println_max($borderstring_t, $encoding, $columns) if $border;
my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
- println_max($text, $columns);
+ println_max($text, $encoding, $columns);
foreach my $entry (@$data) {
- println_max($borderstring, $columns) if $border;
+ println_max($borderstring_m, $encoding, $columns) if $border;
$text = sprintf $formatstring, map {
substr(data_to_text($entry->{$_}, $returnprops->{$_}) // $colopts->{$_}->{default},
0, $colopts->{$_}->{cutoff});
} @$props_to_print;
- println_max($text, $columns);
+ println_max($text, $encoding, $columns);
}
- println_max($borderstring, $columns) if $border;
+ println_max($borderstring_b, $encoding, $columns) if $border;
}
# prints the result of an API GET call returning an array as a table.
sub print_api_result {
my ($format, $data, $result_schema, $props_to_print, $options) = @_;
- $options //= {};
- $options = { %$options }; # copy
+ if (!defined($options)) {
+ $options = query_terminal_options({});
+ } else {
+ $options = { %$options }; # copy
+ }
return if $result_schema->{type} eq 'null';
if ($format eq 'json') {
+ # Note: we always use utf8 encoding for json format
print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
} elsif ($format eq 'text' || $format eq 'plain') {
+ my $encoding = $options->{encoding} // 'UTF-8';
my $type = $result_schema->{type};
if ($type eq 'object') {
$props_to_print = [ sort keys %$data ] if !defined($props_to_print);
print_api_list($data, $result_schema, $props_to_print, $options);
} else {
foreach my $entry (@$data) {
- print data_to_text($entry) . "\n";
+ print encode($encoding, data_to_text($entry) . "\n");
}
}
} else {
- print "$data\n";
+ print encode($encoding, "$data\n");
}
} else {
die "internal error: unknown output format"; # should not happen