Skip to content

Commit 31f9514

Browse files
authored
feat(cli): add pager support to renku log (#2932)
1 parent c99659f commit 31f9514

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

poetry.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

renku/ui/cli/log.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
DatasetLogViewModel,
6666
LogViewModel,
6767
)
68+
from renku.ui.cli.utils.terminal import show_text_with_pager, strip_ansi_codes
6869

6970

7071
def _print_log(log_entry: LogViewModel) -> str:
@@ -208,14 +209,23 @@ def _print_dataset_log(log_entry: DatasetLogViewModel) -> str:
208209
)
209210
@click.option("-w", "--workflows", is_flag=True, default=False, help="Show only workflow executions.")
210211
@click.option("-d", "--datasets", is_flag=True, default=False, help="Show only dataset modifications.")
211-
def log(columns, format, workflows, datasets):
212+
@click.option("--no-pager", is_flag=True, help="Don't use pager (less) for output.")
213+
@click.option("-c", "--no-color", is_flag=True, help="Do not colorize output.")
214+
def log(columns, format, workflows, datasets, no_pager, no_color):
212215
"""Show a history of renku workflow and dataset commands."""
213216
from renku.command.log import log_command
214217

215218
result = log_command().with_database().build().execute(workflows_only=workflows, datasets_only=datasets).output
216219
if format == "detailed":
217220
entries = sorted(result, key=lambda e: e.date, reverse=True)
218-
texts = [_print_log(e) for e in entries]
219-
click.echo("\n\n".join(texts))
221+
text = "\n\n".join([_print_log(e) for e in entries])
222+
223+
if no_color:
224+
text = strip_ansi_codes(text)
225+
226+
if no_pager:
227+
click.echo(text)
228+
else:
229+
show_text_with_pager(text)
220230
else:
221-
click.echo(LOG_FORMATS[format](result, columns))
231+
click.echo(LOG_FORMATS[format](result, columns), not no_pager)

renku/ui/cli/utils/terminal.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
"""Utility functions for ViewModels."""
1919

2020
import functools
21+
import pydoc
22+
import re
23+
import shutil
2124
from typing import TYPE_CHECKING
2225

2326
import click
@@ -31,6 +34,27 @@
3134
style_key = functools.partial(click.style, bold=True, fg=color.MAGENTA)
3235
style_value = functools.partial(click.style, bold=True)
3336

37+
ANSI_REGEX = re.compile(r"(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F]|(?:\x1B\[|\x9B)[0-?]*[ -/]*[@-~])")
38+
39+
40+
def strip_ansi_codes(text: str) -> str:
41+
"""Remove ANSI formatting codes from text."""
42+
return ANSI_REGEX.sub("", text)
43+
44+
45+
def show_text_with_pager(text: str) -> None:
46+
"""Print text with a pager (i.e. ``less``) if appropriate.
47+
48+
Args:
49+
text(str): The text to print.
50+
"""
51+
tty_size = shutil.get_terminal_size(fallback=(120, 40))
52+
53+
if len(text.splitlines()) >= tty_size.lines:
54+
pydoc.pipepager(text, "less --chop-long-lines -R --tilde")
55+
else:
56+
click.echo(text)
57+
3458

3559
def print_markdown(text: str):
3660
"""Print markdown text to console."""
@@ -44,7 +68,8 @@ def print_plan(plan: "PlanViewModel", err: bool = False):
4468
"""Print a plan to stderr.
4569
4670
Args:
47-
err: Print to ``stderr``.
71+
plan(PlanViewModel): The plan to print.
72+
err(bool,optional): Print to ``stderr`` (Default value = False).
4873
"""
4974

5075
def print_key_value(key, value, print_empty: bool = True):

renku/ui/cli/workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,6 @@
649649
"""
650650

651651
import os
652-
import pydoc
653652
import shutil
654653
import sys
655654
from pathlib import Path
@@ -665,6 +664,7 @@
665664
from renku.core import errors
666665
from renku.ui.cli.utils.callback import ClickCallback
667666
from renku.ui.cli.utils.plugins import available_workflow_providers, supported_formats
667+
from renku.ui.cli.utils.terminal import show_text_with_pager
668668

669669
if TYPE_CHECKING:
670670
from renku.command.view_model.composite_plan import CompositePlanViewModel
@@ -1147,7 +1147,7 @@ def visualize(sources, columns, exclude_files, ascii, interactive, no_color, pag
11471147
use_pager = True
11481148

11491149
if use_pager:
1150-
pydoc.tempfilepager(text_output, "less --chop-long-lines -R --tilde")
1150+
show_text_with_pager(text_output)
11511151
else:
11521152
click.echo(text_output)
11531153
return

0 commit comments

Comments
 (0)