Skip to content

Commit

Permalink
feat: Add CLI interface, with config file
Browse files Browse the repository at this point in the history
  • Loading branch information
nickderobertis committed Jun 20, 2022
1 parent 96679f5 commit 4f7717f
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 98 deletions.
5 changes: 5 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ terminhtml = "*"
playwright = "*"
moviepy = "*"
rich = "*"
cliconf = "*"
py-app-conf = "==1.3.0a10"

[pipenv]
allow_prereleases = true
189 changes: 95 additions & 94 deletions Pipfile.lock

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
]

# Short description of the package
PACKAGE_SHORT_DESCRIPTION = "Create video and image files via TerminHTML rather than HTML/CSS/JS"
PACKAGE_SHORT_DESCRIPTION = (
"Create video and image files via TerminHTML rather than HTML/CSS/JS"
)

# Long description of the package for PyPI
# Set to 'auto' to use README.md as the PyPI description
Expand Down Expand Up @@ -97,7 +99,7 @@

# Add any Python scripts which should be exposed to the command line in the format:
# CONSOLE_SCRIPTS = ['funniest-joke=funniest.command_line:main']
CONSOLE_SCRIPTS = ([],)
CONSOLE_SCRIPTS = (["terminrec=terminhtml_recorder.cli:cli"],)

# Add any arbitrary scripts to be exposed to the command line in the format:
# SCRIPTS = ['bin/funniest-joke']
Expand Down
7 changes: 6 additions & 1 deletion terminhtml_recorder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
"""
Create video and image files via TerminHTML rather than HTML/CSS/JS
"""
from terminhtml_recorder.recorder import TerminHTMLRecorder
from terminhtml_recorder.formats import OutputFormat
from terminhtml_recorder.recorder import (
PageLocators,
TerminHTMLRecorder,
default_page_interactor,
)
3 changes: 3 additions & 0 deletions terminhtml_recorder/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from terminhtml_recorder.cli import cli

cli()
89 changes: 89 additions & 0 deletions terminhtml_recorder/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import os
import sys
from pathlib import Path
from typing import Optional

import typer
from cliconf import CLIAppConfig, CLIConf, configure
from pyappconf import ConfigFormats

from terminhtml_recorder.exc import NoHTMLFoundException
from terminhtml_recorder.formats import OutputFormat
from terminhtml_recorder.recorder import (
PageInteractor,
TerminHTMLRecorder,
default_page_interactor,
)

cli = CLIConf(
name="terminhtml-recorder",
help="Record TerminHTML animated output to a video file.",
)


@cli.command()
@configure(
CLIAppConfig(
app_name="terminhtml-recorder",
config_name="terminrec",
default_format=ConfigFormats.PY,
py_config_imports=[
"from pathlib import Path",
"from terminhtml_recorder import PageLocators, default_page_interactor, OutputFormat",
"import terminhtml_recorder.recorder",
"from terminhtml_recorder.cli import record",
],
)
)
def record(
in_path: Optional[Path] = typer.Option(
None,
"--input-path",
"-i",
help="Path of file containing TerminHTML output. If not passed, must be piped to stdin.",
),
out_path: Optional[Path] = typer.Option(
None,
"--output-path",
"-o",
help="Path of file to write recording to. Defaults to terminhtml.<output-format>",
show_default=False,
),
output_format: Optional[OutputFormat] = typer.Option(
OutputFormat.GIF, "--format", "-f", help="Format of recording"
),
delay: float = typer.Option(
1.1,
"--delay",
"-d",
help="Delay from browser start until recording start. Increase if you see an uninitialized terminal at the start. Decrease if you don't see the beginning of typing.",
),
interactor: PageInteractor = default_page_interactor,
):
"""
Record TerminHTML animated output to a video file.
"""
if in_path is not None:
html = in_path.read_text()
# Else if input was piped to stdin
elif not sys.stdin.isatty() and not os.getenv("PYCHARM_HOSTED"):
# Read HTML from stdin
html = sys.stdin.read()
else:
raise NoHTMLFoundException(
f"No HTML passed. Must either specify an input file or pipe HTML to stdin."
)

if not html:
raise NoHTMLFoundException(
f"Empty HTML found. Must either specify a valid input file or pipe HTML to stdin."
)

out_path = out_path or Path("terminhtml").with_suffix(f".{output_format.value}")

recorder = TerminHTMLRecorder(html, interactor)
recorder.record(out_path, format=output_format, delay=delay)


if __name__ == "__main__":
cli()
6 changes: 6 additions & 0 deletions terminhtml_recorder/exc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TerminHTMLRecorderException(Exception):
pass


class NoHTMLFoundException(TerminHTMLRecorderException):
pass
1 change: 1 addition & 0 deletions tests/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
INPUT_FILES_DIR = TESTS_DIR / "input_files"

TERMINHTML_DEMO_GIF = INPUT_FILES_DIR / "terminhtml-demo.gif"
TERMINHTML_DEMO_HTML = INPUT_FILES_DIR / "terminhtml-demo.html"
3 changes: 2 additions & 1 deletion tests/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from terminhtml_recorder.formats import OutputFormat
from terminhtml_recorder.recorder import Recording, TerminHTMLRecorder
from tests.config import TERMINHTML_DEMO_GIF
from tests.config import TERMINHTML_DEMO_GIF, TERMINHTML_DEMO_HTML


def create_terminhtml_demo_gif(out_path: Path = TERMINHTML_DEMO_GIF) -> Recording:
Expand All @@ -19,6 +19,7 @@ def create_terminhtml_demo_gif(out_path: Path = TERMINHTML_DEMO_GIF) -> Recordin
input=["Nick DeRobertis"],
)
text = term.to_html()
TERMINHTML_DEMO_HTML.write_text(text)
recorder = TerminHTMLRecorder(text)
recording = recorder.record(out_path, OutputFormat.GIF)
return recording
Expand Down
Binary file modified tests/input_files/terminhtml-demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 4f7717f

Please sign in to comment.