Created
October 27, 2024 17:41
-
-
Save willmcgugan/0cc14e4ce8ee4b3753e217c0c02763d8 to your computer and use it in GitHub Desktop.
Proof of concept, running scrollart (https://scrollart.org/) in Textual
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import asyncio | |
import asyncio.subprocess | |
from textual import work | |
from textual.app import App, ComposeResult | |
from textual.widgets import Log | |
class ScrollArt(App): | |
CSS = """ | |
Log { | |
overflow-x: scroll; | |
} | |
""" | |
def compose(self) -> ComposeResult: | |
yield Log(max_lines=1000) | |
def on_mount(self) -> None: | |
self.call_after_refresh(self.run_scrollart_script, sys.argv[1]) | |
@work | |
async def run_scrollart_script(self, path: str) -> None: | |
log = self.query_one(Log) | |
environ = dict(os.environ) | |
width, height = self.screen.scrollable_content_region.size | |
environ |= {"COLUMNS": str(width), "ROWS": str(height), "PYTHONUNBUFFERED": "1"} | |
process = await asyncio.create_subprocess_exec( | |
sys.executable, | |
path, | |
stdin=asyncio.subprocess.DEVNULL, | |
stdout=asyncio.subprocess.PIPE, | |
stderr=asyncio.subprocess.PIPE, | |
env=environ, | |
) | |
self.notify(str(process)) | |
assert process.stdout is not None | |
assert process.stderr is not None | |
while line_bytes := await process.stdout.readline(): | |
log.write(line_bytes.decode("utf-8")) | |
self.notify(str(process)) | |
stderr = (await process.stderr.read()).decode("utf-8") | |
log.write(stderr) | |
if __name__ == "__main__": | |
app = ScrollArt() | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment