You are not logged in.

#1 2023-05-02 21:36:46

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 1,976
Website

Introducing gh4st - a lo-fi terminal emulator

Inspired by this: https://bbs.archlinux.org/viewtopic.php … 2#p2083502

#! /usr/bin/env python3
"""A terminal emulator full of hot air."""

from subprocess import PIPE, Popen
from tkinter import END, Tk, Text, Event
from typing import Callable


def shellexec(text: Text) -> Callable[[Event], None]:
    """Run a pseudo-shell on the text input."""

    def run(_: Event, shell: str = '/bin/bash') -> None:
        """Shell wrapper."""

        command = text.get('end-1c linestart', 'end-1c')

        if command.strip() == 'exit':
            raise SystemExit()

        with Popen(shell, stdin=PIPE, stdout=PIPE, stderr=PIPE) as bash:
            stdout, stderr = bash.communicate(command.encode('utf-8'))
            text.insert(END, stdout.strip())
            text.insert(END, stderr.strip())

    return run


def main(title: str = __doc__) -> None:
    """Main GUI."""

    root = Tk()
    root.resizable(False, False)
    root.title(title)
    text = Text(root, height=16)
    text.bind('<Return>', shellexec(text))
    text.pack()
    root.mainloop()


if __name__ == '__main__':
    main()

macro_rules! yolo { { $($tokens:tt)* } => { unsafe { $($tokens)* } }; }

Online

Board footer

Powered by FluxBB