From 4ac8500023831f47b64d8fcfda341423fbfc2ce1 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 29 Jun 2018 13:15:24 +0200 Subject: [PATCH] PVE::CLIFormatter::query_terminal_options - new helper Signed-off-by: Dietmar Maurer --- src/PVE/CLIFormatter.pm | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/PVE/CLIFormatter.pm b/src/PVE/CLIFormatter.pm index 77f528b..cbe6390 100644 --- a/src/PVE/CLIFormatter.pm +++ b/src/PVE/CLIFormatter.pm @@ -2,11 +2,30 @@ package PVE::CLIFormatter; 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, $encoding, $max) = @_; @@ -217,8 +236,11 @@ sub print_api_list { 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'; -- 2.39.2