Skip to content

Commit

Permalink
Added pyte to handle terminal schenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
vadim0x60 committed Oct 3, 2023
1 parent ec2a2f1 commit 6b08d8c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ jobs:
- name: Build and publish to pypi
uses: JRubics/poetry-publish@master
with:
python_version: "3.10.8"
pypi_token: ${{ secrets.PYPI_TOKEN }}
2 changes: 2 additions & 0 deletions programlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .language import Language, languages, language_
from .agent import Agent
from .terminal import Terminal
from .program import Program
6 changes: 3 additions & 3 deletions programlib/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from uuid import uuid4
from pathlib import Path
from itertools import zip_longest
from programlib.agent import Agent

from programlib import language_
from programlib import Agent, language_, Terminal

def correctness(expected_outputs, outputs):
assert expected_outputs, 'expected_outputs is empty. Cannot test program correctness.'
Expand All @@ -29,6 +28,7 @@ class Program():
def __init__(self, source=None, name=None, language='C++',
workdir=Path(__file__).parent / 'programs'):
self.language = language_(language)
self.term = Terminal()

self.workdir = workdir
self.name = name or str(uuid4())
Expand Down Expand Up @@ -62,7 +62,7 @@ def run(self, input_lines=[], force=True):

self.stdout, self.exitstatus = self.language.run(self.workdir, self.name, input_lines)
assert force or not self.exitstatus, f'Exit status {self.exitstatus}'
return self.stdout.splitlines()
return repr(self.stdout).splitlines()

def spawn(self, delimiter='\n'):
"""
Expand Down
18 changes: 18 additions & 0 deletions programlib/terminal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pyte

class Terminal:
def __init__(self, rows=24, cols=80):
self.screen = pyte.Screen(cols, rows)
self.stream = pyte.Stream(self.screen)

def emulate(self, raw_output, clean=True):
self.stream.feed(raw_output)

lines = self.screen.display
self.screen.reset()

if clean:
lines = (line.rstrip() for line in lines)
lines = (line for line in lines if line)

return lines
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ python = "^3.8"
pexpect = "^4.8.0"
gym = "^0.26.2"
numpy = "^1.24.2"
pyte = "^0.8.0"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 6b08d8c

Please sign in to comment.