Beispiel #1
0
def mock_prompt_input():
    # TODO: remove if prompt-toolkit min version gets bumped
    if PIPE_INPUT_CONTEXT_MANAGER:
        with create_pipe_input() as pipe_input:
            with create_app_session(input=pipe_input, output=DummyOutput()):
                yield pipe_input
    else:
        pipe_input = create_pipe_input()
        try:
            with create_app_session(input=pipe_input, output=DummyOutput()):
                yield pipe_input
        finally:
            pipe_input.close()
Beispiel #2
0
def test_cli_argument():
    arguments = ['ws-ui']
    with patch.object(sys, 'argv', arguments):
        with create_app_session(input=create_pipe_input(),
                                output=DummyOutput()):
            app = Application()
            asyncio.run(app.main())
Beispiel #3
0
def app():
    pipe_input = create_pipe_input()
    try:
        with create_app_session(input=pipe_input, output=DummyOutput()):
            config = Config()
            app = App(config=config)
            yield app
    finally:
        pipe_input.close()
Beispiel #4
0
def mock_input():
    pipe_input = create_pipe_input()
    try:
        with create_app_session(
                input=pipe_input,
                output=DummyOutput(),
        ):
            yield pipe_input
    finally:
        pipe_input.close()
Beispiel #5
0
    def test_layout(self):
        input = create_pipe_input()
        with create_app_session(input=input, output=DummyOutput()):
            tui = TerminalUI()
            layout = tui.app.layout
            assert len(list(layout.find_all_windows())) > 1

            children = layout.container.get_children()
            assert len(children) == 2
            assert len(children[0].get_children()) == 2
def setup_stdout_redirection():
    """
    Prompt toolkit will try to create a VT100/Win32 output which expects stdout/stdout to have a file descriptor.
    Since Click's CliRunner wraps stdout in io.TextIOWrapper that does not have one we will get an error.
    Therefore we create a AppSession with an custom output module that simply writes to the current sys.stdout
    """
    class StdoutOutput(DummyOutput, ABC):
        def write(self, data: str) -> None:
            sys.stdout.write(data)

    # Create app session
    with create_app_session(input=DummyInput(), output=StdoutOutput()):
        # Yield control to the test case
        yield
def main():
    if sys.platform == "linux":
        from Xlib import XK

        mapping = get_xlib_mapping()
        key_pressed_context = xlib_key_pressed_context
        reverse_lookup = {
            v: k[3:] for k, v in XK.__dict__.items() if k.startswith("XK_")
        }
        mapping = reverse_lookup.get
    else:
        mapping = get_keyboard_mapping()
        key_pressed_context = pynput_key_pressed_context
        mapping = str

    with create_app_session() as session:
        with session.input.raw_mode():
            try:
                session.output.hide_cursor()
                with key_pressed_context() as get_pressed:
                    while True:
                        # Read keys
                        for key in session.input.read_keys():
                            if key.key == "c-c":
                                raise KeyboardInterrupt
                            if key.key == "c-d":
                                raise EOFError
                        # Get codes
                        codes = list(map(mapping, get_pressed()))
                        # Print pressed key codes
                        print(*codes, flush=True, end="")
                        # Tick
                        time.sleep(1 / 30)
                        # Clear line and hide cursor
                        session.output.write_raw("\r")
                        session.output.erase_down()
                        # Flush output
                        session.output.flush()
            except (KeyboardInterrupt, EOFError):
                pass
            finally:
                session.output.show_cursor()
                session.output.flush()
                print()
Beispiel #8
0
def main(args=None):
    parser = argparse.ArgumentParser(prog="gambaterm",
                                     description="Gambatte terminal front-end")
    add_base_arguments(parser)
    add_optional_arguments(parser)
    parser.add_argument("--disable-audio",
                        "--da",
                        action="store_true",
                        help="Disable audio entirely")
    args = parser.parse_args(args)

    if args.input_file is not None:
        save_directory = tempfile.mkdtemp()
        gb_input_context = gb_input_from_file_context(args.input_file,
                                                      args.skip_inputs)
    else:
        gb_input_context = gb_input_from_keyboard_context()
        save_directory = None
        if args.enable_controller:
            gb_input_context = combine_gb_input_from_controller_context(
                gb_input_context)

    if args.color_mode == 0:
        raise RuntimeError("No color mode seems to be supported")

    # Enter terminal raw mode
    with create_app_session() as app_session:
        with app_session.input.raw_mode():
            try:

                # Detect color mode
                if args.color_mode is None:
                    args.color_mode = detect_local_color_mode(app_session)

                # Prepare alternate screen
                app_session.output.enter_alternate_screen()
                app_session.output.erase_screen()
                app_session.output.hide_cursor()
                app_session.output.flush()

                # Enter input and audio contexts
                with gb_input_context as get_gb_input:
                    player = no_audio if args.disable_audio else audio_player
                    with player(args.speed_factor) as audio_out:

                        # Run the emulator
                        return_code = run(
                            args.romfile,
                            get_gb_input,
                            app_session=app_session,
                            audio_out=audio_out,
                            frame_advance=args.frame_advance,
                            color_mode=args.color_mode,
                            break_after=args.break_after,
                            speed_factor=args.speed_factor,
                            use_cpr_sync=args.cpr_sync,
                            save_directory=save_directory,
                            force_gameboy=args.force_gameboy,
                        )

            # Deal with ctrl+c and ctrl+d exceptions
            except (KeyboardInterrupt, EOFError):
                pass

            # Exit with return code
            else:
                exit(return_code)

            # Restore terminal to its initial state
            finally:
                # Wait for a possible CPR
                time.sleep(0.1)
                # Clear alternate screen
                app_session.input.read_keys()
                app_session.output.erase_screen()
                app_session.output.quit_alternate_screen()
                app_session.output.show_cursor()
                app_session.output.flush()