Skip to content

Commit

Permalink
Add --pager flag for juv cat (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt authored Nov 5, 2024
1 parent c56c9ff commit df8dda1
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 50 deletions.
11 changes: 9 additions & 2 deletions src/juv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,14 @@ def exec_(
@cli.command()
@click.argument("notebook", type=click.Path(exists=True), required=True)
@click.option("--script", is_flag=True)
def cat(notebook: str, *, script: bool) -> None:
@click.option(
"--pager",
type=click.STRING,
help="The pager to use.",
default=lambda: os.environ.get("JUV_PAGER"),
hidden=True,
)
def cat(notebook: str, *, script: bool, pager: str | None) -> None:
"""Print notebook contents to stdout."""
from ._cat import cat

Expand All @@ -316,7 +323,7 @@ def cat(notebook: str, *, script: bool) -> None:
)
return

sys.stdout.write(cat(path, script=script))
cat(path=path, script=script, pager=pager)


def main() -> None:
Expand Down
36 changes: 35 additions & 1 deletion src/juv/_cat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import re
import sys
from typing import TYPE_CHECKING

import jupytext
Expand Down Expand Up @@ -41,7 +42,7 @@ def strip_python_frontmatter_comment(content: str) -> tuple[str, str]:
return "", content


def cat(nb: Path | dict, *, script: bool) -> str:
def notebook_contents(nb: Path | dict, *, script: bool) -> str:
fmt = "py:percent" if script else "md"
notebook = nb if isinstance(nb, dict) else jupytext.read(nb)
contents = jupytext.writes(notebook, fmt=fmt)
Expand All @@ -50,3 +51,36 @@ def cat(nb: Path | dict, *, script: bool) -> str:
else:
_, contents = strip_markdown_header(contents)
return contents.lstrip()


def cat(path: Path, *, script: bool, pager: str | None = None) -> None:
code = notebook_contents(path, script=script)

if pager:
import os
import subprocess

command = [pager, "-"]

# special case bat to apply syntax highlighting
if pager == "bat":
command.extend(
[
"--language",
"md" if not script else "py",
"--file-name",
f"{path.stem}.md" if not script else f"{path.stem}.py",
]
)

subprocess.run( # noqa: PLW1510, S603
command,
input=code.encode(),
stdout=sys.stdout,
stderr=sys.stderr,
env=os.environ,
)

else:
# otherwise, just print to stdout
sys.stdout.write(notebook_contents(path, script=script))
5 changes: 3 additions & 2 deletions src/juv/_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import jupytext

from ._cat import cat
from ._cat import notebook_contents


class EditorAbortedError(Exception):
Expand Down Expand Up @@ -66,7 +66,8 @@ def edit(path: Path, editor: str) -> None:
update["metadata"]["id"] = update["id"]
prev_cells[update["id"]] = update

text = open_editor(cat(prev_notebook, script=False), suffix=".md", editor=editor)
code = notebook_contents(path, script=False)
text = open_editor(code, suffix=".md", editor=editor)
new_notebook = jupytext.reads(text.strip(), fmt="md")

# Update the previous notebook cells with the new ones
Expand Down
Loading

0 comments on commit df8dda1

Please sign in to comment.