fix(ls): support tabulate 0.10.0 preserve_whitespace (#11021)

This commit is contained in:
skshetry
2026-03-31 10:32:56 +05:45
committed by GitHub
parent 333edb2cbf
commit 0f4e8a23c8
3 changed files with 31 additions and 19 deletions
+1 -9
View File
@@ -109,8 +109,6 @@ def _build_tree_structure(
def show_tree(entries, with_color=False, with_size=False, with_hash=False): def show_tree(entries, with_color=False, with_size=False, with_hash=False):
import tabulate
rows = _build_tree_structure( rows = _build_tree_structure(
entries, entries,
with_color=with_color, with_color=with_color,
@@ -119,13 +117,7 @@ def show_tree(entries, with_color=False, with_size=False, with_hash=False):
) )
colalign = ("right",) if with_size else None colalign = ("right",) if with_size else None
ui.table(rows, colalign=colalign, preserve_whitespace=True)
_orig = tabulate.PRESERVE_WHITESPACE
tabulate.PRESERVE_WHITESPACE = True
try:
ui.table(rows, colalign=colalign)
finally:
tabulate.PRESERVE_WHITESPACE = _orig
class CmdList(CmdBaseNoRepo): class CmdList(CmdBaseNoRepo):
+3 -1
View File
@@ -289,7 +289,7 @@ class Console:
return console.Console(stderr=True) return console.Console(stderr=True)
def table( def table( # noqa: PLR0913
self, self,
data: "TableData", data: "TableData",
headers: Optional["Headers"] = None, headers: Optional["Headers"] = None,
@@ -301,6 +301,7 @@ class Console:
row_styles: Optional[Sequence["Styles"]] = None, row_styles: Optional[Sequence["Styles"]] = None,
borders: Union[bool, str] = False, borders: Union[bool, str] = False,
colalign: Optional[tuple[str, ...]] = None, colalign: Optional[tuple[str, ...]] = None,
preserve_whitespace: bool = False,
) -> None: ) -> None:
from dvc.ui import table as t from dvc.ui import table as t
@@ -329,6 +330,7 @@ class Console:
pager=pager, pager=pager,
force=force, force=force,
colalign=colalign, colalign=colalign,
preserve_whitespace=preserve_whitespace,
) )
def status(self, status: str, **kwargs: Any) -> "Status": def status(self, status: str, **kwargs: Any) -> "Status":
+27 -9
View File
@@ -30,19 +30,37 @@ def plain_table(
pager: bool = False, pager: bool = False,
force: bool = True, force: bool = True,
colalign: Optional[tuple[str, ...]] = None, colalign: Optional[tuple[str, ...]] = None,
preserve_whitespace: bool = False,
) -> None: ) -> None:
import tabulate as tabulate_mod
from funcy import nullcontext from funcy import nullcontext
from packaging.version import Version
from tabulate import tabulate from tabulate import tabulate
text: str = tabulate( # NOTE: tabulate 0.10.0+ supports preserve_whitespace as a kwarg,
data, # while 0.9.0 only respects the global PRESERVE_WHITESPACE.
headers if headers is not None else (), kwargs: dict = {}
tablefmt="github" if markdown else "plain", _orig = tabulate_mod.PRESERVE_WHITESPACE
disable_numparse=True, if preserve_whitespace:
# None will be shown as "" by default, overriding if Version(tabulate_mod.__version__) >= Version("0.10"):
missingval="-", kwargs["preserve_whitespace"] = True
colalign=colalign, else:
) tabulate_mod.PRESERVE_WHITESPACE = True
try:
text: str = tabulate(
data,
headers if headers is not None else (),
tablefmt="github" if markdown else "plain",
disable_numparse=True,
# None will be shown as "" by default, overriding
missingval="-",
colalign=colalign,
**kwargs,
)
finally:
tabulate_mod.PRESERVE_WHITESPACE = _orig
if markdown: if markdown:
# NOTE: md table is incomplete without the trailing newline # NOTE: md table is incomplete without the trailing newline
text += "\n" text += "\n"