Skip to content

Commit

Permalink
Merge pull request #34 from akcano/main
Browse files Browse the repository at this point in the history
make the terminal directive more consistent
  • Loading branch information
ru-fu authored Apr 15, 2024
2 parents 52c59c7 + a2bb0b9 commit d03d0cd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,19 +270,35 @@ You can override the style in your own style sheet.
To add a terminal view to your page, use the `:terminal:` directive and specify the the input (as `:input:` option) and output (as the main content of the directive).
Any lines prefixed with `:input:` in the main content are treated as input as well.

To override the prompt (`user@host:~$` by default), specify the `:user:` and/or `:host:` options.
To override the prompt (`user@host:~$` by default), specify the `:user:`, `:dir:` and/or `:host:` options.
To make the terminal scroll horizontally instead of wrapping long lines, add `:scroll:`.

For example, in MyST syntax:

````
Single command sample:
```{terminal}
:input: command number one
:input: single command
:user: root
:host: vm
:dir: /tmp/dir/
output line one
output line two
```
Multiple command sample:
```{terminal}
:user: root
:host: vm
:dir: /tmp/dir/
:input: a command
output line one
output line two
:input: another command
more output
```
Expand Down
19 changes: 10 additions & 9 deletions canonical-sphinx-extensions/terminal-output/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TerminalOutput(Directive):
"input": directives.unchanged,
"user": directives.unchanged,
"host": directives.unchanged,
"dir": directives.unchanged,
"scroll": directives.unchanged,
}

Expand All @@ -34,23 +35,23 @@ def input_line(prompt_text, command):

def run(self):

# if :user: or :host: are provided, replace those in the prompt
# Build prompt with :user:, :host: and :dir: (whichever is provided)

command = "" if "input" not in self.options else self.options["input"]
user = "user" if "user" not in self.options else self.options["user"]
host = "host" if "host" not in self.options else self.options["host"]
prompt_text = user + "@" + host + ":~$ "
if user == "root":
prompt_text = prompt_text[:-2] + "# "
command = self.options["input"] if "input" in self.options else ""
user = self.options["user"] if "user" in self.options else "user"
host = self.options["host"] if "host" in self.options else "host"
dir = self.options["dir"] if "dir" in self.options else "~"
prompt_text = f"{user}@{host}:{dir}{'#' if user == 'root' else '$'} "

out = nodes.container()
out["classes"].append("terminal")
if "scroll" in self.options:
out["classes"].append("scroll")

# Add the original prompt and input
# Add the original prompt and input if the command is present

out.append(self.input_line(prompt_text, command))
if command:
out.append(self.input_line(prompt_text, command))

# Go through the content and append all lines as output
# except for the ones that start with ":input: " - those get
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = canonical-sphinx-extensions
version = 0.0.20
version = 0.0.21
author = Ruth Fuchss
author_email = [email protected]
description = A collection of Sphinx extensions used by Canonical documentation
Expand Down

0 comments on commit d03d0cd

Please sign in to comment.