Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Moult opened this issue Dec 14, 2024 · 1 comment
Open

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

Moult opened this issue Dec 14, 2024 · 1 comment

Comments

@Moult
Copy link
Contributor

Moult commented Dec 14, 2024

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.

@Moult
Copy link
Contributor Author

Moult commented Dec 15, 2024

Edit: nevermind :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant