Merge "Autofit table output if stdout is a tty"

This commit is contained in:
Zuul 2023-09-11 09:24:41 +00:00 committed by Gerrit Code Review
commit 76b5df665b
1 changed files with 10 additions and 1 deletions

View File

@ -13,6 +13,7 @@
"""Output formatters using prettytable."""
import os
import sys
import prettytable
@ -32,6 +33,14 @@ def _format_row(row):
return new_row
def _do_fit(fit_width):
if os.name == 'nt':
# NOTE(pas-ha) the isatty is not reliable enough on Windows,
# so do not try to auto-fit
return fit_width
return sys.stdout.isatty() or fit_width
class TableFormatter(base.ListFormatter, base.SingleFormatter):
ALIGNMENTS = {
@ -177,7 +186,7 @@ class TableFormatter(base.ListFormatter, base.SingleFormatter):
"""
if max_width > 0:
term_width = max_width
elif not fit_width:
elif not _do_fit(fit_width):
# Fitting is disabled
return
else: