snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit d8ecba5eae4dc0a0f2dc05134f2aee159178ae1b
parent 92b4a791dbfd1060aa54f677e06edcedaf550c3a
Author: Josh Moore <jxm5210@rit.edu>
Date:   Thu, 28 May 2020 17:36:29 -0400

Add snaketerm

Diffstat:
Asnakewm/apps/tools/snaketerm/__init__.py | 13+++++++++++++
Asnakewm/apps/tools/snaketerm/term.py | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/snakewm/apps/tools/snaketerm/__init__.py b/snakewm/apps/tools/snaketerm/__init__.py @@ -0,0 +1,13 @@ +from .term import SnakeTerm + +def load(manager, params): + """ + Create an launch a new instance of SnakeTerm. + """ + # default position + pos = (100, 100) + + if params is not None and len(params) > 0: + pos = params[0] + + SnakeTerm(pos, manager) diff --git a/snakewm/apps/tools/snaketerm/term.py b/snakewm/apps/tools/snaketerm/term.py @@ -0,0 +1,73 @@ +import sys +import traceback + +from io import StringIO + +import pygame +import pygame_gui + +from pygame_gui.elements import UITextBox + +class SnakeTerm(pygame_gui.elements.UIWindow): + def __init__(self, pos, manager): + super().__init__( + pygame.Rect(pos, (400,300)), + manager, + window_display_title='snaketerm', + object_id='#snaketerm', + resizable=True + ) + + self.textbox = pygame_gui.elements.UITextBox( + "", + relative_rect=pygame.Rect(0, 0, 368, 200), + manager=manager, + container=self, + anchors={ + 'left': 'left', + 'right': 'right', + 'top': 'top', + 'bottom': 'bottom' + } + ) + + self.input = pygame_gui.elements.UITextEntryLine( + relative_rect=pygame.Rect(0, -35, 368, 30), + manager=manager, + container=self, + anchors={ + 'left': 'left', + 'right': 'right', + 'top': 'bottom', + 'bottom': 'bottom' + } + ) + + def set_text(self, text): + self.textbox.html_text = text.replace('\n', '<br>') + self.textbox.rebuild() + + def append_text(self, text): + self.textbox.html_text = self.textbox.html_text + text.replace('\n', '<br>') + self.textbox.rebuild() + + def process_event(self, event): + super().process_event(event) + + if event.type == pygame.KEYUP and event.key == pygame.K_RETURN: + _stdout = sys.stdout + sys.stdout = tout = StringIO() + + try: + code = compile(self.input.get_text(), 'snaketerm_code', 'exec') + exec(code, globals()) + except Exception: + e_type,e_val,e_traceback = sys.exc_info() + print('Traceback (most recent call last):') + traceback.print_tb(e_traceback, None, tout) + print(e_type, e_val) + + sys.stdout = _stdout + result = tout.getvalue() + self.append_text(result) + self.input.set_text('')