Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions byexample/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@ def get_default_options_parser(cmdline_args):
help=
"select a terminal emulator to interpret the output (default to 'dumb')."
)
options_parser.add_argument(
"+term-type",
default='dumb',
help=
"set the TERM env var for the interpreters/runners (default to 'dumb', other values could be 'vt100'); if left empty, leave it unset."
)

options_parser.add_flag(
"type",
Expand Down
17 changes: 17 additions & 0 deletions byexample/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,15 @@ def _spawn_interpreter(
env = os.environ.copy()
if env_update:
env.update(env_update)

# Override the environment with LINES, COLUMNS (from geometry)
env.update({'LINES': str(rows), 'COLUMNS': str(cols)})

# With TERM=dumb is the safest option, TERM=vt100 may be needed to offer a dummy
# *but* functional TERM because some programs may print scary "WARNING: terminal is not fully functional"
term_kind = options['term_type'].strip()
env.update({'TERM': term_kind})

self._drop_output() # there shouldn't be any output yet but...
self._cmd = cmd

Expand Down Expand Up @@ -573,16 +580,26 @@ def _exec_and_wait(self, source, options, *, from_example=None, **kargs):
countdown = Countdown(timeout)
lines = source.split('\n')

if clog().isEnabledFor(DEBUG):
with log_with("sendlines") as clog2:
clog2.debug("\n > " + '\n > '.join(lines))

self._last_num_lines_sent = 0
for line in lines[:-1]:
with profile_ctx("sendline"):
# turn the echo off (may be)
self._may_turn_echo_off(options)

self._sendline(line)
self._last_num_lines_sent += 1
self._expect_prompt_or_type(
options, countdown, input_list=input_list
)

with profile_ctx("sendline"):
# turn the echo off (may be)
self._may_turn_echo_off(options)

self._sendline(lines[-1])
self._last_num_lines_sent += 1

Expand Down
51 changes: 47 additions & 4 deletions docs/advanced/terminal-emulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ is fast and works well in most cases.

But you can change this to disable emulation with ``+term=as-is`` or
to enable full ANSI terminal emulation with ``+term=ansi``.

## Dumb terminal

In this mode, ``byexample`` emulates a very simple terminal, processing
Expand Down Expand Up @@ -104,9 +103,9 @@ It can be useful in some special cases to check white spaces
that are removed by the dumb or ANSI terminals.

```python
>>> print("\tfoo\tbar\nbaz\tsaz \rtaz") # byexample: +term=as-is
>>> print("\tfoo\tbar\nbaz\tsaz\rtaz") # byexample: +term=as-is
foo bar
baz saz
baz saz
taz
```

Expand Down Expand Up @@ -163,7 +162,7 @@ that use ``ncurses``) and require a terminal emulator.
Examples of this are programs like ``less``, ``more``, ``top`` and ``man``.

```shell
$ less test/ds/python-tutorial.v2.md # byexample: +term=ansi +rm=  +stop-on-timeout
$ TERM=vt100 less test/ds/python-tutorial.v2.md # byexample: +term=ansi +rm=  +stop-on-timeout
 This is a 101 Python tutorial
 The following is an example written in Python about arithmetics

Expand Down Expand Up @@ -212,6 +211,50 @@ line 32
If this is a problem change the [geometry](/{{ site.uprefix }}/advanced/geometry):
increase the count of rows that the terminal has with ``+geometry``.

## ``TERM`` environment variable

While the ``+term`` controls how the *output* is emulated and can be
changed per example, ``byexample`` also sets the ``TERM`` environment
variable to indicate the interpreter (and possible any subprogram
executed by it) that the terminal if of a particular type.

Contrary to ``+term``, ``+term-type`` has effect only if it is set
in the command line before the interpreters are started and it cannot be
changed later.

By default, ``byexample`` sets ``TERM=dumb`` to indicate a
zero-capabilites terminal. You can change this setting from
the command line with ``-o +term-type=vt100`` (``vt100`` indicates a
basic but functional terminal).

```shell
$ byexample -l shell test/ds/echo-term-env-variable.md
<...>
TERM=dumb
<...>

$ byexample -l shell -o +term-type=vt100 test/ds/echo-term-env-variable.md
<...>
TERM=vt100
<...>
```

If you want to use the same ``TERM`` value that your real terminal has,
you can pass it (nothing special is needed):

```shell
$ TERM=xterm
$ byexample -l shell -o +term-type=$TERM test/ds/echo-term-env-variable.md
<...>
TERM=xterm
<...>
```

> *Changed* in `byexample 11.0.0`. Before `11.0.0.` the `TERM` variable
> was inherit from the caller's environment. Starting from `11.0.0`
> this is not longer the case. If you need the old behavior,
> do `-o +term-type=$TERM` as shown above.

<!--

The following tests make sure that the runners for C++, PHP and Elixir
Expand Down
Loading