terminhtml package

Run shell commands and convert into an HTML/CSS animated terminal

Subpackages

Submodules

terminhtml.ansi_converter module

terminhtml.ansi_converter.ansi_styles()[source]

Returns the ansi2html stylesheet, narrowed to what this block of ansi needs.

Return type:

str

terminhtml.ansi_converter.ansi_to_html(ansi)[source]

Converts an ansi string to html.

Return type:

str

terminhtml.ansi_converter.strip_all_ansi(string)[source]
Return type:

str

terminhtml.base_exc module

exception terminhtml.base_exc.TerminHTMLException[source]

Bases: Exception

Base class for all exceptions in this package.

terminhtml.cli module

terminhtml.cli.run_commands_create_html(commands=typer.Argument, setup_commands=typer.Option, input=typer.Option, allow_exceptions=typer.Option, prompt_matchers=typer.Option, command_timeout=typer.Option, cwd=typer.Option, out_path=typer.Option, partial_html=typer.Option, inline_css=typer.Option, echo=typer.Option, no_force_color=typer.Option)

Create an animated HTML/CSS terminal by running commands and recording their output.

Examples: ``` # Run two commands $ terminhtml “echo woo > something.txt” “cat something.txt”

# Output results to a file $ terminhtml -o output.html “echo woo”

# Run a script my-script.sh $ terminhtml “$(<my-script.sh)”

# Run setup commands before the animated session $ terminhtml “cat woo.txt” -s “echo a > woo.txt && echo b >> woo.txt”

# Output partial HTML $ terminhtml -p “echo woo”

# Allow longer-running commands $ terminhtml -t 20 “sleep 15 && echo woo”

# Animate commands that prompt for user input $ terminhtml -m “] ” -i “woo” “read -p ‘[value?] ‘ varname && echo $varname”

# Run commands in a specific directory rather than a temporary directory $ terminhtml -c .. “ls -l”

# Echo the commands to stdout $ terminhtml -e “echo foo”

# Turn off forcing color output, use the terminal’s color setting $ terminhtml -n “man grep | grep grep” ```

Return type:

None

terminhtml.cli_docs module

terminhtml.demo_output module

class terminhtml.demo_output.ColorBox[source]

Bases: object

terminhtml.demo_output.make_test_card()[source]

Get a renderable that demonstrates a number of features.

Return type:

Table

terminhtml.exc module

exception terminhtml.exc.IncorrectCommandSpecificationException[source]

Bases: TerminHTMLUserException

Raised if the command specification is incorrect.

exception terminhtml.exc.TerminHTMLUserException[source]

Bases: TerminHTMLException

Base class for all expected exceptions in TerminHTML.

exception terminhtml.exc.UserCommandException(message, output, context)[source]

Bases: TerminHTMLUserException

Exception for user commands.

__init__(message, output, context)[source]

terminhtml.main module

class terminhtml.main.CommandResults(**data)[source]

Bases: BaseModel

results: List[CommandResult]
class terminhtml.main.TerminHTML(**data)[source]

Bases: BaseModel

command_results: CommandResults
classmethod from_commands(commands, setup_commands=None, input=None, allow_exceptions=False, prompt_matchers=None, command_timeout=10, cwd=None, echo=False, force_color=True)[source]

Create a TerminHTML object from a list of commands.

Parameters:
  • commands (Sequence[str]) – A list of commands to run.

  • setup_commands (Optional[List[str]]) – A command to run before the commands, the output of these commands will not show.

  • input (Union[List[str], str, None]) – A list of strings to send to the commands. The input element will be matched with the command by index. If a single string is passed, it will be converted into a single-element list so that it will be passed to the first command.

  • allow_exceptions (bool) – If True, exceptions will be raised.

  • prompt_matchers (Optional[List[str]]) – A list of regex strings to match the prompt of the command. If a prompt is matched, it will be provided the matched input.

  • command_timeout (int) – The timeout in seconds for each command. If a command times out, the process will fail.

  • cwd (Optional[Path]) – The working directory to run the commands in. Defaults to a temporary directory when None is passed.

  • echo (bool) – If True, the output of the commands will be echoed to the console.

  • force_color (bool) – If True, will try to emulate a terminal with support for ANSI 8-bit color. If False, will allow the terminal settings to pass through from the terminhtml caller.

Return type:

TerminHTML

Returns:

A TerminHTML object.

get_styles_for_inline_html(inline_css=False)[source]
Return type:

str

property styles: str
Return type:

str

Return type:

str

to_html(full=True, inline_css=False)[source]

Convert the TerminHTML object to HTML.

Parameters:
  • full (bool) – If True, the HTML will be a full standalone page. If False, the HTML will be just the terminal part.

  • inline_css (bool) – If True, the CSS will be included inline. If False, the CSS will be linked to a stylesheet. Note that if full is False, this parameter is ignored.

Return type:

str

Returns:

The HTML string.

terminhtml.output module

class terminhtml.output.LineEnding(value)[source]

Bases: str, Enum

An enumeration.

CR = '\r'
CRLF = '\r\n'
LF = '\n'
class terminhtml.output.LineOutput(**data)[source]

Bases: BaseModel

line: str
line_ending: LineEnding
prompt_output: Optional[PromptOutput]
time: datetime
class terminhtml.output.Output(**data)[source]

Bases: BaseModel

property delays: List[int]
Return type:

List[int]

lines: List[LineOutput]
class terminhtml.output.PromptOutput(**data)[source]

Bases: BaseModel

prompt: str
user_input: str