# Path: dragonpy/utils/pager.py
# WINDOWS = os.name == 'nt'
# PY3K = sys.version_info >= (3,)
# STD_INPUT_HANDLE = -10
# STD_OUTPUT_HANDLE = -11
# STD_ERROR_HANDLE = -12
#     ENTER_ = '\x0d'
#     CTRL_C_ = '\x03'
#     ENTER_ = '\n'
#     CTRL_C_ = None
# ESC_ = '\x1b'
#     LEFT = ['\xe0', 'K']
#     UP = ['\xe0', 'H']
#     RIGHT = ['\xe0', 'M']
#     DOWN = ['\xe0', 'P']
#     LEFT = ['\x1b', '[', 'D']
#     UP = ['\x1b', '[', 'A']
#     RIGHT = ['\x1b', '[', 'C']
#     DOWN = ['\x1b', '[', 'B']
# ENTER = [ENTER_]
# ESC = [ESC_]
#     class COORD(Structure):
#     class SMALL_RECT(Structure):
#     class CONSOLE_SCREEN_BUFFER_INFO(Structure):
# def _windows_get_window_size():
# def _posix_get_window_size():
# def getwidth():
# def getheight():
# def dumpkey(key):
#     def hex3fy(key):
# def _getch_windows(_getall=False):
# def _getch_unix(_getall=False):
# def getchars():
# def echo(msg):
# def prompt(pagenum):
# def page(content, pagecallback=prompt):
# def _manual_test_console():
# def _manual_test_getch():
# def _manual_test_getchars():


import _thread
import logging
import queue
import sys
import threading
import time
    import tkinter
from dragonpy.utils import pager
###############################################################################
# Console Base ################################################################
###############################################################################


class InputPollThread(threading.Thread):
    def __init__(self, cpu_process, user_input_queue):
        super().__init__(name="InputThread")
        self.cpu_process = cpu_process
        self.user_input_queue = user_input_queue
        self.check_cpu_interval(cpu_process)

    def check_cpu_interval(self, cpu_process):
        """
        work-a-round for blocking input
        """
        try:
            #            log.critical("check_cpu_interval()")
            if not cpu_process.is_alive():
                log.critical("raise SystemExit, because CPU is not alive.")
                _thread.interrupt_main()
                raise SystemExit("Kill pager.getch()")
        except KeyboardInterrupt:
            _thread.interrupt_main()
        else:
            t = threading.Timer(1.0, self.check_cpu_interval, args=[cpu_process])
            t.start()

    def _run(self):
        while self.cpu_process.is_alive():

# Path: dragonpy/utils/pager.py
# WINDOWS = os.name == 'nt'
# PY3K = sys.version_info >= (3,)
# STD_INPUT_HANDLE = -10
# STD_OUTPUT_HANDLE = -11
# STD_ERROR_HANDLE = -12
#     ENTER_ = '\x0d'
#     CTRL_C_ = '\x03'
#     ENTER_ = '\n'
#     CTRL_C_ = None
# ESC_ = '\x1b'
#     LEFT = ['\xe0', 'K']
#     UP = ['\xe0', 'H']
#     RIGHT = ['\xe0', 'M']
#     DOWN = ['\xe0', 'P']
#     LEFT = ['\x1b', '[', 'D']
#     UP = ['\x1b', '[', 'A']
#     RIGHT = ['\x1b', '[', 'C']
#     DOWN = ['\x1b', '[', 'B']
# ENTER = [ENTER_]
# ESC = [ESC_]
#     class COORD(Structure):
#     class SMALL_RECT(Structure):
#     class CONSOLE_SCREEN_BUFFER_INFO(Structure):
# def _windows_get_window_size():
# def _posix_get_window_size():
# def getwidth():
# def getheight():
# def dumpkey(key):
#     def hex3fy(key):
# def _getch_windows(_getall=False):
# def _getch_unix(_getall=False):
# def getchars():
# def echo(msg):
# def prompt(pagenum):
# def page(content, pagecallback=prompt):
# def _manual_test_console():
# def _manual_test_getch():
# def _manual_test_getchars():


# Path: dragonpy/components/periphery.py
import _thread
import logging
import queue
import sys
import threading
import time
    import tkinter
from dragonpy.utils import pager

###############################################################################
# Console Base ################################################################
###############################################################################


class InputPollThread(threading.Thread):
    def __init__(self, cpu_process, user_input_queue):
        super().__init__(name="InputThread")
        self.cpu_process = cpu_process
        self.user_input_queue = user_input_queue
        self.check_cpu_interval(cpu_process)

    def check_cpu_interval(self, cpu_process):
        """
        work-a-round for blocking input
        """
        try:
            #            log.critical("check_cpu_interval()")
            if not cpu_process.is_alive():
                log.critical("raise SystemExit, because CPU is not alive.")
                _thread.interrupt_main()
                raise SystemExit("Kill pager.getch()")
        except KeyboardInterrupt:
            _thread.interrupt_main()
        else:
            t = threading.Timer(1.0, self.check_cpu_interval, args=[cpu_process])
            t.start()

    def _run(self):
        while self.cpu_process.is_alive():

            char = pager.getch()  # Important: It blocks while waiting for a input