Skip to content

Scroll up / down and scroll region escape sequence not supported #186

@Moult

Description

@Moult

There is a feature where you can set a scroll region and scroll up / down that portion of the screen. This is used in NetHack ttyrecs. See this sample code to demonstrate it:

import sys
import time

# Define helper functions to send escape sequences
def esc(seq):
    sys.stdout.write(f"\x1b[{seq}")
    sys.stdout.flush()

# Clear the screen
esc("2J")  # CSI 2J clears the screen

# Move cursor to home position (1,1)
esc("H")


# Fill the terminal with some text
for i in range(1, 15):
    print(f"Line {i}")

# Set a scroll region from line 2 to 10
esc("2;10r")

# Pause to observe initial state
time.sleep(2)

esc("1S")  # Scroll up 1 line
time.sleep(2)

# Scroll up within the scroll region
# for _ in range(3):
#     esc("1S")  # Scroll up 1 line
#     time.sleep(0.5)

# Scroll down within the scroll region
# for _ in range(2):
#     esc("1T")  # Scroll down 1 line
#     time.sleep(0.5)

# Reset scroll region to full screen
esc("r")

And here is how you can recreate it using pyte:

import pyte

screen = pyte.Screen(80, 20)
stream = pyte.Stream(screen)

asdf = [
b'\x1b[2J', # clear screen
b'\x1b[H', # go to home (1, 1)
]
for data in asdf:
    stream.feed(data.decode('cp437'))

for i in range(1, 15):
    stream.feed(f"Line {i}\r\n")

# stream.do_thing()
stream.feed(b"\x1b[2;10r".decode('cp437')) # Set scroll region
stream.feed(b"\x1b[2T".decode('cp437')) # Scroll down 2 lines
stream.feed(b"\x1b[r".decode('cp437')) # Reset scroll region

for x in screen.display:
    print(repr(x))

Pyte outputs this:

'Line 1                                                                          '
'Line 2                                                                          '
'Line 3                                                                          '
'Line 4                                                                          '
'Line 5                                                                          '
'Line 6                                                                          '
'Line 7                                                                          '
'Line 8                                                                          '
'Line 9                                                                          '
'Line 10                                                                         '
'Line 11                                                                         '
'Line 12                                                                         '
'Line 13                                                                         '
'Line 14                                                                         '
'                                                                                '
'                                                                                '
'                                                                                '
'                                                                                '
'                                                                                '
'                                                                                '

As you can see nothing is scrolled.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions