from __future__ import annotations

from .adapter import Adapter, AdapterOutputWriter


class StdOutWriter(AdapterOutputWriter):
    def write(self, output: str) -> None:
        print(output, end="", flush=True)


class REPL:
    def __init__(self, adapter: Adapter) -> None:
        self.adapter = adapter
        self.adapter.output = StdOutWriter()

    def operate(self) -> None:
        self.adapter.output.write("ELMpythonator ready\n\r>")
        while True:
            try:
                line = input("")
            except (EOFError, KeyboardInterrupt):
                print("^D")
                break
            if not line:
                continue
            if line.startswith("quit"):
                print("")
                break
            self.adapter.feed_command(line + "\r")
